I want to invoke a method from a parent class as part of a child class method in JavaScript, using pseudoclassical pattern. I've seen code samples where the parent class method is invoked by 'Parent.prototype.parentMethod.call(this)' as below:
function Parent(arg1) {
this.arg1 = arg1;
}
Parent.prototype.parentMethod = function() {
return `Argument 1 is: ${this.arg1}`;
};
function Child(arg1, arg2) {
Parent.call(this, arg1);
this.arg2 = arg2;
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.prototype.childMethod = function() {
return `${Parent.prototype.parentMethod.call(this)} and Argument 2 is: ${this.arg2}`;
};
In implementing the child method, why is it necessary (or desirable) to do 'Parent.prototype.parentMethod.call(this)' instead of just 'this.parentMethod'? Shouldn't the prototype chain make it possible to do 'this.parentMethod'?