【发布时间】:2019-10-08 04:36:01
【问题描述】:
我很难理解这两个代码块之间的区别,以及为什么一个工作与另一个工作。我在一次讨论 JS 中的 Temporal Dead Zone 的演讲中看到了这些代码 sn-ps,但很难理解这个带有默认参数的特殊情况。
抛出ReferenceError
// sample1.js
const a = 2;
function square(a = a) {
return a * a;
}
// Does not work!
square();
作品
// sample2.js
const init = 2;
function square(a = init) {
return a * a;
}
// Works!
square(); // => 4
【问题讨论】: