I'm writing a library that exposes the following function
function call(instance, func, arguments) {
return {
call: {
instance: instance,
func: func,
arguments: arguments
}
}
}
Is the name arguments
ok to use? It's really the best name for it, but I don't want to clash with the builtin variable. It works in Node. Will this work in all browsers and JavaScript environments?
Edit:
Note that the above function does work as expected in Node. What I'm wondering is whether this will work everywhere.
I'd rather not rename it unless there's a technical reason to. I'd really like this external-facing function and the docs to reflect this parameter name, because the returned object is going to be serialized as JSON and used across languages.
Named arguments enable you to specify an argument for a parameter by matching the argument with its name rather than with its position in the parameter list. Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates.
The only thing that matters is the order in which the arguments are passed. This familiar approach is called positional arguments. It is usually fine for cases where you pass one or two arguments since its hard to mess up the order of arguments.
They are used to define a function and are also called formal parameters and formal arguments. In the following example, parameter1 and parameter2 are parameters of the 'exampleFunction' function. In this context, Optional Parameters are those parameters that need not always be passed i.e. they're optional.
Passing a function as an argument to the function is quite similar to the passing variable as an argument to the function. so variables can be returned from a function. The below examples describe passing a function as a parameter to another function.
It is okay to use the name arguments
in "sloppy mode", but not recommended.
It is forbidden in strict mode, which all new code should use if the author cares about code quality.
function a(arguments) {
console.log(arguments);
}
a(1);
// Prints "1"
(function () {
'use strict';
function a(arguments) {
console.log(arguments);
}
a(1);
}());
// Uncaught SyntaxError: Unexpected eval or arguments in strict mode
This is akin to naming a variable "Object". Maybe in some obscure context it makes sense to use that name, but by doing so you lose access to the global Object
object and useful methods on it like Object.keys
. Similarly, by making this poor naming decision, you can no longer manipulate the arguments
object, which is varargs in JS.
In the interest of improving the maintainability of your code, it is best to avoid creating ticking time bombs like this one. There is a good reason why it is not allowed in strict mode: It is likely to cause misery for anyone who wants to use arguments
as it is typically used.
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