0

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?

1

3 Answers 3

5

Actually, it has nothing to do with scope and everything to do with context.

See, when an event handler is called, this is set to whatever element the event is bound to. In this case, this is window.

Try something like this:

function Test() {
    var self = this;
    self.a = function() {return 'hello';};
    self.b = function() {alert(self.a());};
    window.onload = self.b;
}

By "saving" the context to a variable self, you avoid context problems.

Sign up to request clarification or add additional context in comments.

1 Comment

All answers did the job, upvoted. Chose yours as answer for explaining the binding of this.
3

Try

window.onload = this.b.bind(this)

When you write someObj.func in javascript, the resulting reference has no idea of someObj. Therefore, if you need the correct this in func, you have to provide it explicitly.

This can be done by closing this in the func's parent scope, as other answers suggest, but bind is more transparent in my opinion.

3 Comments

.bind() is not cross-browser, unfortunately.
@NiettheDarkAbsol this is why i didn't knew about that. but when it will be fully supported will be nice. Support table at the end of the page linked Mozilla docs about bind function
@NiettheDarkAbsol: No, but there are polyfills available. Have a look at the MDN documentation.
3

in your code this refers to the local function function() { alert(this.a()); }, if you want to refer to the "class scope" you have to store a refence to the class :

function Test() {
    var localTest = this;
    this.a = function() { return 'hello'; }
    this.b = function() { alert(localTest.a()); }
    window.onload = this.b;
}
test = new Test();

3 Comments

I'm not 100% sure what you mean by "in your code this refers to the local function function() { alert(this.a()); }" (what refers to the function? Did you mean this?), but I think it's wrong.
the this refers to local context, not the class context, so when you call the function, the object calling the funcion has the this scope, so you have to store a reference to the outern class
Yeah, that makes more sense (but you really should format this as code). But that's not what you have written in your answer. You wrote it so that it can be easily understood as that this refers to function() {...} (which would be wrong).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.