我在Javascript中读过scope chain,但它对我来说没有任何意义,谁能告诉我什么是scope chain,它是如何处理图形或甚至是傻子都能理解的东西的。我用谷歌搜索了一下,但我没有找到任何可以理解的东西:
发布于 2016-03-06 05:31:04
我知道这是一个古老的帖子,但它仍然对开发人员有帮助。我想以一种稍微不同的方式来做这件事,因为这样对初学者理解作用域链接会更友好。以下是我修改后的代码版本:
var currentScope = 0; // global scope
function a () {
var currentScope = 1, one = 'scope1';
alert(currentScope);
function b () {
var currentScope = 2, two = 'scope2';
alert(currentScope);
function c () {
var currentScope = 3, three = 'scope3';
alert(currentScope);
alert(one + two + three); // climb up the scope chain to get one and two
}
c();
}
b();
}
a();
发布于 2009-09-27 18:44:17
这是关乎结束的问题。您可以在内部作用域中使用作用域外部的变量:
function get_inner_scope () {
var outer = 'Outer variable value';
return function () {
alert(outer);
}
}
f = get_inner_scope();
f(); // alerts Outer variable value
更多关于其他样本的详细信息,请点击以下链接:http://blogs.msdn.com/jscript/archive/2007/07/26/scope-chain-of-jscript-functions.aspx
https://stackoverflow.com/questions/1484143
复制相似问题