0

I have a module

something = {
    value : "",
    anotherVal : "",
    method : function(){
           this.anotherVal = "I think it is accessable here";
           GLOBAL.action(
           func : function(){
               // In here I want to access the above value.
               // Which is a property of the outer something module.
               <![magicOccursHere]!>.value = "magically access outer scope"
           });
    }
}

As you can see from the above code I would like to access a property on a level of scope outside of this by using some magicaOccursHere...

I have done this before by making the module return a function not a json object and then naming each level. But I dislike this syntax.

SOMETHING = new Something();
something = function Something() {
    var bigDaddy = this;
    this.value = "";
    this.anotherVal = "";
    this.method = function(){
           bigdaddy.anotherVal = "Set one of big daddies vars";
           GLOBAL.action(
           func : function(){
               // In here I want to access the above value.
               // Which is a property of the outer something module.
               bigdaddy.value = "can still set one of big daddies vars"
           });
    }
}

I may be completely missing the point here and if you would like to point me to it, then I would be only too happy to read.

Am I being too harsh on the messy pattern and code of option 2?

1 Answer 1

1

If you create a local variable inside your method function, it will be in scope in the inner func:

var something = {
    value : "",
    anotherVal : "",
    method : function(){
           var obj = this;
           this.anotherVal = "I think it is accessable here";
           GLOBAL.action(
           func : function(){
               // In here I want to access the above value.
               // Which is a property of the outer something module.
               obj.anotherVal = "magically access outer scope"
           });
    }
}

Or you can use the fully qualified path:

var something = {
    value : "",
    anotherVal : "",
    method : function(){
           this.anotherVal = "I think it is accessable here";
           GLOBAL.action(
           func : function(){
               // In here I want to access the above value.
               // Which is a property of the outer something module.
               something.anotherVal = "magically access outer scope"
           });
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It's all so simple and obvious when you think about it. Thankyou very much.

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.