I have a simple constructor that has firstname and lastname.
function Parent(){
this.firstName;
this.lastName;
}
There are four functions defined in the prototype of that constructor which does it's own tasks.
Parent.prototype.flipName = function () {
return this.lastName + ' ' + this.firstName;
}
Parent.prototype.setFirstName = function (name) {
this.firstName = name;
}
Parent.prototype.setLastName = function (last) {
this.lastName = last;
}
Parent.prototype.getFullName = function (callback) {
// used alert for the sake of simplicity
alert("Callback: " + callback());
return this.firstName + ' ' + this.lastName;
}
To demonstrate this I have attached jsfiddle as well.
So my question is whenever I pass callback on getFullName function this
somehow loses the context to the Parent object (johny in our case) and returns as undefined undefined
. this
however, works fine on getFullName
function.
I am aware that this
is pointing to the window
object instead of the Parent
in the callback but I can't seem to find the reason behind it.