【发布时间】:2019-07-29 19:39:10
【问题描述】:
console.log((function(x, f = () => x) {
var x;
var y = x;
x = 2;
return [x, y, f()];
})(1));
这是代码 sn-p。这里的输出是 (3) [2, 1, 1] 第三个输出是 1 而不是 2 怎么样?
另外,如果我在 Scratch JS 上运行此代码,它会打印 (3) [2, 1, 2]
为什么这里的输出不同?
我尝试在 IIFE 函数中传递第二个参数,如下所示:
console.log((function(x, f = (z) => z) {
var x;
var y = x;
x = 2;
return [x, y, f(z)];
})(1, 2));
但这会引发错误
chrome 控制台中的输出为 (3) [2, 1, 1],但 Scratch JS 中的输出为 (3) [2, 1, 2]
【问题讨论】:
标签: javascript console.log arrow-functions iife hoisting