Why do we build a prototype inheritance chain rather then using object composition. Looking up through the prototype for each step in the chain get's expensive.
Here is some dummy example code :
var lower = {
"foo": "bar"
};
var upper = {
"bar": "foo"
};
var chained = Object.create(lower, pd(upper));
var chainedPrototype = Object.create(chained);
var combinedPrototype = Object.create(pd.merge(lower, upper));
var o1 = Object.create(chainedPrototypes);
var o2 = Object.create(combinedPrototypes);
uses pd because property descriptors are verbose as hell.
o2.foo is faster then o1.foo since it only goes up two prototype chain rather then three.
Since travelling up the prototype chain is expensive why do we construct one instead of using object composition?
Another better example would be :
var Element = {
// Element methods
}
var Node = {
// Node methods
}
var setUpChain = Object.create(Element, pd(Node));
var chained = Object.create(setUpChain);
var combined = Object.create(pd.merge(Node, Element));
document.createChainedElement = function() {
return Object.create(chained);
}
document.createCombinedElement = function() {
return Object.create(combined);
}
I do not see any code merging prototype objects for efficiency. I see a lot of code building chained prototypes. Why is the latter more popular?
The only reason I can think of is using Object.isPrototypeOf to test for individual prototypes in your chain.
Apart from isPrototypeOf are there clear advantages to using inheritance over composition?