Sunday, May 15, 2016

prototype


var Person = function(){this.bar = 'bar'};
Person.prototype.foo = 'foo'; // Person.prototype = { foo : 'foo', ... }

var Chef = function(){this.goo = 'goo'};
Chef.prototype = new Person(); // Chef.prototype = { bar : 'bar', ... }

var cody = new Chef();
console.log(cody.goo); // logs 'goo': own prop
console.log(cody.bar); // logs 'bar': found bar from Chef's prototype
console.log(cody.foo); // logs 'foo': found foo from Chef's prototype's prototype

No comments:

Post a Comment