javascript Javascript跳过函数调用中的参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25642690/
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
Javascript skip arguments in function call
提问by Vee6
javascript beginner here.
javascript初学者在这里。
Let's say I'm having a javascript function that takes 3 arguments:
假设我有一个带有 3 个参数的 javascript 函数:
function f(arg1, arg2, arg3) { // do stuff }
I know that I can call f(value1, value2); and in that case inside the function scope arg1 will be value1, arg2 will be value2 and arg3 will be null.
我知道我可以调用 f(value1, value2); 在这种情况下,函数范围内的 arg1 将为 value1,arg2 将为 value2,arg3 将为 null。
Everything ok with this. However if I want to call the function giving values only to arg1
and arg3
I need to do something like this: f(value1, null, value2)
;
一切正常。但是,如果我要调用的函数只给值arg1
和arg3
我需要做的是这样的:f(value1, null, value2)
;
Is there a way I can specify which arguments to have which values in a more C#-esque manner (without specifying not given arguments as null)? Something like this: for calling f with values only for arg1
and arg3
I would write f(value1, arg3 = value2);
有没有一种方法可以指定哪些参数以更具 C# 风格的方式具有哪些值(不指定未给定的参数为空)?像这样:为了调用 f 的值只为arg1
,arg3
我会写f(value1, arg3 = value2);
Any ideas? Cheers!
有任何想法吗?干杯!
回答by Manjar
there is a way i have seen for this:
我已经看到了一种方法:
for example
例如
function person(name,surname,age)
{
...
}
person('Xavier',null,30);
you can do this:
你可以这样做:
function person(paramObj)
{
var name = paramObj.name;
var surname = paramObj.surname;
var age = paramObj.age;
}
calling like this:
像这样调用:
person({name:'Xavier',age:30});
I think this is the closest you'll be able to do it like in c# have in mind that JS is not compilled so you can't predict the arguments of a function.
我认为这是您在 c# 中能够做到的最接近的事情,请记住,JS 未编译,因此您无法预测函数的参数。
EDIT:
编辑:
For better syntax you can use ES6 object destructuring, like this:
为了获得更好的语法,您可以使用 ES6 对象解构,如下所示:
function person(somePerson)
{
let {name, surname, age} = somePerson;
}
回答by Lix
The only way you would be able to do this with JS is to pass one array containing all of the parameters.
使用 JS 执行此操作的唯一方法是传递一个包含所有参数的数组。
Your default values would have to be set within the function - you can't define default values for arguments in JavaScript.
您必须在函数中设置默认值 - 您不能在 JavaScript 中为参数定义默认值。
function foo( args ){
var arg1 = args[ 0 ] || "default_value";
var arg2 = args[ 1 ] || 0;
///etc...
}
Even better, instead of an array you could pass a simple object which would allow you to access the arguments by their key in the object:
更好的是,您可以传递一个简单的对象而不是数组,该对象允许您通过对象中的键访问参数:
function foo( params ){
var arg1 = params[ "arg1" ] || "default_value";
var arg2 = params[ "arg2" ] || 0;
///etc...
}
回答by George
If you were going to do (let's say it was valid)
如果你打算这样做(假设它是有效的)
f(value1, arg3 = value2)
Then argument 2 would be undefined, so just pass that:
那么参数 2 将是未定义的,所以只需传递:
f(value1, undefined, value2)
回答by Ali
Yes you can. It can be written as:
是的你可以。可以写成:
function f(arg1, undefined, arg3) { // do stuff }
In this call argument 1 & 3 will pass, and argument 2 will not be sent.
在这个调用中,参数 1 和 3 将通过,而参数 2 不会被发送。
回答by xunux
Hey I had a similar problem but i don't know if it would apply in any context, and if it's the same for ES5, but in ES6 and within my context i was able to just pass undefined as the argument i want to skip.
嘿,我有一个类似的问题,但我不知道它是否适用于任何上下文,以及它是否适用于 ES5,但在 ES6 和我的上下文中,我能够将 undefined 作为我想跳过的参数传递。
What i mean by context, is that in my case I am assigning a default value to that argument in the function with ES6 format:
我所说的上下文的意思是,在我的情况下,我在 ES6 格式的函数中为该参数分配一个默认值:
const exampleFunc = (arg1 = "defaultValue",arg2) => {
console.log(arg1,arg2)
}
exampleFunc(undefined,"hello!");
//this would log to the console "defaultValue","hello!"
I'm not sure if this would work if you don't have a default value assigned in the function with ES6 format, but it worked for me! Hope this helped
如果您没有在 ES6 格式的函数中分配默认值,我不确定这是否可行,但它对我有用!希望这有帮助
回答by CicheR
With ECMAScript 6(ECMAScript 2015) and the introduction of Default Parameters, it can be as easy as setting default values to the parameters and passing undefined
to skipa parameter:
与ECMAScript的6(ECMAScript的2015)和引入的默认参数,其可以是容易,因为设置默认值的参数,并传递undefined
到跳过一个参数:
function paramsTest(p1 = "p1 def", p2 = "p2 def", p3 = "p3 def") {
console.log([p1, p2, p3]);
}
paramsTest(); // [ "p1 def", "p2 def", "p3 def"]
paramsTest("p#1", "p#2"); // [ "p#1", "p#2", "p3 def"]
paramsTest("p#1", undefined, null); // [ "p#1", "p2 def", null]
回答by Ben Sewards
Another interesting approach to this would be to use the Function.prototype.call()
method. All we have to do is assign the arguments with default call object values, and then you're free to call the method with only the arguments you want to use:
另一个有趣的方法是使用该Function.prototype.call()
方法。我们所要做的就是使用默认调用对象值分配参数,然后您可以自由地仅使用您想使用的参数调用该方法:
function f(arg1 = this.arg1, arg2 = this.arg2 || false, arg3 = this.arg3) {
console.log([arg1, arg2, arg3]);
}
f.call({ arg1: 'value for arg1', arg3: 'value for arg3'});
f('value for arg1', undefined, 'value for arg3');
Notice that I set arg2 to this.arg2 || false
- this syntax allows a fallback to false instead of undefined when no f.call instance object arg2 property exists.
请注意,我将 arg2 设置为this.arg2 || false
- 当不存在 f.call 实例对象 arg2 属性时,此语法允许回退到 false 而不是 undefined。
回答by Edi G.
Add this code to your function (for default values)
将此代码添加到您的函数中(用于默认值)
function f(a, b, c)
{
a = typeof a !== 'undefined' ? a : 42;
b = typeof b !== 'undefined' ? b : 'default_b';
a = typeof c !== 'undefined' ? c : 43;
}
call the function like this
像这样调用函数
f(arg1 , undefined, arg3)