开发者

Event Handler access context JavaScript

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜