简体   繁体   English

一个简单的函数声明会在JavaScript中形成一个闭包吗?

[英]Will a simple function declaration form a closure in JavaScript?

Will the following code form a closure? 以下代码是否会形成闭包?

function f() {}

Does where this function is defined affect the answer? 定义此函数的位置会影响答案吗?

Yes, it forms a closure. 是的,它形成了一个封闭。

A closure is the combination of a function reference and the environment where the function is created. 闭包是函数引用和创建函数的环境的组合。 The code in the function will always have access to any variables defined in the scope where the function is created, no matter how the function is called. 无论函数如何调用,函数中的代码始终都可以访问创建函数的作用域中定义的任何变量。

Example; 例; the function f will always use the variable x even if it is called in a scope where x isn't reachable: 函数f将始终使用变量x即使它在x不可达的范围内调用:

 function container() { var x = 42; function f() { document.write(x); } return f; } var func = container(); func(); // displays 42 //document.write(x); // would give an error as x is not in scope 

You create a closure when the function has references to variables in its lexical scope: 当函数在其词法范围内引用变量时,您可以创建一个闭包:

var a = 'foo'
function f() {} // no closure
function f() {return a} // closure

If you place a debugger statement inside the function you can see what the closure contains in Chrome devtools. 如果在函数中放置一个debugger语句,您可以在Chrome devtools中看到该闭包包含的内容。 You'll see the first example has nothing, while the second will show a: 'foo' 你会看到第一个例子什么都没有,而第二个例子会显示a: 'foo'

Yes, it always defines a closure no matter where it is defined. 是的,它始终定义一个闭包,无论它在何处定义。 This tutorial has a few illustrative examples. 教程有一些说明性示例。

Because javascript is a special language (see scope, hoisting and more recently the TDZ), depending on how specific you are in your question you might get different answers. 因为javascript是一种特殊语言(请参阅范围,提升以及最近的TDZ),根据您在问题中的具体具体情况,您可能会得到不同的答案。

Is it a javascript closure? 这是一个javascript关闭? No. See definition by Mozilla https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures 请参阅Mozilla的定义https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures

Is it captured in a closure (or does it form a closure - the original question)? 它是在一个闭包中捕获的(还是形成一个闭包 - 最初的问题)? Depends on optimizations. 取决于优化。 But most likely yes. 但很可能是的。 See http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/ and more importantly https://en.wikipedia.org/wiki/Closure_%28computer_programming%29 请参阅http://dmitrysoshnikov.com/ecmascript/chapter-6-closures/ ,更重要的是https://en.wikipedia.org/wiki/Closure_%28computer_programming%29

The closure term is used by javascript "evangelists" to identify those functions that would not work correctly if they weren't captured in actual closures - this is the correct way of saying it from a fundamentally operational point of view. javascript“福音传道者”使用关闭术语来识别那些在实际闭包中没有被捕获时无法正常工作的函数 - 这是从基本操作的角度说出它的正确方法。 And the reason is probably due to a lack of a better term rather than anything else. 原因可能是由于缺乏更好的术语而不是其他任何东西。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM