函数参数默认值
2020-11-02 本文已影响0人
ticktackkk
基础用法
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 = {}) {
// ...
}