解决解构默认值只有在值为 undefined 时才能触发设置的默认值问题, 减少和后端沟通的时间. (English Doc)
$ npm i babel-plugin-destructuring-with-null -D
const { name = 'tom' } = null
const { list: [, first = 'foo', second = 'bar'] } = { list: [false, null, undefined] };
↓ ↓ ↓ ↓ ↓ ↓
const _ref = null,
name = _destructuring_with_null(_ref, 'name', 'tom'); // name === 'tom'
const _list = {list: [false, null, undefined]},
_list$list = _slicedToArray(_list.list, 3),
first = _destructuring_with_null(_list$list, '1', 'foo'), // first === 'foo',
second = _destructuring_with_null(_list$list, '2', 'bar');// second === 'bar';
.babelrc
{
"plugins": [
"babel-plugin-destructuring-with-null"
]
}
ES6 destructuring 只有在获取值为 undefined 的时候才会触发设置的默认值, 但是其他语言没有 undefined 这个东西, null 就意味着不存在. 这个插件存在的目的就是为了避免在解构之前的多次判断解构对象是否为 null.
Copyright (c) 2017-present, Army-U. Released under the MIT License.