Javascript: Access the right scope "under" apply(...)

Posted by Chau on Stack Overflow See other posts from Stack Overflow or by Chau
Published on 2010-04-13T09:17:40Z Indexed on 2010/04/13 9:32 UTC
Read the original article Hit count: 210

Filed under:
|
|

This is a very old problem, but I cannot seem to get my head around the other solutions presented here.

I have an object

function ObjA() {
    var a = 1;
    this.methodA = function() {
        alert(a);
    }
}

which is instantiated like

var myObjA = new ObjA();

Later on, I assign my methodA as a handler function in an external Javascript Framework, which invokes it using the apply(...) method.

When the external framework executes my methodA, this belongs to the framework function invoking my method.

Since I cannot change how my method is called, how do I regain access to the private variable a?

My research tells me, that closures might be what I'm looking for.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about scope