简体   繁体   English

在 JavaScript 中创建闭包时

[英]When closures are created in JavaScript

I want to know that when closures is created in JavaScript我想知道在 JavaScript 中创建闭包时

function outerFunc() {
    var a = 2;

    function() {
        console.log(a);
        console.log(b);
    };
    var b = 4;
}

var saveFunc = outerFunc();
saveFunc();

Is closures are created before execution of code or closure is created when outerFunc invoked or closures is created when we write innerFunc or closure is created when innerFunc is return from outerFunc and we store innerFunc with closure in saveFunc?是在执行代码之前创建闭包,还是在调用outerFunc时创建闭包,或者在我们编写innerFunc时创建闭包,或者当innerFunc从outerFunc返回并且我们将带有闭包的innerFunc存储在saveFunc中时创建闭包?

Think of it this way: "a closure is a thing."可以这样想:“闭包是一件事。” Specifically, it is a function() that has been declared within the body of another function.具体来说,它是在另一个函数体内声明的function() You can pass a reference to that function to allow it to be called, and, when you do, it magically will still have access to the variables (such as a, b in your example) which were available to it.您可以传递对该函数的引用以允许调用它,并且当您这样做时,它仍然可以神奇地访问可用的变量(例如您的示例中的a, b )。 That's the magick of closures.这就是闭包的神奇之处。

At the outerFunc() creation time.在 outerFunc() 创建时。 A closure is created everytime when we create a function.每次创建函数时都会创建一个闭包。

Please visit this link : Closures请访问此链接: 关闭

Closures are created at function definition time .闭包是在函数定义时创建的。 So, if you have:所以,如果你有:

var a = 1;
function foo() {
  console.log(a);
}

This is a closure.这是一个关闭。 It "closes over" the outside variables.它“关闭”了外部变量。 In this case a .在这种情况下, a . Honestly, the "closes over" never made sense to me (probably does from a different perspective), so I prefer to think of it this way - a function that has access to the outer scope.老实说,“关闭”对我来说从来没有意义(可能从不同的角度来看),所以我更喜欢这样想 - 一个可以访问外部作用域的函数。 In this case a comes from the outer scope.在这种情况下, a来自外部作用域。

This may seem a bit weird because a lot of examples and explanations of closures have something like:这可能看起来有点奇怪,因为很多关于闭包的例子和解释都是这样的:

 function outer() { var a = 1; return function inner(b) {//<----------- return a + b; // | } // | } // | // | var add1 = outer(); // | // | console.log(add1.name); // "inner" --- console.log(typeof a); // undefined console.log(add1(41)); // 42

Where the explanation is that inner is a closure.解释是inner是一个闭包。 It is is created as soon as outer is executed (definition time for inner ) and later returned from outer .outer执行后立即创建inner定义时间),稍后从outer返回。 The special property of the closure is that retains reference to the variable a which is currently out of scope.闭包的特殊属性是保留对当前超出范围的变量a引用。 This is true but it gives the wrong impression.这是真的,但它给人的印象是错误的。 There doesn't need to be an outside reference in the function for it to be a closure.函数中不需要外部引用即可使其成为闭包。 Even a pure function like:即使是一个纯函数,如:

function add(a, b) {
   return a + b;
}

which doesn't use outside references and is entirely self-contained forms a closure at creation.它不使用外部引用并且完全独立,在创建时形成一个闭包。

In short, any function in JavaScript is a closure and has never not been.简而言之,JavaScript 中的任何函数都是一个闭包,而且从来都不是。

However, this is slightly more academic.然而,这有点学术性。 As mentioned, the useful property of closures is that they retain references to the scope they are defined in. So, in practice we usually refer to closures only when this property is used - so, in the example with inner and outer - both have formed closures but we'd focus on inner because it actually uses the outer scope.如前所述,闭包的有用属性是它们保留对定义它们的作用域的引用。因此,在实践中,我们通常仅在使用此属性时才引用闭包——因此,在具有innerouter的示例中——两者都已形成闭包,但我们将重点放在inner因为它实际上使用外部作用域。

When an inner function uses the outer function's variable, then closer is created in javascript.当内部 function 使用外部函数的变量时,则在 javascript 中创建更紧密。

 // Closure is not created example function addSquares(a, b) { function square(x) { return x * x; } return square(a) + square(b); } // Closure is created example function addSquares(a, b) { function square(x) { return a * x; } return square(a) + square(b); }

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

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