第三章 Functions are fundamental
3.3 Invocations
The manner in which a function is invoked has a huge impact on how the code within it operates, primarily in how the
this parameter is established.
There are four ways to invoke a function:
- As a function, in which the function is invoked in a straightforward manner.
- As a method, which ties the invocation to an object, enabling OOP.
- As a constructor, in which a new object is brought into being.
- Via its apply() or call() methods.
3.3.1 From arguments to function parameters
When a list of arguments is supplied as part of a function invocation, these arguments are assigned to the parameters specified in the function declaration in the same order that each was specified.
If there is a different number of arguments than parameters:
- If more arguments are supplied than parameters, the "excess" arguments are simply assigned to parameter names. But we still have a way to get at them.
- If there are more parameters than arguments, the parameters that have no corresponding argument are set to undefined.
THE ARGUMENTS PARAMETER
The
arguments parameter is a collection of all of the arguments passed to the function. It has a
length property and the individual argument values can be obtained using array indexing notation such as arguments[2]. But arguments IS NOT a JavaScript array.
THE "THIS" PARAMETER
Whenever a function is invoked, an implicit parameter named
this is passed tho the function. What the this parameter points to isn't defined by how the function is declared, but by how it's invoked.
3.3.2 Invocation as a function
function ninjia(){};
ninjia();
function samurai = function() {};
samurai();
When invoked in this manner, the function context is the global context - the window object.
3.3.3 Invocation as a method
When a function is assigned to a property of an object and the invocation occurs by referencing the function using that property, then the function is invoked as a method of that object.
var o = {};
o.whatever = function(){};
o.whatever();
The object becomes the function context and is available within the function via the this parameter.
3.3.4 Invocation as a constructor
function creep() { return this; }
new creep();
THE SUPERPOWERS OF CONSTRUCTORS
When a constructor is invoked, the following actions take place:
- A new empty object is created
- This object is passed to the constructor as the this parameter, and thus becomes the constructor's function context
- In the absence of any explicit return value, the new object is returned as the constructor's value
3.3.5 Invocation with the apply() and call() methods
apply() and call() allow us to invoke a function and to explicitly specify any object we want as the function context.
function juggle() {
var result = 0;
for (var n = 0; n < arguments.length; n++) {
result += arguments[n];
}
this.result = result;
}
var ninja = {};
juggle.apply(ninja, [1,2,3,4]);
juggle.call(ninja, 5, 6, 7, 8);
FORCING THE FUNCTION CONTEXT IN CALLBACKS
function forEach(list, callback) {
for (var n = 0; n < list.length; n++) {
callback.call(list[n], n);
}
}
var weapons = ['shuriken', 'katana', 'nunchucks'];
forEach(weapons, function(index) {
assert(this == weapons[index]);
}