我无法理解这两个代码块之间的区别以及为什么一个代码块与另一个代码块有效。我在一次关于 JS 中的临时死区的演讲中看到了这些代码片段,但是很难理解这个带有默认参数的特殊情况。
抛出一个 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
白板的微信
慕斯王
30秒到达战场
慕田峪9158850
随时随地看视频慕课网APP
相关分类