3 ms·
Can you explain? It's sugar for this very common pattern: function Bar() { Foo.call(this); } Bar.prototype = Object.create(Foo.prototype);
by Touche 12y ago
Can you explain? It's sugar for this very common pattern:
function Bar() {
Foo.call(this);
}
Bar.prototype = Object.create(Foo.prototype);
Bar.prototype.constructor = Bar;
Foo.prototype.thing = function() { ....
What other "approach" to prototypical inheritance is there in JavaScript? The module pattern doesn't use prototypes.
- backslash_16 12y agoIs there a good book or website to learn about prototypical inheritance? I've read a lot of Crockford's book and some stuff online about it, but I've never run across the pattern you use above. It would be great to know what the benefits are vs declaring function literals on an object, or declaring a function directly on Bar's prototype.
- cstrahan 12y agoMozilla's documentation does a good job explaining prototypal inheritance: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inhe...
- jurassic 12y agoMy "Aha!" moment came while reading this Kyle Simpson blog series http://davidwalsh.name/javascript-objects http://davidwalsh.name/javascript-objects
- jbergens 12y agoThis blog post and presentation discusses this in more detail. http://ericleads.com/2013/02/fluent-javascript-three-different-kinds-of-prototypal-oo/ http://ericleads.com/2013/02/fluent-javascript-three-differe...