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.
精彩评论