简体   繁体   English

内部匿名函数如何具有外部函数的范围?

[英]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 . 没有ifforwhileswitch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从内部函数访问外部作用域? - How to access outer scope from inner function? 将变量从内部函数分配到外部作用域 - Assign variable from inner function to outer scope 将对“ this”匿名外部函数的引用传递给内部函数 - Passing reference to “this” anonymous outer function to passed inner function 如何在内部函数中调用外部“this”? - How to invoke outer “this” in inner function? Javascript范围:将外部函数中的变量传递给内部函数 - Javascript scope: passing a variable from an outer function to an inner function 将javascript参数从外部范围传递到匿名回调函数 - Passing a javascript parameter from an outer scope to an anonymous callback function 流类型不适用于匿名函数中的外部作用域变量 - Flow type does not apply to outer scope variables within an anonymous function 内部函数作用域的访问元素,而不是敲除中的外部元素 - accessing element of inner function scope instead of outer in knockout 如何从具有phonegap数据库SQLite查询的内部函数返回值到外部函数 - how to return a value from inner function that have a phonegap database SQLite query to the outer function 内部函数是外部函数的属性吗? - is the inner function a property of outer function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM