Crockford Prototypical Inheritance... No Prototype Chain? No Super?
Solution 1:
The technique is centered on the prototype chain, this type of inheritance is also known as differential inheritance.
For example:
var obj = {};
var obj1 = Object.create(obj);
var obj2 = Object.create(obj1);
The prototype chain of obj2
looks like this:
-------- -------- ------------------ obj2 ---> | obj1 | -----> | obj | -----> | Object.prototype | -----> null -------- ------- ------------------
The arrow that connects the objects in the above example is the internal [[Prototype]]
property, and that forms the prototype chain.
When you try to access a property, for example in obj2
, it will be searched along all the objects on the prototype chain until is found, otherwise the property accessor will simply yield undefined
The concept of super doesn't really exist, although there are ways to know the [[Prototype]]
of an object.
The ECMAScript 5th Edition introduced the Object.getPrototypeOf
method, for example:
Object.getPrototypeOf(obj2) === obj1; // true
However this method is not widely supported yet (along with the standard Object.create
).
Some implementations provide access to an object's [[Prototype]]
through the __proto__
property, e.g.:
obj2.__proto__ === obj1; // true
But remember that __proto__
is non-standard.
The isPrototypeOf
method, part of ECMAScript 3, allows you to know if an object is in the prototype chain of another, e.g.:
obj1.isPrototypeOf(obj2); //true
obj.isPrototypeOf(obj2); //true
Object.prototype.isPrototypeOf(obj2); //true
In conclusion, a property is resolved as soon is found in the prototype chain, and if you want to avoid __proto__
and Object.getPrototypeOf
(since the first is non-standard and the former is not widely supported yet), I would simply recommend you to prefix your init
method in the base object, so it can be accessible from the more specific instances, e.g.:
var base = {
baseInit: function () { /*...*/ }
};
var specific = Object.create(base);
specific.init = function () {
//...this.baseInit();
//...
};
specific.init();
Post a Comment for "Crockford Prototypical Inheritance... No Prototype Chain? No Super?"