js结构赋值设置默认值

js yekong

当解构赋值时没有值会设置一个默认值

let [a = 'false'] = []
console.log(a)

编译成es5的结果

var _ref = [],
    _ref$ = _ref[0],
    a = _ref$ === undefined ? 'false' : _ref$;

console.log(a);

结构赋值 undefined和null的结果

let [a, b = 'wanjunshijie', c = 'wj2'] = ['wj', undefined, null]
console.log(a)
console.log(b)
console.log(c)

执行结果
wanjunshijiecom 2021-09-06 at 17.23.34@2x

喜欢