简体   繁体   English

C#'params'是否有等效的JavaScript?

[英]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对象之间有三个主要区别:

  1. rest parameters are only the ones that haven't been given a separate name, while the arguments object contains all arguments passed to the function rest参数只是那些没有给出单独名称的参数,而arguments对象包含传递给函数的所有参数
  2. the arguments object is not a real array, while rest parameters are Array instances, meaning methods like sort, map, forEach or pop can be applied on it directly; arguments对象不是真正的数组,而rest参数是Array实例,这意味着sort,map,forEach或pop等方法可以直接应用于它;
  3. the arguments object has additional functionality specific to itself (like the callee property). arguments对象具有特定于其自身的附加功能(如callee属性)。

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不是数组,因此它没有joinpush等方法。 It's just an array-like object (with numerical properties and a length property) so it can be iterated through. 它只是一个类似数组的对象(具有数字属性和length属性),因此可以迭代。

JavaScript has arguments object inside functions. JavaScript在函数内部有arguments对象。 It contains of all params passed to the function. 它包含传递给函数的所有参数。

More info 更多信息

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

Javascript functions can accept any number of parameters by default. 默认情况下,Javascript函数可以接受任意数量的参数。 You can see them with the arguments variable. 您可以使用arguments变量查看它们。

See here . 看到这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM