admin管理员组文章数量:1134599
I have a function like this:
function foo(a, b, c, d, e, f) {
}
In order to call this function only with an f
argument, I know I should do:
foo(undefined, undefined, undefined, undefined, undefined, theFValue);
Is there a less verbose way to do this?
I have a function like this:
function foo(a, b, c, d, e, f) {
}
In order to call this function only with an f
argument, I know I should do:
foo(undefined, undefined, undefined, undefined, undefined, theFValue);
Is there a less verbose way to do this?
Share Improve this question edited Apr 23, 2023 at 8:13 jonrsharpe 122k30 gold badges264 silver badges472 bronze badges asked Sep 11, 2015 at 8:08 sergeserge 15.1k41 gold badges140 silver badges228 bronze badges 5 |9 Answers
Reset to default 84Such:
foo(undefined, undefined, undefined, undefined, undefined, arg1, arg2);
.is equal to:
foo(...Array(5), arg1, arg2);
.or:
foo(...[,,,,,], arg1, arg2);
Such:
foo(undefined, arg1, arg2);
.is equal to:
foo(...Array(1), arg1, arg2);
.or:
foo(...[,], arg1, arg2);
Such:
foo(arg1, arg2);
.is equal to:
foo(...Array(0), arg1, arg2);
.or:
foo(...[], arg1, arg2);
You could use apply
:
foo.apply(this, Array(5).concat([theFValue]));
In this case, 5
is the amount of parameters you want to skip.
Wrap that in a function:
function call(fn, skipParams, parameter) {
fn.apply(this, Array(skipParams).concat([parameter]));
}
call(foo, 5, theFValue);
However, in that case the scope of this
is different, so you may need to pass that, too:
function call(fn, skipParams, parameter, thisArg) {
fn.apply(thisArg, Array(skipParams).concat([parameter]));
}
call(foo, 5, theFValue, this);
Then again, this implementation only allows 1 parameter to be passed. Let's improve that:
function call(fn, skipParams, parameters, thisArg) {
fn.apply(thisArg, Array(skipParams).concat(parameters));
}
call(foo, 5, [theFValue, theGValue, theHValue], this);
That's starting to get a "little" verbose. It also doesn't handle missing parameters after the first parameter that well, unless you want to pass undefined
:
call(foo, 5, [theFValue, theGValue, theHValue, undefined, theJValue], this);
Or, something completely different:
var _ = undefined;
foo(_,_,_,_,_, theFValue);
On a more serious note:
Your best option to deal with optional parameters, is to change the way you're handling parameters. Simply pass an object:
function foo(parameters){
// do stuff with `parameters.a`, `parameters.b`, etc.
}
foo({c: 1, g: false});
This approach doesn't suffer from any of the drawbacks in the earlier examples.
A better way to deal with optional arguments is to pass an object whose attributes you look up:
function foo(options) {
var a = options.a,
b = options.b,
c = options.c,
d = options.d,
e = options.e,
f = options.f;
}
foo({ f: 15 });
Skip function:
const skip = (num) => new Array(num);
Skipping beginning params:
foo(...skip(4), f);
Skipping end params:
foo(f, ...skip(4));
Skipping middle params:
foo(f, ...skip(4), f2);
If you will pass an object with a property name f so you can use destructuring assignment with ES6 syntax like this:
function foo({ f }) {
console.log(f);
}
foo({ g: 5, f: 10 });
I provide some methods that may help you achieve, as below,
- Destructuring assignment (recommend)
- Optional_chaining
Method1: Destructuring assignment
Example1
function Person(name, {id="007", age=-1, info={msg:null, mood:undefined}}) {
return [name, id, age, info.msg, info.mood]
}
//
本文标签:
Skip arguments in a JavaScript functionStack Overflow
版权声明:本文标题:Skip arguments in a JavaScript function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人,
转载请联系作者并注明出处:http://www.betaflare.com/web/1736862057a1955958.html,
本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
var _ = undefined; foo(_,_,_,_,_, theFValue);
solution is buried in this long answer. You would of course do the initial declaration of_
once, in some common utility file that you include in every js file. – ToolmakerSteve Commented Oct 27, 2020 at 0:30