I basically have an object, extended with a function through its prototype. Inside that function, another function exists, however when using this
in this nested function, it does not seem to refer to the object, but the function.
For example,
var sampleObject = function() {
this.foo = 123;
}
sampleObject.prototype.getFoo = function() {
var nested = function() {
return this.foo;
}
return nested();
}
var test = new sampleObject();
window.alert(test.getFoo()); // undefined
The this.foo
does not refer to the 123 value, but is undefined as this refers to the nested function, in which no foo
exists. How can I access the 123 value from the nested function?