So I have this Javascript function (class):
function Test() {
this.a = function() { return 'hello'; }
this.b = function() { alert(this.a()); }
window.onload = this.b;
}
test = new Test();
The code does not work, because the function this.b on window load becomes a global function (outside of the Test function/class) where this.a does not exist.
Something like this:
function Test() {
this.a = function() { return 'hello'; }
this.b = function() { alert(test.a()); } // changed this to test
window.onload = this.b;
}
test = new Test();
does work, but it assumes I know which variable holds the Test function/class which loses the functionality of creating multiple classes.
What is the best solution to maintain this approach (using this pointers inside function/class) and get the wanted result?