call() , apply() , & bind() functions ??

call() , apply() , & bind() functions ??

In JavaScript, functions are first-class objects, and they come with built-in methods that allow us to control how and where they are executed. Three such methods are call, apply, and bind. These methods let us set the value of this inside a function and can be very handy in various scenarios.

?? They are useful for borrowing methods from other objects and writing less code.


call()??

The call method lets you call a function with a specific this value and arguments one by one.

? Syntax:? functionName.call(thisValue, arg1, arg2, ...);

? Explanation

  • thisValue: The object you want this to refer to inside the function.
  • arg1, arg2, ...: The arguments you want to pass to the function.


apply()??

The apply method is similar to call, but it takes the arguments as an array.

? Syntax : functionName.apply(thisValue, [argsArray]);

? Explanation

  • thisValue: The object you want this to refer to inside the function.
  • [argsArray]: An array of arguments you want to pass to the function.

?? Important: The only difference between apply and call is that apply takes an array of arguments. Both call and apply invoke the function immediately.

bind()??

The bind method creates a new function that, when called, has its this value set to the specified value, and can have some arguments preset.

? Syntax : const boundFunction = functionName.bind(thisValue, arg1, arg2, ...);

? Explanation

  • thisValue: The object you want this to refer to inside the new function.
  • arg1, arg2, ...: The arguments you want to preset for the new function.

?? Major Difference: Unlike call and apply, which invoke the function immediately, bind returns a new function that you can call later.



Conclusion ??

?? call lets you call a function with a specific this value and arguments provided one by one.

?? apply is similar but takes the arguments as an array.

?? bind creates a new function with a specific this value and optionally preset arguments.

Differences:

?? Invocation: call and apply invoke the function immediately, while bind returns a new function for later invocation.

?? Arguments: call takes arguments individually, whereas apply takes arguments as an array.

?? Usage: bind is useful when you need a function to be called later with a specific this value and preset arguments, while call and apply are used to invoke functions immediately with a specific this value.

?? These methods help you control the context (this value) of your functions, making your code more flexible and reusable.


Meanwhile what you all can do is, Like and Share this edition among your peers and also subscribe to this Newsletter so that you all can get notified when I come up with more content in future. Share this Newsletter with anyone who might be benefitted from this content.

Until next time, Dive Deep and Keep Learning! ??


Rahul Goyal

Engineering @Probo | Ex-SWE @GFG | Tech Enthusiast | Node | React | Next

8 个月

Insightful!

Om J.

Creating HYPE | Digital Marketing and Management | Python

8 个月

Interesting!

要查看或添加评论,请登录

Siddharth Sharma的更多文章

社区洞察

其他会员也浏览了