解构赋值与默认值结合使用
function foo({x, y = 5}) {
console.log(x, y)
}
foo({})
foo({x: 1})
foo({x: 1, y: 2})
foo({y: 3})
foo()
/** output
undefined 5
1 5
1 2
undefined 3
TypeError: Cannot destructure property `x` of 'undefined' or 'null'.
**/function foo_2({x, y = 5} = {}) {
console.log(x, y)
}
foo_2()
// undefined 5Last updated