I've been playing with javascript recently and I'm trying to e to grips with the advantages of pseudoclassical inheritance (as described by Crockford using the prototype property of objects). Crockford says he rarely uses it and prefers the functional approach, that is, creating a function that augments an object like
var obj = function() {
var self = {};
self.method = function() {
};
return self;
}
I prefer this approach, it's easier to understand and seems flexible.
However, I see lots of code that still use the prototype property, including popular frameworks like jQuery. I'm wondering what are the advantages? My understanding of using prototype property is that it gives us the ability to ask whether an object is a particular type by traversing the prototype chain. However, being a dynamic language, wouldn't it be better to ask if an object can do something, rather than what it is?
I've been playing with javascript recently and I'm trying to e to grips with the advantages of pseudoclassical inheritance (as described by Crockford using the prototype property of objects). Crockford says he rarely uses it and prefers the functional approach, that is, creating a function that augments an object like
var obj = function() {
var self = {};
self.method = function() {
};
return self;
}
I prefer this approach, it's easier to understand and seems flexible.
However, I see lots of code that still use the prototype property, including popular frameworks like jQuery. I'm wondering what are the advantages? My understanding of using prototype property is that it gives us the ability to ask whether an object is a particular type by traversing the prototype chain. However, being a dynamic language, wouldn't it be better to ask if an object can do something, rather than what it is?
Share Improve this question edited Nov 23, 2011 at 16:38 user1046334 asked Nov 23, 2011 at 16:25 stantonastantona 3,3002 gold badges26 silver badges29 bronze badges 1- 1 have a look here about the speed, blogs.msdn./b/kristoffer/archive/2007/02/13/… and allso check about javascript prototype inheritance – Poelinca Dorin Commented Nov 23, 2011 at 16:32
5 Answers
Reset to default 4The advantage I see of using prototype
instead of this approach is efficiency. Every instance of an object done the "classic" way (using prototype
) will share the methods, while the functional approach will create new methods for each.
Yes, it usually is "better to ask if an object can do something, rather than what it is" (sometimes known as "duck typing," because if an object looks like a duck and quacks like a duck...). Sometimes, though, you really want to know if an object is, for instance, an Array and not merely an object that has a length property.
The prototypal or pseudo-classical approach, however, also has a performance advantage when you are creating a large number of objects.
It's also much easier to override a method using the pseudo-classical approach. See the explanation at https://stackoverflow./q/4508498/9897.
People use the prototype because it's an inheritance construct.
If you want to inherit methods and properties from another object then you have to use the prototype.
var Proto = {
method: function () { }
}
var obj = function() {
return Object.create(Proto);
}
I think that two relatively important aspects are speed and RAM usage. If your constructor creates new instances of all those methods, this will both slow down your code and might also cause their outer scope (the constructor function) to be preserved, thereby causing more RAM usage.
Proof: http://jsperf./in-constr-vs-on-proto - at least in chrome, doing it in the constructor is 89% slower.
There is an important distinction between using a delegate prototype, and using pseudo-classical inheritance. Prototypal inheritance relies on setting the prototype for delegation, and prior to Object.create(), the only way to do that was to set the prototype property of a constructor function. Now that we have Object.create(), I use it instead of constructor functions, for the same purpose.
The primary reason to use a delegate prototype is to save resources by sharing the same memory for functions between all instances which delegate to that prototype.
The example you listed is called a factory function, and it is frequently used in bination with Object.create() in order to achieve convenient prototypal inheritance.