the 'new' keyword in javascript
DESCRIPTION
TRANSCRIPT
var simpleObject = { a: 'a' };
{ a: 'a' }
simpleObject.toString(); // um, where does this function come from?
var simpleObject = { a: 'a' };
{ a: 'a', __proto__: { ... toString: function, hasOwnProperty: function, ... } }
var Demo = function() { ... }; Demo.prototype.doSomething = function() { ... };
var Demo = function() { this.prop = 'text'; }; Demo.prototype.action = function() { alert(this.prop); };
var myDemo = Demo(); myDemo; // undefined myDemo.action() // ERROR window.prop; // 'text'
var Demo = function() { this.prop = 'text'; }; Demo.prototype.action = function() { alert(this.prop); };
var myDemo = Demo(); myDemo; // { prop:'a', action:function } window.prop; // undefined myDemo.action(); // alerts "text"
var Demo = function() { this.prop = 'text'; }; Demo.prototype.action = function() { alert(this.prop); }; var myDemo = new Demo();
var Demo = function() { this.prop = 'text'; }; Demo.prototype.action = function() { alert(this.prop); }; var myDemo = new Demo();
•
•
•
•
•
•
•
•
Object.create(myProtoObject);
var obj = Object.create(null); // returns { } obj.toString(); // ERROR
var Demo = function() { this.prop = 'text'; }; Demo.prototype.action = function() { alert(this.prop); }; var myDemo = new Demo();
var Demo = { init: function() { this.prop = 'text'; }, action: function() { alert(this.prop); } }; var myDemo = Object.create(Demo); myDemo.init();