Is the following function legal and portable?
function(_, _, x){
return x;
}
Sometimes I want to write a callback that doesn't use the leftmost parameters so I wonder what is the most concise way to do so.
Conclusion:
function(_1, _2, x)
is probably as short as it gets then.
Parameter RulesJavaScript function definitions do not specify data types for parameters. JavaScript functions do not perform type checking on the passed arguments. JavaScript functions do not check the number of arguments received.
Functions can accept more than one argument. When calling a function, you're able to pass multiple arguments to the function; each argument gets stored in a separate parameter and used as a discrete variable within the function.
In JavaScript, you can reassign values to variables you declared with let or var .
You can call a Javascript function with any number of parameters, regardless of the function's definition. Any named parameters that weren't passed will be undefined.
It is valid in non-strict mode code, but invalid in strict mode code:
It is a SyntaxError if any Identifier value occurs more than once within a FormalParameterList of a strict mode FunctionDeclaration or FunctionExpression.
Source: http://es5.github.com/#x13.1
Therefore, you may want to avoid this, since at one point in the future you will want to move on to strict mode...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With