xbObjects revisited
Continuing the earlier discussion on Objects and inheritance, how does this affect the implementation of xbObjects.js?. It turns out that the approach used by xbObjects does not really benefit from the initialization via parent constructor function nor the normal JavaScript inheritance by setting a child class's prototype to an instance of the parent class. However, xbObject's implementation can be improved and I have made several improvements to increase speed and usefulness.
-
Since xbObjects have their prototypes constructed lazily, the use of the prototype to record parent class references does not eliminate the need for an additional data structure to record the inheritance chains.
-
Recording a reference to the parent constructor in the class does not help xbObjects with regard to initialization since one of the features of xbObjects is the use of a separate
init()
method which separates initialization from construction. -
The
parentMethod()
ofxbObject
as previously defined in xbObjects-1.0.js can definitely be improved using the appropriatecall/apply
calls on the parent methods. The previous implementation also walked up the inheritance chain to 'fake' the isa property as belonging to the class which actually implemented the overridden method. This has been improved by caching the class name of the implementing class of any overridden method. -
Although the approach used in xbObject to defer prototype construction breaks the
instanceof
operator, this was easily emulated and added to the api as a methodisInstanceOf
.
xbObject example
Advantages
-
Easy inherited intialization
-
Easy to call overridden methods from parent class
-
Prototype construction is delayed until the first instance of a class is created.
-
Inheritance chain is available from method
isInstanceOf()
Disadvantages
-
The inheritance chain is non-standard.