Isolating the inheritance part into a function
Let's move the code that takes care of all the inheritance details from the last example into a reusable extend()
function:
function extend(Child, Parent) { var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.uber = Parent.prototype; }
Using this function (or your own custom version of it) helps you keep your code clean with regard to the repetitive inheritance-related tasks. This way, you can inherit by simply using the following two lines of code:
extend(TwoDShape, Shape); extend(Triangle, TwoDShape);
Let's see a complete example:
// inheritance helper function extend(Child, Parent) { var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child...