Reputation: 7255
My problem is I can't access the proper this object in my parent class.
Parent class :
ViewController.prototype.display = function() {
console.log(this); // Log a Window object
};
Child class :
ParentMissionsViewController.prototype = Object.create(ViewController.prototype);
ParentMissionsViewController.prototype.constructor = ParentMissionsViewController;
ParentMissionsViewController.prototype.parent = ViewController.prototype;
ParentMissionsViewController.prototype.display = function() {
// Other stuff here
this.parent.display.call();
};
I don't get why this isn't my current object, if someone can explain me ?
Upvotes: 0
Views: 78
Reputation: 72329
If you call
this.parent.display();
then it looks like:
var that = this.parent;
that.display();
which you, as I suppose, wanted to avert. Using call
is the good way, but you need to provide the value for this
:
this.parent.display.call(this /* , arg, arg... */);
See the signature of call
at MDN.
Upvotes: 3
Reputation: 481
Try
this.parent.display();
When you use call to invoke a method, you need to specify custom this value as first argument. What this value are you getting in current code? I am guessing undefined?
Upvotes: 0