Taking a deeper look into functions. We will focus on invoking functions. The JavaScript code within the curly braces of a function is the body. The body of a function is not executed at the time of defining a function rather it is executed at the time of being invoked. Let's take a look at the ways a function can be invoked:
- as functions
- as methods
- as constructors
- indirectly through their call() and apply() methods
Invoke a function as a function:
When invoked by an invocation expression, a function is invoked as functions or as methods (described in a moment). An invocation expression is a function expression that evaluates to a function object followed by an open parenthesis, a comma-separated list of zero or more argument expressions, and a close parenthesis.
var total = distance(0,0,2,1) + distance(2,1,3,5);
In an invocation, each argument expression is evaluated, and the resulting values become the arguments to the function. These values are assigned to the parameters named in the function definition. In the body of the function, a reference to a parameter evaluates to the corresponding argument value.
With a method invocation, a method is a property of an object that evaluates to a function object.
person = {
firstName: “John”,
lastName: “Doe”, fullName: function _get
FullName () { this.result = this.firstName + “ “ +
this.lastName;
}
};
The above example shows a person object with a fullName method as a property of person. Our fullName() property returns the firstName and lastName concatenated from our person object.
With the method fullName() defined to the object person, it can be invoked like this:
person.fullName(); // a method invocation to concatenate firstName and lastName of a person
object
person.result // => John Doe
Methods and this keyword are imperative to object-oriented programming. Any function that is used as a method is effectively passed an implicit argument: the object through which it is invoked. Ideally, a method performs an operation on that object, and the method-invocation syntax is an elegant way of expressing such.
Invoke a function as a constructor:
If a function or method invocation is preceded by the keyword new, then it is a constructor invocation. Constructor invocations are different than regular function and method invocations, specifically their handling of arguments, invocation context, and return value.
var o = new Object();
A constructor invocation creates a new, empty object that inherits from the prototype property of the constructor. Constructor functions are intended to initialize objects and this newly created object is used as the invocation context, so the constructor function can refer to it with this keyword.
Indirect Invocation:
Like all JavaScript objects, functions have methods. Namely, but not limited to, call() and apply(). Both of which invoke a function indirectly. Both methods can invoke any function as a method of any object, even if it is not actually a method of that object. Both methods also allow you to specify the arguments for the invocation. The call() method uses its own argument list as arguments and the apply() method expects an array of values to be used as arguments. For more info on this subject, please see Part One.