就是说要“将闭包分配给变量"在JavaScript上下文中正确吗? [英] Is saying to "assign a closure to a variable" correct in the context of JavaScript?

查看:22
本文介绍了就是说要“将闭包分配给变量"在JavaScript上下文中正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对示例代码中的第20行的解释感到好奇.

I am curious for the line 20 explanation in my example code.

在第9行,声明名称为 func1 的变量. 已分配给由功能 foo()调用返回的闭包 .

On line 9 variable with name func1 is declared. It's assigned to the closure that is returned by the function foo() invocation.`

我知道调用函数 foo()会同时返回函数 bar 和指向变量 a 的指针,该变量位于它的词法范围.由于闭包是在其词法范围内与所有变量结合在一起的函数,包括函数和类名,我可以说我正在将变量 func1 分配给闭包吗?

I know that the invocation of the function foo() returns both the function bar and the pointer to the variable a which is in its lexical scope. Since closure is a function combined with all of the variables in its lexical scope, including function and class names, can I say that I am assigning the variable func1 to the closure?

这种解释使用的是正确的词,而不是模糊的吗?您能提出一个更好,更简明的解释和释义的第20行吗?

Is this explanation uses the right words and not vague? Could you suggest a better and more concise explanation and paraphrase line 20?

function foo() {
  let a = 1;
  return function bar() {
    a += 100;
    console.log(a);
  }
}

let func1 = foo(); 
let func2 = foo();

func1(); // ???
func2(); // ???
func1(); // ???
func2(); // ???

/*
On line 9 variable with name `func1` is declared. 

!! It's assigned to the closure that is returned by the function `foo()` invocation. 

The closure contains a pointer to the variable `a` that is in the lexical scope of the function `bar` that is returned by the `foo` invocation.

On line 10 variable with name `func2` is declared. It's assigned to the value of closure that is returned by the function `bar()` invocation. The closure contains a pointer to the variable `a` that is in the lexical scope of the function `bar`.


Variables accessible through the closure during the `func1` invocation and `func2` invocation are two separate independent variables that just happen to have the same name (`a`). 

That is, this program would print the following to the console:
- 101
- 101
- 201
- 201
*/

推荐答案

我知道调用函数 foo()会同时返回函数 bar 和指向变量 a 的指针,该变量位于它的词法范围.

I know that the invocation of the function foo() returns both the function bar and the pointer to the variable a which is in its lexical scope.

它只返回一件事: foo 创建的函数 bar .函数 bar 本身具有对创建它的环境的引用,因此对 a 变量也具有引用.它会关闭该环境.所以 bar 是一个闭包.您在JavaScript代码中创建的所有函数都是闭包.

It just returns one thing: the function bar that foo creates. The function bar, itself, has a reference to the environment where it was created, and thus to the a variable; it closes over that environment. So bar is a closure. All functions you create in JavaScript code are closures.

...我可以说我正在将变量func1分配给闭包吗?

...can I say that I am assigning the variable func1 to the closure?

您不能为闭包分配任何内容;相反.您可以分配给变量(各种类型),也可以初始化常量.您在 const func1 = foo(); 中所做的是使用由以下函数返回的函数(如果愿意,您可以说"closure")初始化常量 func1 . foo .

You can't assign anything to closures; it's the other way around. You can assign to variables (of various kinds) and you can initialize constants. What you're doing in const func1 = foo(); is initializing the constant func1 with the the function (you could say "closure" if you like) returned by foo.

这篇关于就是说要“将闭包分配给变量"在JavaScript上下文中正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆