1

In the following example, is there any way to get a reference to the someValue variable declared outside someFunction from within someFunction or is it completely obscured by the function's parameter of the same name. I appreciate that I could attach it to window and access it from within the function using this, but is there a way of accessing it in this situation?

[Edit] To clarify. I understand that the parameter is shadowing the variable. Obviously changing the name of the parameter would remove this issue. My question is whether there is any way to access the variable given this situation.

(function($){

   var someValue = 41;

   function someFunction(someValue) {

      console.log(someValue); //= 22

   }

   someFunction(22);


}(jQuery));
8
  • 2
    So in your example, you'd like the console.log() to output 41, correct? Commented Dec 7, 2012 at 10:31
  • 1
    can't you rename the function's param? Commented Dec 7, 2012 at 10:33
  • 1
    No. It is completely shadowed, and there is no way to get around it. Rename one the two variables. Commented Dec 7, 2012 at 10:34
  • 1
    once upon a time, there was a .__parent__ property which could get accessed by ECMAscript. But those are just rumors, because that was a long time ago. Currently, there is no option to directly access parent contexts. Commented Dec 7, 2012 at 10:39
  • @BenM Yes. That is correct. Commented Dec 7, 2012 at 11:05

3 Answers 3

2

You seem to be deliberately shadowing the variable, and then trying to get its value. Just give it a different name or rename your parameter.

   var someValue = 41;
   function someFunction(myParameter) {
      console.log(someValue); // someValue == 41
   }
   someFunction(22); // logs 41
Sign up to request clarification or add additional context in comments.

2 Comments

I know that's what is happening. My question was whether in this situation I could get access to the shadowed variable.
@1ndivisible No, you can't.
0

There is no way to log the someValue that was declared outside the function, unless you're going to pass the variable:

(function($){
    var someValue = 41;

    function someFunction(someValue, otherValue) {
        console.log(someValue, otherValue); //= 22 41
    }
    someFunction(22, someValue);
}(jQuery));

Or rename the parameter:

(function($){
    var someValue = 41;

    function someFunction(otherValue) {
        console.log(someValue); //= 41
    }
    someFunction(someValue);
}(jQuery));

This is because the someValue name you use as parameter overshadows the one declared outside the function.

Comments

0

Because you are using local variable (var), the context of someValue is set to the function. The best solution is to use a different name as suggested by others, if you are desperate to use the same name though you could attach it to window object by removing the var keyword like so:

(function($) {

    someValue = 41;

    function someFunction(someValue) {
        console.log(window.someValue); //= 41
        console.log(someValue); //= 22
    }

    someFunction(22);

}(jQuery));

Comments

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.