开发者

Event Handler access context JavaScript

开发者 https://www.devze.com 2023-01-19 13:06 出处:网络
I have a question in JavaScript context. I\'m a little confused by this issue. The code below describes my question:

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(outOfScop开发者_如何学JAVAe);
  }
}

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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消