0

I have a question in JavaScript context. I'm a little confused by this issue. The code below describes my question:

$(..).someFunction{
  var outOfScope = "OUT OF SCOPE!";

  $('somelink').click(handler);

  function handler() {
    alert(outOfScope);
  }
}

My question is: how outOfScope variable (which was defined outside the handler) is seen inside the handler?

1
  • See also Jibbering's Javascript Closures FAQ. Commented Mar 5, 2011 at 21:13

2 Answers 2

4

The variable outOfScope is scoped to someFunction, so it is available inside someFunction.

The function handler is inside someFunction, so the variable outOfScope is still available.

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

Comments

0

That´s how JavaScript works.

All variables that are defined directly inside a scope will also be available in all the scopes that are defined inside the scope.

1 Comment

Not quite true. Variables inside the scopes that are inside a scope are not available to the outer scope or other inner scopes. Just to be picky ;) Maybe you could put "directly inside a scope".

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.