'closures' 的定义
Definition of 'closures'
让我问一个问题。它与 JavaScript 中的闭包有关,但与它们的工作方式无关。
David Flanagan 在他的 "JavaScript The Definitive Guide 6th Edition" 中写道:
...从技术上讲,所有 JavaScript 函数都是闭包:它们是对象,并且具有与之关联的作用域链。。 ..
这是正确的吗?我可以将每个函数(函数对象 + 它的作用域)称为 "closure" 吗?
堆栈的标签 "closures" 说:
闭包是第一个class函数,它引用(关闭)定义它的范围内的变量。如果闭包在其定义范围结束后仍然存在,它关闭的变量也将继续存在。
在 JavaScript 中,每个函数都引用其定义范围内的变量。所以,它仍然有效。
问题是: 为什么那么多开发者不这么想?这个理论有什么问题吗?不能作为通用定义吗?
这是一个很难确定的术语。简单地 声明 的函数只是一个函数。闭包是调用函数。通过调用一个函数,space 被分配给传递的参数和声明的局部变量。
如果一个函数只是 returns 一些值,并且该值只是一些简单的东西(例如,什么都没有,或者只是一个数字或一个字符串),那么闭包就会消失并且真的没有什么有趣的关于它。但是,如果某些参数或局部变量(大部分是相同的)"escape" 函数的引用,则闭包 — space 分配给局部变量,以及父链 spaces — 坚持。
下面是一些引用可以从函数中 "escape" 的方法:
function escape(x, y) {
return {
x: x,
y: y,
sum: function() { return x + y; }
};
}
var foo = escape(10, 20);
alert(foo.sum()); // 30
从函数返回并保存在 "foo" 中的对象将保留对这两个参数的引用。这是一个更有趣的例子:
function counter(start, increment) {
var current = start;
return function() {
var returnValue = current;
current += increment;
return returnValue;
};
}
var evenNumbers = counter(0, 2);
alert(evenNumbers()); // 0
alert(evenNumbers()); // 2
alert(evenNumbers()); // 4
在那一个中,返回值本身就是一个函数。该函数涉及引用参数 "increment" 和局部变量 "current".
的代码
我会把闭包的概念和函数的概念混为一谈 - class 对象。这两件事确实是分开的,尽管它们是协同的。
需要注意的是,我不是基本人格的形式主义者,而且我对术语真的很糟糕,所以这可能应该被否决。
从技术上讲,所有函数都是闭包。但是如果函数没有引用任何自由变量,闭包的环境就是空的。函数和闭包之间的区别只有在需要与函数代码一起保存的闭包变量时才有意义。因此,通常将不访问任何自由变量的函数称为 functions,将访问任何自由变量的函数称为 closures,因此您了解这个区别。
定义正确。
闭包保留它出生的范围
考虑这个简单的代码:
getLabelPrinter = function( label) {
var labelPrinter = function( value) {
document.write(label+": "+value);
}
return labelPrinter; // this returns a function
}
priceLabelPrinter = getLabelPrinter('The price is');
quantityLabelPrinter = getLabelPrinter('The quantity is');
priceLabelPrinter(100);
quantityLabelPrinter(200);
//output:
//The price is: 100 The quantity is: 200
I would try to answer your question knowing you were asked about what closures are during the interview (read it from the comments above).
First, I think you should be more specific with "think otherwise". How exactly?
Probably we can say something about this noop function's closure:
function() {}
But it seems it has no sense since there are no variables would bound on it's scope.
I think even this example is also not very good to consider:
function closureDemo() {
var localVar = true;
}
closureDemo();
Since its variable would be freed as there is no possibility to access it after this function call, so there is no difference between JavaScript and let's say C language.
Once again, since you said you have asked about what closures are on the interview, I suppose it would be much better to show the example where you can access some local variables via an external function you get after closureDemo()
call, first.喜欢
function closureDemo() {
var localVar = true;
window.externalFunc = function() {
localVar = !localVar; // this local variable is still alive
console.log(localVar); // despite function has been already run,
// that is it was closed over the scope
}
}
closureDemo();
externalFunc();
externalFunc();
Then to comment about other cases and then derive the most common definition as it more likely to get the interviewer to agree with you rather than to quote Flanagan and instantly try to find the page where you've read it as a better proof of your statement or something.
Probably your interviewer just thought you don't actually know about what closures are and just read the definition from the book. Anyhow I wish you good luck next time.
让我问一个问题。它与 JavaScript 中的闭包有关,但与它们的工作方式无关。
David Flanagan 在他的 "JavaScript The Definitive Guide 6th Edition" 中写道:
...从技术上讲,所有 JavaScript 函数都是闭包:它们是对象,并且具有与之关联的作用域链。。 ..
这是正确的吗?我可以将每个函数(函数对象 + 它的作用域)称为 "closure" 吗?
堆栈的标签 "closures" 说:
闭包是第一个class函数,它引用(关闭)定义它的范围内的变量。如果闭包在其定义范围结束后仍然存在,它关闭的变量也将继续存在。
在 JavaScript 中,每个函数都引用其定义范围内的变量。所以,它仍然有效。
问题是: 为什么那么多开发者不这么想?这个理论有什么问题吗?不能作为通用定义吗?
这是一个很难确定的术语。简单地 声明 的函数只是一个函数。闭包是调用函数。通过调用一个函数,space 被分配给传递的参数和声明的局部变量。
如果一个函数只是 returns 一些值,并且该值只是一些简单的东西(例如,什么都没有,或者只是一个数字或一个字符串),那么闭包就会消失并且真的没有什么有趣的关于它。但是,如果某些参数或局部变量(大部分是相同的)"escape" 函数的引用,则闭包 — space 分配给局部变量,以及父链 spaces — 坚持。
下面是一些引用可以从函数中 "escape" 的方法:
function escape(x, y) {
return {
x: x,
y: y,
sum: function() { return x + y; }
};
}
var foo = escape(10, 20);
alert(foo.sum()); // 30
从函数返回并保存在 "foo" 中的对象将保留对这两个参数的引用。这是一个更有趣的例子:
function counter(start, increment) {
var current = start;
return function() {
var returnValue = current;
current += increment;
return returnValue;
};
}
var evenNumbers = counter(0, 2);
alert(evenNumbers()); // 0
alert(evenNumbers()); // 2
alert(evenNumbers()); // 4
在那一个中,返回值本身就是一个函数。该函数涉及引用参数 "increment" 和局部变量 "current".
的代码我会把闭包的概念和函数的概念混为一谈 - class 对象。这两件事确实是分开的,尽管它们是协同的。
需要注意的是,我不是基本人格的形式主义者,而且我对术语真的很糟糕,所以这可能应该被否决。
从技术上讲,所有函数都是闭包。但是如果函数没有引用任何自由变量,闭包的环境就是空的。函数和闭包之间的区别只有在需要与函数代码一起保存的闭包变量时才有意义。因此,通常将不访问任何自由变量的函数称为 functions,将访问任何自由变量的函数称为 closures,因此您了解这个区别。
定义正确。 闭包保留它出生的范围
考虑这个简单的代码:
getLabelPrinter = function( label) {
var labelPrinter = function( value) {
document.write(label+": "+value);
}
return labelPrinter; // this returns a function
}
priceLabelPrinter = getLabelPrinter('The price is');
quantityLabelPrinter = getLabelPrinter('The quantity is');
priceLabelPrinter(100);
quantityLabelPrinter(200);
//output:
//The price is: 100 The quantity is: 200
I would try to answer your question knowing you were asked about what closures are during the interview (read it from the comments above).
First, I think you should be more specific with "think otherwise". How exactly?
Probably we can say something about this noop function's closure:
function() {}
But it seems it has no sense since there are no variables would bound on it's scope.
I think even this example is also not very good to consider:
function closureDemo() {
var localVar = true;
}
closureDemo();
Since its variable would be freed as there is no possibility to access it after this function call, so there is no difference between JavaScript and let's say C language.
Once again, since you said you have asked about what closures are on the interview, I suppose it would be much better to show the example where you can access some local variables via an external function you get after closureDemo()
call, first.喜欢
function closureDemo() {
var localVar = true;
window.externalFunc = function() {
localVar = !localVar; // this local variable is still alive
console.log(localVar); // despite function has been already run,
// that is it was closed over the scope
}
}
closureDemo();
externalFunc();
externalFunc();
Then to comment about other cases and then derive the most common definition as it more likely to get the interviewer to agree with you rather than to quote Flanagan and instantly try to find the page where you've read it as a better proof of your statement or something. Probably your interviewer just thought you don't actually know about what closures are and just read the definition from the book. Anyhow I wish you good luck next time.