没办法,JS 不支持类似其他语言中的命名参数写法:
function func(arg1 = null, arg2 = null, arg3 = null) {
}
func(arg3: true); // wrong
但我建议你需要传三个以上参数的时候就不要这么写,应换成对象形式传入,配置 Object.assign
来设置默认值,这样无论是可读性还是可扩展性都会好很多:
function query(options = {}) {
options = Object.assign({
pageIndex: this.pageIndex,
isOverseas: this.isOverseas,
isSen: this.isSen,
ip: this.ip,
traffic: this.traffic,
system: this.system,
time: this.time,
flag: true
}, options, {});
}
query({ flag: false }); // correct
P.S. 换成对象形式后也可以直接在函数参数里写默认值,但没有 Object.assign
兼容性好。
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…