const supersearch = function({a = 1, b}) {
console.log(arguments[0]);
console.log(a, b);
};
supersearch({b: 2});
预期成果:
{
"a": 1,
"b": 2
}
1 2但实际结果:
{
"b": 2
}
1 2我该怎么得到arguments?
医生:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
最新情况:
我现在能想到的一种方法,有更好的方法来写吗?
const supersearch = function({a, b}) {
console.log({...{a: 1}, ...arguments[0]});
console.log(a, b);
};
supersearch({b: 2});
发布于 2019-11-30 06:53:19
嗨,你定义了一个对象类型的参数,这样就发生了。
const supersearch = function({}) {
alert(JSON.stringify(arguments[0]));
alert(arguments[0].a + '' + arguments[0].b);
};
supersearch({a:1 ,b: 2});
发布于 2019-11-30 10:09:24
也许你在找这个
const supersearch = function({a = 1, b, ...rest}) {
console.log({a,b});
console.log(a, b);
};
supersearch({b: 2});
发布于 2019-11-30 10:23:06
我建议如下:
const supersearch = function(inputParams) {
const defaulParams = { a: 1};
const allParams = {...defaultParams, ...inputParams};
const {a,b} = allParams;
console.log(allParams);
console.log(a, b);
};https://stackoverflow.com/questions/59113384
复制相似问题