What’s the purpose of prototype? [duplicate]

Using the prototype makes faster object creation since properties/methods on the prototype don’t have to be re-created each time a new object is created. When you do this: function animal() { this.name=”rover” this.set_name = function (name) { this.name = name } } The set_name method is created every time you create an animal. But when … Read more

Preserving a reference to “this” in JavaScript prototype functions [duplicate]

For preserving the context, the bind method is really useful, it’s now part of the recently released ECMAScript 5th Edition Specification, the implementation of this function is simple (only 8 lines long): // The .bind method from Prototype.js if (!Function.prototype.bind) { // check if native implementation available Function.prototype.bind = function(){ var fn = this, args … Read more

How does __proto__ differ from constructor.prototype?

I’ve been trying to wrap my head around this recently and finally came up with this “map” that I think sheds full light over the matter http://i.stack.imgur.com/KFzI3.png I know I’m not the first one making this up but it was more interesting figuring it out that finding it :-). Anyway, after that I found e.g. … Read more