本文介绍了跳过JavaScript函数中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的功能:
function foo(a, b, c, d, e, f) {
}
为了仅使用 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?
解决方案:
我选择了一些建议的解决方案(不使用辅助函数)
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});
推荐答案
如此:
foo(undefined, undefined, undefined, undefined, undefined, arg1, arg2);
.等于:
foo(...Array(5), arg1, arg2);
.or:
foo(...[,,,,,], arg1, arg2);
这样:
foo(undefined, arg1, arg2);
.等于:
foo(...Array(1), arg1, arg2);
.or:
foo(...[,], arg1, arg2);
这样:
foo(arg1, arg2);
.等于:
foo(...Array(0), arg1, arg2);
.or:
foo(...[], arg1, arg2);
这篇关于跳过JavaScript函数中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!