the 'new' keyword in javascript

Post on 15-Jan-2015

74 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

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();

top related