Using function.call to Call a Function

In JavaScript, there is a way to call a function by using the .call() method. The syntax of the .call() is as follows:

functionName.call(thisArg, arg1, arg2, ….) // arg1 and arg2 are optional


functionName: Refers to the function being called and. thisArg specifies the object that this references inside the invoked function.

Description

Note: This function is almost identical to apply(), except that the function arguments are passed to call() individually as a list, while for apply() they are combined in one object, typically an array - for example, func.call(this, "eat", "bananas") vs. func.apply(this, ["eat", "bananas"]).

Normally, when calling a function, the value of this inside the function is the object that the function was accessed on. With call(), you can assign an arbitrary value as this when calling an existing function, without first attaching the function to the object as a property. This allows you to use methods of one object as generic utility functions.

Warning: Do not use call() to chain constructors (for example, to implement inheritance). This invokes the constructor function as a plain function, which means new.target is undefined, and classes throw an error because they can't be called without new. Use Reflect.construct() or extends instead.