[英]How inner anonymous function have scope of outer function?
In the script shown below 在下面显示的脚本中
$(function(){
var outerValue="OuterValue";
$('#btnScope').click(function(){
alert(outerValue);
});
});
The outer function (ie $()
) executes when the page loads. 外部函数(即
$()
)在页面加载时执行。 At this time click event will be bound to anonymous function (which alerts). 此时,click事件将绑定到匿名功能(此功能会发出警报)。 This function uses value of
outerValue
which might have lost scope after completing the ready( $()
) function. 该函数使用
outerValue
值,该值可能在完成ready( $()
)函数后失去了作用域。 How this is possible? 这怎么可能? How could I know the scope of variable?
我怎么知道变量的范围?
How the interpreter define its scope? 口译员如何定义其范围?
The outer scope is not lost . 外部范围不会丢失 。 The scoping as you describe it is fairly accurate.
您所描述的范围界定是相当准确的。
A variable's scope is roughly where it's defined (where var
is). 变量的范围大致是定义它的位置(
var
在哪里)。 Any inner scope can access anything in it's outer scope. 任何内部作用域都可以访问其外部作用域中的任何内容。 Only functions have scope though;
但是只有功能具有作用域; not
if
, for
, while
or switch
. 没有
if
, for
, while
或switch
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.