I use one of the approaches to class inheritance in JavaScript (as used in the code that I modify), but I don’t understand how to attach additional functions for a method in a subclass to the functionality of the corresponding parent class, the method already has; in other words, I want to redefine the parent method in the child class using a method that, in addition to its own subclass of its own class, also executes the same method of the parent method. So, I'm trying to call the parent method from the child method, but is this possible?
Code here: http://jsfiddle.net/7zMnW/ . Please open the development console to see the result.
The code is also here:
function MakeAsSubclass (parent, child) { child.prototype = new parent; // No constructor arguments possible at this point. child.prototype.baseClass = parent.prototype.constructor; child.prototype.constructor = child; child.prototype.parent = child.prototype; // For the 2nd way of calling MethodB. } function Parent (inVar) { var parentVar = inVar; this.MethodA = function () {console.log("Parent MethodA sees parent local variable:", parentVar);}; this.MethodB = function () {console.log("Parent MethodB doesn't see parent local variable:", parentVar);}; } function Child (inVar) { Child.prototype.baseClass.apply(this, arguments); this.MethodB = function () { console.log("Child method start"); Child.prototype.MethodB.apply(this, arguments); // 1st way this.parent.MethodB.apply(this, arguments); // 2 2nd way console.log("Child method end"); }; } MakeAsSubclass(Parent, Child); var child = new Child(7); child.MethodA(); child.MethodB();
source share