Multiple ways of extending javascript objects
/** * One-way reference from subclass to superclass (instance) * Most of the time this is what you want. It should be done * before adding other methods to Subclass. */ ChildClass.prototype = new SuperClass(); /** * Two-way reference * Superclass will also get any Subclass methods added later. */ ChildClass.prototype = SuperClass.prototype; /** * Cloning behavior * This does not setup a reference, so instanceof will not work. */ angular.extend(ChildClass.prototype, SuperClass.prototype); /** * Enhancing a single instance * This could be used to implement the decorator pattern. */ angular.extend(subClassInstance, SuperClass.prototype);
If this post made you think, you'll probably like the next one. I write about what's actually changing in software engineering, not what LinkedIn wants you to believe. No spam, unsubscribe anytime.
I could not resist commenting. Perfectly written!