As far as I understand,Prototypal inheritance
and closure
are two incompatible ways for creating objects.
- With prototypes, all the instances share the same function
- With closures, each instance has its own function (with its specific closure)
By creating object using clojure
, I am referring to the following pattern:
function createPerson(p){
return {
getName: function() { return p;}
};
}
When in writing a Javascript application, prototype
is a better choice than closure
for creating objects? I am not looking for general explanations about the advantages of prototypal inheritance
. Instead, I would like to see a real-life web development scenario where prototype inheritance
is really useful.
The only useful case I can think about is for augmenting basic types.