JavaScript – Invocation Expressions

An invocation expression is JavaScript’s
syntax for calling (or executing) a function or method. It starts with
a function expression that identifies the function to be called. The
function expression is followed by an open parenthesis, a
comma-separated list of zero or more argument expressions, and a close
parenthesis. Some examples:

f(0)            // f is the function expression; 0 is the argument expression.
Math.max(x,y,z) // Math.max is the function; x, y and z are the arguments.
a.sort()        // a.sort is the function; there are no arguments.

When an invocation expression is evaluated, the function
expression is evaluated first, and then the argument expressions are
evaluated to produce a list of argument values. If the value of the
function expression is not a callable object, a TypeError is thrown.
(All functions are callable. Host objects may also be callable even if
they are not functions. This distinction is explored in Callable Objects.) Next, the argument values are assigned,
in order, to the parameter names specified when the function was
defined, and then the body of the function is executed. If the
function uses a return statement to
return a value, then that value becomes the value of the invocation
expression. Otherwise, the value of the invocation expression is
undefined. Complete details on
function invocation, including an explanation of what happens when the
number of argument expressions does not match the number of parameters
in the function definition, are in Chapter 8 .

Every invocation expression includes a pair of parentheses and
an expression before the open parenthesis. If that expression is a
property access expression, then the invocation is known as a
method invocation. In method invocations, the
object or array that is the subject of the property access becomes the
value of the this parameter while
the body of the function is being executed. This enables an
object-oriented programming paradigm in which functions (known by
their OO name, “methods”) operate on the object of which they are
part. See Chapter 9 for details.

Invocation expressions that are not method invocations normally
use the global object as the value of the this keyword. In ECMAScript 5, however,
functions that are defined in strict mode are invoked with undefined as their this value rather than the global object.
See “use strict” for more on strict mode.

Comments are closed.