javascript - 关闭异步操作

标签 javascript closures

我有几个关于关闭的问题。抱歉,如果这看起来只是另一个关闭问题,有大量的帖子和文章,我读了很多,但我仍然有问题,找不到答案。

    var message = 'Hello!';
    document.getElementById('foo').onclick = function(){alert(message)};
    message = 'Goodbye!';

这里会提示“再见”,解释是:“闭包不仅仅是传递变量的值,甚至是对变量的引用。闭包捕获变量本身!”

一个。拜托,任何人都可以向我解释“捕获变量本身”的含义以及消息是“再见”而不是“你好”的原因吗?

函数或警报是闭包?

另外,我是否纠正了以下内容:onclick 被添加到事件表中,将通知(发生单击时)发送到事件队列,事件循环检查调用堆栈,它是空的,所以警报消息正在添加并执行。当这一切发生时,JS 引擎已经到达代码行 -> message = 'Goodbye!';这就是为什么最终提醒“再见”?!

    for (var i = 1; i < 5; i++) {
      setTimeout(() => console.log(i), i*1000)
    }

vs 

    for (let i = 1; i < 5; i++) {
      setTimeout(() => console.log(i), i*1000)
    }

    for (var i = 1; i < 5; i++) {
      (function(i){
       setTimeout(() => console.log(i), i*1000)
      })(i)
    }
  • 我总是读到“当第一个 setTimeout() 运行时,我已经在 5 了,我们没有任何方法可以引用它”——但是什么时候首先 setTimeout() 运行,循环结束了吗?这实际上与事件队列等有关吗?!
  • 我也经常读到问题是变量 i 没有被引用。我只是不明白引用变量 i 与循环完成后运行的 setTimeout 有什么关系。
  • 当我使用 let 而不是 var 时,我总是阅读 let 它的 block 作用域,而不是函数作用域,所以我不会成为全局的。我明白了。但是有人可以解释一下,究竟发生了什么 - 为什么/我不是全局变量负责 setTimeout 在 for 循环完成后不被调用?我不明白其中的联系。
  • 另外,有人可以解释一下为什么 i*1000 不会像我理解的那样总是 5 吗?这是因为它在关闭之外?

非常感谢,如果有任何不清楚的地方,请告诉我。

最佳答案

Please, can anyone explain to me what "capturing variable itself" means and how is THAT responsible for the message to be "goodbye" and not "hello"

该函数只是提醒 message 变量的值。

当函数运行时,该值为 'Goodbye!'

the function or the alert is the closure?

那个例子中没有闭包。

Also, am I correct with the following: the onclick is added to event table, that sends notice (when click occurred) to event queue, event loop checks call stack, its empty so the alert message is being added and executed. While all that is happening, JS engine already reached the line of code -> message = 'Goodbye!'; and this is why "Goodbye" is ultimately alerted?!

没有。

  1. 您将 'Hello!' 分配给变量
  2. 您分配事件处理程序
  3. 您将 'Goodbye!' 分配给变量
  4. 元素被点击
  5. 事件触发
  6. 调用函数

在点击发生之前它不会被添加到堆栈中,因此它不会在步骤 2 和步骤 3 之间添加。

I always read "by the time the first setTimeout() runs, i is already at 5, and we don’t have any way to reference it" - but what is the reason that when first setTimeout() runs, the loop has finished? Has that actually to do with the event queue etc?!

setTimeout 设置一个函数在以后运行,不会阻塞其他代码运行,所以循环一直运行,改变i

I also always read the problem is that variable i isnt referenced.

这与引用无关。

I just dont understand how referencing variable i has anything to do with setTimeout running after loop already finished.

i的关系与setTimeout无关。您传递给 setTimeout 的函数明确使用 i。这就是它的重要性。

But could someone explain, what exactly happens - why/how is i not being global variable responsible for setTimeout NOT being called after for loop finished? I dont understand the connection.

同样,与 setTimeout 没有任何关系。

每次循环时,您都会创建一个新函数并将其传递给 setTimeout。该函数使用 i

如果 i 的作用域是 block 而不是包含函数,那么每次你绕过循环你都会得到一个不同的 i变量(而不是 same i 变量,您将使用 var 或全局变量)。

Also, please can someone explain why with i*1000, i will not as I understand be always 5? Its because its outside of the closure?

没有。这是因为它在传递给 setTimeout 的函数之外所以被使用 立即使用当前值,而不是稍后i 的值更改为循环后的任何值之后已经完成了。

关于javascript - 关闭异步操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59590178/

相关文章:

javascript - 将方法分配给原型(prototype)不起作用

JavaScript 闭包范围

python - Python 2.7.x 中部分函数的动态生成

javascript - 订单函数调用

php - 为什么 PHP 需要闭包的 use 运算符?

javascript - 在循环的setTimeout中使用IIFE,但是为什么呢?

javascript - Chrome 进度丰富通知状态不会上移

javascript - 我怎样才能通过闭包问题来增加全局变量

javascript - 我正在测试 ArrayBuffer 并想知道为什么结果看起来像这样 :

javascript - MVVM "binder"定义及其用法?