javascript 什么是自由变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12934929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are free variables?
提问by Geek
Javascript closure definition says :
Javascript 闭包定义说:
A "closure" is an expression (typically a function) that can have free variables together with an environment that binds those variables (that "closes" the expression).
“闭包”是一个表达式(通常是一个函数),它可以具有自由变量以及绑定这些变量(“关闭”表达式)的环境。
Can some one explain to me the concept of free variables ? Is this concept Javascript specific or applies to other languages also ?
有人可以向我解释自由变量的概念吗?这个概念是 Javascript 特定的还是也适用于其他语言?
回答by Denys Séguret
Free variables are simply the variables that are neither locally declared nor passed as parameter.
自由变量只是既没有在本地声明也没有作为参数传递的变量。
In computer programming, the term free variable refers to variables used in a function that are not local variables nor parameters of that function.1The term non-local variable is often a synonym in this context.
在计算机编程中,术语自由变量是指函数中使用的变量,这些变量既不是局部变量也不是该函数的参数。1在这种情况下,术语非局部变量通常是同义词。
In javascript closures, those are simply the variables that the function takes (read and write) in the enclosing scope where is declared the closure or in a parent scope.
在 javascript 闭包中,这些只是函数在声明闭包的封闭作用域或父作用域中采用(读取和写入)的变量。
Look at this real world example :
看看这个真实世界的例子:
Gol.prototype._ensureInit = function() {
...
var _this = this;
var setDim = function() {
_this.w = _this.canvas.clientWidth;
_this.h = _this.canvas.clientHeight;
_this.canvas.width = _this.w;
_this.canvas.height = _this.h;
_this.dimChanged = true;
_this.draw();
};
setDim();
window.addEventListener('resize', setDim);
...
};
In this example a closure points from the setDim
function towards the variable _this
declared in the enclosing scope (the _ensureInit
function). This variable isn't declared in setDim
nor passed. It's a "free variable".
在这个例子中,一个闭包从setDim
函数指向_this
在封闭作用域(_ensureInit
函数)中声明的变量。这个变量既没有声明setDim
也没有传递。这是一个“自由变量”。
Note that _this
doesn't become a variable of the function setDim
: another function declared in the same scope would share the same variable.
请注意,_this
它不会成为函数的变量setDim
:在同一范围内声明的另一个函数将共享相同的变量。
回答by jAndy
A "free-translation" could be: "out of scope" - variables
.
“免费翻译”可以是:"out of scope" - variables
.
Since ECMAscript uses lexical scoping, a free variable is a variable which was defined in a parent-scope and gets looked-up by a scope-chain search.
由于 ECMAscript 使用词法作用域,自由变量是在父作用域中定义并通过作用域链搜索进行查找的变量。
(function _outerScope() {
var foo = 42;
(function _innerScope() {
var bar = 100;
console.log( foo + bar ); // 142
}());
}());
In the above example, foo
is a free variablewithin the context of _innerScope
. it becomes very obvious if we have a quick glance into the underlying concepts of ECMAscript.
在上面的例子中,foo
是上下文中的自由变量_innerScope
。如果我们快速浏览一下 ECMAscript 的基本概念,就会变得非常明显。
A Contextis linked to an Activation Object(in ES3), respectively a Lexical Enviroment Record(in ES5), which contains things like: function declarations
, variables declared with var
and formal paramters
, as well as a reference to all parent Activation Objects/ Lexical Environments. If a variable needs to be accessed, the ECMAscript engine will first look into the AOs/ LEsfrom the current Contextitself; if it can't be found there, it looks into the parent AO's / LE's.
甲上下文链接到激活对象(在ES3),分别一个词汇环境记录(在ES5),其中包含像的东西:function declarations
,variables declared with var
和formal paramters
,以及对所有的参考父激活对象/词法环境。如果需要访问变量,ECMAscript 引擎将首先从当前上下文本身查看AO/ LE;如果在那里找不到它,它会查看父AO的 / LE。
Since any Contextstores this data in an array-like structure(don't forget we're talking about implementation level here, not Javascript itself), we are talking about Lexical Scope
, because we search through all parent Contextsin order.
由于任何Context 都将这些数据存储在类似数组的结构中(不要忘记我们在这里讨论的是实现级别,而不是 Javascript 本身),因此我们讨论的是Lexical Scope
,因为我们按顺序搜索所有父Context。
回答by Headshota
As an example:
举个例子:
var myModule = (function (){
var moduleVar; // closure variable
return function(){
// actual function
}
})();
the variable defined there is a closure variable. it can be used all over the closure itself but is not part of a global namespace.
定义的变量有一个闭包变量。它可以在整个闭包本身中使用,但不是全局命名空间的一部分。