Call function in Javascript

The call() function is a built-in function in JavaScript that allows you to call a function with a specified “this” value. This tutorial lets us learn more about JavaScript’s call() function. 

“this” in Javascript

“this” in JavaScript is an object. Every JavaScript function gets a copy of this (except arrow functions). Its value does not depend on where we declare it but on how we invoke it. You can read more about from the article this in javascript.

The call function is one of the ways we set the “this” of a function. 

Syntax

The syntax of the call function is as follows

Here, func is the function that you want to call.

thisArg is the object you want to use as this value in func.

arg1arg2, etc., are the arguments you want to pass to the function func.

The call function returns whatever is returned by the function func.

Call function Examples

The following is an example of using the call() function to call a function.

The code sayHello.call(person); sets the person as the this inside the sayHello function and invokes it. 

If we invoke sayHello() directly (without call), then the “this” inside the sayHello is bound to the global object in “not strict” mode. But if we use the “strict” mode, “this” is always null.

The following example shows how you can pass parameters using the call function.

The sayHello function below accepts a parameter, which we pass as the second argument to the call function.

The add function accepts two arguments. The code shows how to invoke the add function using the call function.

Borrowing methods from another object

We can use the call function to invoke methods from other objects.

The john object in the following code contains the method greet, and the mike object does not.

We can take the greet method from the john object, set its this to mike, and invoke it (john.greet.call(mike)).

Chaining Constructors

Constructor chaining is a technique where a constructor function calls another constructor function. Using this technique, we can create an inheritance relationship between two or more constructor functions, where one constructor function “inherits” properties and methods from another.

For example, think of Employee & Customer objects. Both of these objects have common fields like name & address.

What we do is create a Person constructor function with the common fields. And in the Employee & Customer constructor functions, invoke the Person constructor, passing “this” value.

Array-like objects

Array-like objects are objects that look like arrays but are not arrays. For example, the numbers object below is an object with numerical indexes as property names and has a length property.

We cannot run the array method on them directly. But use the call function to invoke their array methods like sort, filer, split, etc on array like objects. 

The code below sorts the numbers object using the Array sort method.

Summary

  1. The call() function invokes any function with a specified “this” value. 
  2. The value of “this” is passed as the first argument of the call function.
  3. We pass the function’s arguments as subsequent arguments to the call function.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top