JavaScript Function Call


There are 4 ways to call JavaScript functions.

The difference between each method is the initialization of this .


this keyword

Generally speaking, in Javascript, this points to the current object when the function is executed.

NoteNote: this is a reserved keyword, and you cannot modify the value of this .

Call JavaScript function

In the previous chapters we have learned how to create functions.

The code in the function is executed after the function is called.


Call as a function

Example

function myFunction(a, b) {
     return a * b;
}
myFunction(10, 2); // myFunction(10, 2) returns 20

Try It! 

The above function does not belong to any object. But in JavaScript it is always the default global object.

The default global object in HTML is the HTML page itself, so the function belongs to the HTML page.

The page object in the browser is the browser window (window object). The above functions will automatically become functions of the window object.

myFunction() and window.myFunction() are the same:

Example

function myFunction(a, b) {
     return a * b;
}
window.myFunction(10, 2); // window.myFunction(10, 2) returns 20

 Try It! 

NoteThis is a commonly used method invokes a JavaScript function, but it is not good programming practice
global variables, methods or functions easily lead to bug naming conflicts.

Global object

When the function is not called itself the object this value will become the global object.

The global object in a web browser is the browser window (window object).

This instance returns the value of this as the window object:

Example

function myFunction() {
     return this;
}
myFunction(); // return window object

Try It! 

NoteCalling a function as a global object will make the value of this a global object.
Using the window object as a variable can easily cause the program to crash.

Function as a method call

In JavaScript you can define functions as methods of objects.

The following example creates an object (myObject), the object has two properties (firstName and lastName), and a method (fullName):

Example

var myObject = {
     firstName:"John",
     lastName: "Doe",
     fullName: function () {
         return this.firstName + "" + this.lastName;
     }
}
myObject.fullName(); // returns "John Doe"

 Try It! 

The fullName method is a function. Functions belong to objects. myObject is the owner of the function.

this object has JavaScript code. The value of this in the example is myObject .

Test the following! Modify the fullName method and return this value:

Example

var myObject = {
     firstName:"John",
     lastName: "Doe",
     fullName: function () {
         return this;
     }
}
myObject.fullName(); // return [object Object] (owner object)

 Try It! 

NoteCalling a function as an object method will make the value of this the object itself.

Use the constructor to call a function

If the new keyword is used before the function call , the constructor is called.

This looks like a new function is created, but in fact the JavaScript function is a recreated object:

Example

// Constructor:
function myFunction(arg1, arg2) {
     this.firstName = arg1;
     this.lastName = arg2;
}
 
// This creates a new object
var x = new myFunction("John","Doe");
x.firstName; // returns "John"

Try It! 

The call to the constructor creates a new object. The new object inherits the properties and methods of the constructor.

NoteThe this keyword in the constructor does not have any value. The value of
this is created when the function call instantiates the object (new object).

Call a function as a function method

In JavaScript, functions are objects. JavaScript functions have their properties and methods.

call() and apply() are predefined function methods. Two methods can be used to call functions, and the first parameter of the two methods must be the object itself.

Example

function myFunction(a, b) {
     return a * b;
}
myObject = myFunction.call(myObject, 10, 2); // returns 20

 Try It! 


Example

function myFunction(a, b) {
     return a * b;
}
myArray = [10, 2];
myObject = myFunction.apply(myObject, myArray); // returns 20

Try It! 

Both methods use the object itself as the first parameter. The difference between the two is the second parameter: apply passes in an array of parameters, which means that multiple parameters are combined into an array and passed in, while call is passed in as a parameter of call (starting from the second parameter).

In JavaScript strict mode (strict mode), the first parameter will become the value of this when calling a function , even if the parameter is not an object.

In JavaScript non-strict mode (non-strict mode), if the value of the first parameter is null or undefined, it will use the global object instead.

NoteThrough the call() or apply() method, you can set the value of this and call it as a new method of an existing object.