Before explaining the implicit parameters, it would be appropriate to discuss a little about JavaScript function
's parameters and arguments. Most of the time, we use these two terms interchangeably. But they've a subtle difference:
- a parameter is the variable that is listed as part of the
function
definition - an argument is the value that is passed against the parameter defined in the
function
when the function
is invoked
When the function
is invoked, the arguments are assigned to the parameters of the function
in the specified order. Meaning, the first argument is assigned to the first parameter and second argument is assigned to the second parameter and so on. In JavaScript, if the list of arguments is greater or lesser than the number of parameters, it doesn't complain about it, which leads us to the new ES6 feature rest parameter.
Rest Parameter
If for some reason, you're not sure how many arguments you will end up passing with invoking a function
, you can always use rest parameter with ellipsis (...) prefixed to the parameter name. The parameters passed as rest parameter are available in the function in the form of array. For example:
function myFunc(firstParam, ...remainingParams) {
console.log(firstParam);
for(var i = 0; i < remainingParams.length; i++) {
console.log(remainingParams[i]);
}
}
myFunc('A','B','V','F','T','H');
The above function will log the first parameter to the console followed by the number of parameters passed as rest parameter that are available in remainingParams
array to be logged into the console.
Now let's move towards the implicit parameters of JavaScript function
s.
Whenever a function
is invoked, besides the list of obvious parameters, a couple of hidden parameters are also passed into that function
, i.e., argument
, and this
.
Implicit Parameter - argument
The argument
parameter is a collection of all the arguments passed to a function. The argument
parameter is useful in case no parameter is defined or a matching parameter is not defined, then this could be used to access the values passed to the function. To identify the number of arguments passed, we could use the arguments
length property as shown below:
function myFunc() {
if(arguments.length > 0) {
for(var i = 0; i < arguments.length; i++)
console.log(arguments[i]);
}
}
myFunc(2,3,4,6,1,7,3);
Implicit Parameter - this
When a function is invoked, with the explicitly defined parameters that represents the argument, an implicit parameter this
is also passed. The this
parameter refers to the object that invoked the function. This is why this
parameter is also known as function context.
function myFunc() {
return this;
}
myFunc();
The above function when called in global context would return the window
object. If we try to execute this in strict
mode, then it would return undefined
, like:
function myFunc() {
'use strict'
return this;
}
myFunc();
The this
parameter is a tricky fellow. Unlike other high level object oriented languages where this
always points to the object of the class, this
parameter is highly influenced by the method of invocation.
This brings us to the different ways of invoking a function
which will be explained later in the next article.