I have the following error when running the page below:
“this.testpublic is not a function”
test = function() {
var testprivate = function() {
this.testpublic();
}
this.testpublic = function() {
console.log('test');
}
testprivate();
}
new test();
Apparently when testprivate is called, “this” starts pointing to “window” instead of the object.
Shouldn’t JavaScript preserve “this” context when within the same object?
You need to manipulate the context when calling testprivate. You can use function.call to override the scope of the function. Try this: