Posts

Showing posts with the label call

What does call, apply and bind functions do in JavaScript?

In this article, we will learn about what call , apply and bind functions are in JavaScript and how they are different from each other. These functions are available on all JavaScript functions as they are defined on the prototype of Function class. call – Function.prototype.call The call method allows you to call a function with a given context ( this ) and arguments. This gives you an ability to call a function with different objects. Syntax someFunc.call(thisArg, [arg1, arg2, arg3, …]); thisArg is optional. If not provided then global object will be used i.e. in case of browser the window object. In strict mode, if thisArg is not provided then this inside someFunc will be undefined. arg1, arg2, arg3, … are optional. Examples Calling a function with different objects. function   sayHello ( hostName ) {      console . log ( `Hello  ${this . firstName }   ${this . lastName }  from  ${ hostName  ?  hostName  :  'TheAconiteDev' } ` ); }   let   person1  = {