0

Is it possible to access variables within an anonymous function outside of said function?

I may be using the wrong terminology.

Using the below pseudo code, would it be possible to access var1 in another script loaded on the same page?

function(i,e){
    var var1;
    NewFunc.init = function(){
         var1 = 5; 
    };
}
3
  • Side Note: You have syntax errors in that code which makes it a bit tricky to be sure of your structure. The question text is probably clear enough, but it's worth fixing the code... Commented Jul 20, 2016 at 16:32
  • "I may be using the wrong terminology." Nope, that's just about perfect terminology. Commented Jul 20, 2016 at 16:43
  • Ah thanks, just wanted to make sure. Commented Jul 20, 2016 at 19:04

1 Answer 1

4

Is it possible to access variables within an anonymous function outside of said function?

No, and in fact this is one of the things we use functions for: Hiding things. :-) More below about why that's not just a quirk of syntax.

Variables within a function are entirely private to that function and other functions created within it (which "close over" the variables in scope where they're created). For instance:

function foo() {
  var answer = 42;

  function bar() {
    var question = "Life, the Universe, and Everything";
    console.log(question, answer); // Works fine
  }
  console.log(question, answer);   // Fails with a ReferenceError because
                                   // `question` is out of scope
}
foo();

bar can access foo's answer variable because bar is created within foo. But foo cannot access bar's question variable because foo isn't created within bar.

This means it's possible to create functions that can access and modify a private variable:

// This is ES5 and earlier style; in ES2015+, it could be a bit more concise
function foo() {
  var answer = 42;
  return {
    getAnswer: function() {
      return answer;
    },
    setAnswer: function(value) {
      answer = value;
    }
  };
}
var o = foo();
console.log(o.getAnswer()); // 42
o.setAnswer(67);
console.log(o.getAnswer()); // 67

Note that if we tried to replace

console.log(o.getAnswer()); // 42

with

console.log(answer);

there are two good reasons that fails:

  1. The variable is out of scope

  2. If somehow it were in scope, which answer should it refer to? We can call foo more than once, and each call creates a new answer variable, so...


Side note: It makes no difference whether the function is named or anonymous, or whether it's a normal function or one of ES2015's new "arrow" functions.

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

3 Comments

Was afraid that would be the case - thanks for your quick response!
@eggman: FWIW, added a bit more information which may help with what you're trying to fix (your "was afraid that would be the case" suggests to me you're trying to fix something :-) ).
Crowder: Yes was looking for a solution will try a different approach now. Thanks a lot for your detailed explanation, it has certainly helped to understand the scope of variables within functions.

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.