函数默认参数的TDZ

我们知道块级作用域会有TDZ. 其实方法参数也存在TDZ

function add(first = second, second) {
    return first + second;
}

console.log(add(1, 1));         // 2
console.log(add(undefined, 1)); // throws error

上面这段代码在调用时初始化默认函数的时候,其实时做了下面的事情.

/ JavaScript representation of call to add(1, 1)
let first = 1;
let second = 1;

// JavaScript representation of call to add(undefined, 1)
let first = second;
let second = 1;

所以会报错。

posted @ 2019-08-28 15:34  来亦何哀  阅读(171)  评论(0编辑  收藏  举报