基础用法
function a(x = 1, y = 2) {
console.log(`x=${x},y=${y}`);
}
a(); // x=1,y=2
a(2,3) // x=2,y=3
使用默认值语法设置函数参数的默认值。
// bad
function handleThings(opts) {
opts = opts || {};
}
// good
function handleThings(opts = {}) {
// ...
}
网友评论