Reputation: 2180
I am quite new to javascript. When going through the javascript codes, I have found ::
function Method1(sender, args) { ... }
function Method2(source, arguments) { ... }
When to use (sender, args) and (source, arguments)? What do they mean?
Upvotes: 0
Views: 203
Reputation: 53370
arguments
is actually not a JS reserved word, otherwise your Method2
would not have worked and would have thrown a Syntax Error.
When arguments
is used in the parameters list of a function, it is simply a regular function parameter / argument, exactly like your sender
, source
and args
.
It is true now that Arguments
cannot be used as a class name. It is used internally by JavaScript to create an arguments
object (that is the one referred to by Tushar in the comments).
When control enters an execution context for function code, an arguments object is created unless (as specified in 10.5) the identifier arguments occurs as an Identifier in the function’s FormalParameterList or occurs as the Identifier of a VariableDeclaration or FunctionDeclaration contained in the function code.
In every function, you can access all the parameters passed during that function call using this arguments
array-like object. It is very useful for functions that may accept a non-predetermined (and possibly unlimited) number of parameters.
So what happens with your Method2
is that it uses the same identifier "arguments" which now is assigned just a single parameter. As if it had shadowed the built-in arguments
object.
Whereas if it had not been used in the parameters list, arguments
within the function block would have been automatically assigned the list of all parameters.
Demo: http://jsfiddle.net/5cexbrff/ (see result in the console)
Upvotes: 1