How To Call Parent Method From Child
I'm getting this error when I'm trying to call pranet method: Uncaught TypeError: Cannot read property 'call' of undefined http://jsfiddle.net/5o7we3bd/ function Parent() { this
Solution 1:
You're not putting a "parentFunction" on the "Parent" prototype. Your "Parent" constructor adds a "parentFunction" property to the instance, but that won't be visible as a function on the prototype.
In a constructor function, this
refers to the new instance that's been created as a result of calling via new
. Adding methods to an instance is a fine thing to do, but it's not at all the same as adding methods to the constructor prototype.
If you want to access that "parentFunction" added by the "Parent" constructor, you can save a reference:
functionChild() {
Parent.call(this);
var oldParentFunction = this.parentFunction;
this.parentFunction = function() {
oldParentFunction.call(this);
console.log('parentFunction from child');
}
}
Post a Comment for "How To Call Parent Method From Child"