JavaScript Function Invocation
JavaScript functions have 4 invocation methods.
The difference between each method lies inthisthe initialization of
thisKeywords
Generally speaking, in JavaScript, this points to the current object when the function is executed.
![]() |
Notethisis a reserved keyword, you cannot modifythisthe value of it. |
|---|
Invoking JavaScript Functions
In the previous chapters, we have learned how to create functions.
The code inside a function is executed after the function is invoked.
Invoking as a Function
Example
Try it Yourself »
The above function does not belong to any object. But in JavaScript, it is always the default global object.
In HTML, the default global object is the HTML page itself, so the function belongs to the HTML page.
The page object in a browser is the browser window (the window object). The above function automatically becomes a function of the window object.
myFunction() and window.myFunction() are the same:
Example
Try it Yourself »
![]() |
This is a common method for invoking JavaScript functions, but it is not a good programming practice. Global variables, methods, or functions can easily cause naming conflict bugs. |
|---|
Global Object
When a function is not invoked by its own objectthisthe value of this becomes the global object.
In a web browser, the global object is the browser window (the window object).
This example returnsthisthe value of this is the window object:
Example
Try it Yourself »
![]() |
Invoking a function as a global object makesthisthe value of this the global object. Using the window object as a variable can easily cause the program to crash. |
|---|
Invoking a Function as a Method
In JavaScript, you can define a function as a method of an object.
The following example creates an object (myObject), the object has two properties (firstNameandlastName), and a method (fullName):
Example
Try it Yourself »
fullNameA method is a function. A function belongs to an object.myObjectis the owner of the function.
thisAn object, which owns JavaScript code. In the example,thisthe value of this ismyObjectthe object.
Test it! ModifyfullNamethe method and returnthisthe value:
Example
Try it Yourself »
![]() |
Invoking a function as an object method makesthisthe value of this the object itself. |
|---|
Invoking a Function with a Constructor
If the function call is preceded by thenewkeyword, then it is invoking a constructor.
This looks like creating a new function, but in fact JavaScript functions are recreated objects:
Example
Try it Yourself »
A constructor invocation creates a new object. The new object inherits the properties and methods of the constructor.
![]() |
In the constructor,thisthe keyword has no value. thisThe value of this is created when the function call instantiates the object (new object). |
|---|
Invoking a Function as a Function Method
In JavaScript, functions are objects. JavaScript functions have their own properties and methods.
call()andapply()are predefined function methods.
Example
Try it Yourself »
Example
Try it Yourself »
Both methods use the object itself as the first parameter. The difference between the two lies in the second parameter: apply takes an array of arguments, that is, multiple arguments are combined into an array and passed in, while call passes them as arguments of call (starting from the second parameter).
Under JavaScript strict mode, when calling a function, the first parameter becomesthisthe value of this, even if the parameter is not an object.
Under JavaScript non-strict mode, if the first parameter is null or undefined, it will use the global object instead.
![]() |
Through the call() or apply() methods, you can setthisthe value of this, and call it as a new method of an existing object. |
|---|
