简体   繁体   中英

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. Do we have anything similar in JavaScript?

There is the arguments collection, which contains all arguments passed to the function.

There is a) no need to specify "optional" arguments in the function signature and b) any function accepts any number of parameters.

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:

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 .

Note that arguments behaves like an Array, but technically it isn't one. For example, you can call arguments[0] , but you can't call arguments.slice() . What you can do to get around this is using the Array prototype:

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. @ArunCM's answer explains it.

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 .

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.

Syntax :

function(a, b, ...theArgs) {
// ...
}

There are three main differences between rest parameters and the arguments object:

  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
  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;
  3. the arguments object has additional functionality specific to itself (like the callee property).

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 .

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 . It's just an array-like object (with numerical properties and a length property) so it can be iterated through.

JavaScript has arguments object inside functions. It contains of all params passed to the function.

More info

It is some sort of implicit in the special variable "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')

More examples here: http://www.jtricks.com/javascript_tutorials/varargs.html

Javascript functions can accept any number of parameters by default. You can see them with the arguments variable.

See here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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