函数参数的默认值
1.
function log(x, y = 'world') {
console.log(x, y)
}
这里计算的是是否有值传入 y,如果没有(值为 undefined
)则设置 y 的默认值为 world
2.
function Point(x = 0, y = 0) {
this.x = x
this.y = y
}
const p = new Point()
console.log(p)
//Point { x: 0, y: 0 }
3.
let x = 99
function foo(p = x + 1) {
console.log(p)
}
foo()
// 100
x = 100
foo()
// 101
Last updated
Was this helpful?