跳过 JavaScript 函数中的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32518615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Skip arguments in a JavaScript function
提问by Serge
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:
为了仅使用f
参数调用此函数,我知道我应该这样做:
foo(undefined, undefined, undefined, undefined, undefined, theFValue);
Is there a less verbose way to do this?
有没有更简洁的方法来做到这一点?
Solutions:
I selected some proposed solutions (without using helper third functions)
解决方案:
我选择了一些建议的解决方案(不使用辅助第三功能)
// zero - ideal one, actually not possible(?!)
foo(f: fValue);
// one - asks a "strange" declaration
var _ = undefined;
foo(_, _, _, _, _, fValue);
// two - asks the {} to be used instead of a 'natural' list of args
// - users should be aware about the internal structure of args obj
// so this option is not 'intellisense friendly'
function foo(args){
// do stuff with `args.a`, `args.b`, etc.
}
foo({f: fValue});
回答by Cerbrus
You coulduse apply
:
你可以使用apply
:
foo.apply(this, Array(5).concat([theFValue]));
In this case, 5
is the amount of parameters you want to skip.
在这种情况下,5
是您要跳过的参数数量。
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:
但是,在这种情况下,范围this
是不同的,因此您可能也需要传递它:
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:
再说一次,这个实现只允许传递 1 个参数。让我们改进一下:
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
:
这开始变得“有点”冗长。它也不会处理第一个参数之后丢失的参数,除非您想传递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 anyof the drawbacks in the earlier examples.
这种方法没有前面例子中的任何缺点。
回答by Pacerier
Such:
这样的:
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);
回答by Michael Laszlo
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 });
回答by sookie
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);
回答by Idan Dagan
If you will pass an object with a property name fso you can use destructuring assignmentwith ES6 syntax like this:
如果您将传递一个具有属性名称f的对象,那么您可以使用ES6 语法使用解构赋值,如下所示:
function foo({ f }) {
console.log(f);
}
foo({ g: 5, f: 10 });
回答by Tro
If this is something you're going to want to do often, then consider a simple wrapper:
如果这是你想要经常做的事情,那么考虑一个简单的包装器:
function bar(f) {
foo(undefined, undefined, undefined, undefined, undefined, f);
}
If you're only doing this once, or you're wanting a random permutation of the parameters then this approach isn't the best.
如果您只执行一次,或者您想要参数的随机排列,那么这种方法不是最好的。
回答by Nina Scholz
Use bind for a partial application:
对部分应用程序使用 bind:
function foo(a, b, c, d, e, f) {
document.write(f);
}
function skip(f, n) {
while (n--) {
f = f.bind(null, undefined);
}
return f;
}
skip(foo, 5)('hallo');