[英]Is there a JavaScript equivalent for C# 'params'?
I need a method that can have an arbitrary number of parameters. 我需要一个可以有任意数量参数的方法。 In C# we have the params
statement. 在C#中我们有params
语句。 Do we have anything similar in JavaScript? 我们在JavaScript中有类似的东西吗?
There is the arguments
collection, which contains all arguments passed to the function. 有一个arguments
集合,它包含传递给函数的所有参数。
There is a) no need to specify "optional" arguments in the function signature and b) any function accepts any number of parameters. 有a)无需在函数签名中指定“可选”参数,b)任何函数都接受任意数量的参数。
function foo() {
console.log(arguments);
}
foo(1,2,3,4); // logs [1, 2, 3, 4]
Likewise, there is no need to supply "required" arguments in a function call: 同样,不需要在函数调用中提供“required”参数:
function foo(a, b, c, d) {
console.log(arguments);
}
foo(1,2); // logs [1, 2]
Any argument named in the signature but not supplied in the function call will be undefined
. 签名中指定但在函数调用中未提供的任何参数都将是undefined
。
Note that arguments
behaves like an Array, but technically it isn't one. 请注意, arguments
行为类似于数组,但从技术上讲,它不是一个。 For example, you can call arguments[0]
, but you can't call arguments.slice()
. 例如,您可以调用arguments[0]
,但不能调用arguments.slice()
。 What you can do to get around this is using the Array prototype: 你可以做些什么来解决这个问题是使用Array原型:
Array.prototype.slice.call(arguments, 1, 2);
The so-called rest parameter ...
is a new (ES6+) addition to the language and makes working with variadic functions more comfortable. 所谓的rest参数 ...
是语言的新增功能(ES6 +),使得可变功能的使用更加舒适。 @ArunCM's answer explains it. @ ArunCM的答案解释了它。
I know this thread is too old but I believe something is missing here. 我知道这个帖子太旧了但我相信这里缺少一些东西。
There is Rest parameter ( introduced in ECMAScript 6 ) which will allow us to represent an indefinite number of arguments as an array . Rest参数 ( 在ECMAScript 6中引入 )允许我们将无限数量的参数表示为数组 。
It always returns an array . 它总是返回一个数组 。 Which means even in defensive JavaScript land, it's ok to do things like check .length of rest without guards. 这意味着,即使在防守JavaScript的土地,它的确定做这样的事情的休息检查。长度无人看守。
Syntax : 句法 :
function(a, b, ...theArgs) {
// ...
}
There are three main differences between rest parameters and the arguments object: rest参数和arguments对象之间有三个主要区别:
Additional reading : Spread 补充阅读: 传播
function f(x, ...y) { // y is an Array return x * y.length; } console.log("Expected result : 3*2 = 6 & Actual result : " + f(3, "hello", true)); console.log("Expected result : 3*4 = 12 & Actual result : " + f(3, "a", true, "b", 1)); //here we are not passing anything to "y" but its still safe to check .length of "y" because it always return an array. console.log("Expected result : 3*0 = 0 & Actual result : " + f(3));
Yes. 是。 arguments
. arguments
。
function concatStrings () {
var str = '';
for (var i = 0; i < arguments.length; i++) {
str += arguments[i];
}
return str;
}
Be aware that arguments
isn't an array, so it doesn't have methods like join
or push
. 请注意, arguments
不是数组,因此它没有join
或push
等方法。 It's just an array-like object (with numerical properties and a length
property) so it can be iterated through. 它只是一个类似数组的对象(具有数字属性和length
属性),因此可以迭代。
It is some sort of implicit in the special variable "arguments". 它在特殊变量“arguments”中是某种隐含的。 Use like this: 使用这样:
function something(arg1, arg2) {
for (var i = 0; i < arguments.length; i++) {
var x = arguments[i];
}
}
Then you can call it like something(1, 2, 3, 'a', 'b', 'c')
然后你就可以把它称为something(1, 2, 3, 'a', 'b', 'c')
More examples here: http://www.jtricks.com/javascript_tutorials/varargs.html 更多示例: http : //www.jtricks.com/javascript_tutorials/varargs.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.