2

Possible Duplicate:
Is it possible to gain access to the closure of a function?

Say I have the following implementation of the revealing module pattern:

var myModule = (function() {
    var myVar,
        _self = this;

    function setMyVar( myVar ) {
        _self.myVar = myVar;
    }

    return {
        setMyVar: setMyVar
    }
})()

myModule.setMyVar('happy');​

What I want to do is set the module level myVar to be 'happy'. This doesn't work because 'this' is equal to window in the anonymous parent function. One easy work around would be to avoid child-parent name collisions, but putting that aside, is there a way I could have access to a reference to the anonymous function scope from inside a nested child function?

Community
  • 1
  • 1
robmisio
  • 1,066
  • 2
  • 12
  • 20
  • Looks like you want `function setMyVar( val ) { myVar = val; }`? I don't see what `this`/`_self` is supposed to do here. – Felix Kling Sep 23 '12 at 17:58
  • @felix: This was very much a theoretical exercise just to see if it where possible to access the variable of the parent function that has the same name as one in the local function. If this where an object instantiated with 'new' (i.e. not an anonomous self-executing function), then it would be possible. I guess the answer to my question is clearly 'no' and re-using the same variable name should be avoided. – robmisio Sep 23 '12 at 21:33
  • 2
    @robmisio: Even with `new` it isn't possible. When you use `new`, the `this` value refers to an object, but properties on that object doesn't have any relationship to the variables in the scope. In other words, `this.myVar` refers to entirely separate data than `var myVar`. – I Hate Lazy Sep 23 '12 at 21:44
  • @robmisio: What user1689607 said. There is no way you can access shadowed variables in a higher scope if it is not the global scope. It has nothing to do what `this` refers to inside a function. – Felix Kling Sep 23 '12 at 22:45

2 Answers2

3

"One easy work around would be to avoid child-parent name collisions"

Please don't consider that to be a workaround, but rather a best practice. Variable shadowing doesn't often add benefit, but can add confusion to code and bugs when you forget which variable you're working with.

The only variable scope (or variable object, or binding object... whatever) that is available as an object that can be directly manipulated in code is the "global" scope. No nested scopes allow this sort of direct access, so no, there's no other option other than to avoid shadowed variables.

This should generally not be an issue. A function variable or parameter can only access (and therefore shadow) variables in its original scope, so passing a function into a different scope will not cause any sort of conflict with the variables in that scope.

If needed, code validators like http://jshint.com are able to catch shadowed variables for you.

I Hate Lazy
  • 47,415
  • 13
  • 86
  • 77
1

No. If it is masked, it is masked and you can't get access to it.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335