为什么这段代码输出连续的数字? [英] Why does this code output consecutive numbers?
问题描述
我试图发布一个基本的 JS
技巧问题,这显然也在欺骗我:)
I was trying to post a basic JS
trick question, and it's apparently tricking me too :)
在你的控制台中运行:
x = (w) => w.length;
setTimeout(console.log(x), 0);
每次运行 setTimeout(console.log(x)时, 0);
你得到越来越多的数字。这是为什么?不应该 w
参数引用窗口
对象?
The, each time you run setTimeout(console.log(x), 0);
you get an increasing number. Why is that? Shouldn't the w
parameter referrer to the window
object?
推荐答案
setTimout
返回一个值。您将看到计时器ID,它是setTimeout的返回值,可以用作 clearTimeout
来电。
返回的timeoutID是一个数字非零值,它标识通过调用setTimeout()创建的计时器
;这个值可以是
传递给Window.clearTimeout()来取消超时。 [0]
The returned timeoutID is a numeric, non-zero value which identifies the timer created by the call to setTimeout(); this value can be passed to Window.clearTimeout() to cancel the timeout. [0]
[0] https://developer.mozilla.org/en-US/docs/Web/API/WindowTimers/setTimeout
w
参数不会引用窗口对象。 console.log返回undefined,因此永远不会调用你的函数。
The w
parameter would not refer to the window object. console.log returns undefined, so your function would never get called.
这篇关于为什么这段代码输出连续的数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!