DEV Community

Cover image for call() , apply() and bind() Method In JavaScript
Md Pervez Hossain
Md Pervez Hossain

Posted on

1

call() , apply() and bind() Method In JavaScript

What is the call() Method in js ?

call() method is a built-in javascript method that allows us to invoke a function with specified β€œthis” context.

Key Features πŸ‘

  • First parameter Sets β€œthis” context value for the function.
  • Make a function Reusable.
  • Apply the same function to different objects.
  • Others parameter passed with comma seperated
  • It calls the function directly

Image description

What is the apply() Method in js ?

apply() method is a built-in javascript method that allows us to invoke a function with specified β€œthis” context.

Key Features πŸ‘

  • First parameter Sets β€œthis” context value for the function.
  • Make a function Reusable.
  • Apply the same function to different objects.
  • Other parameters are passed as an array:
  • It calls the function Immediately

Image description

What is the bind() Method in js ?

The bind() method is a built-in JavaScript method that creates a copy of the original function and returns a new function with a specified this context, rather than invoking the original function immediately.

Key Features πŸ‘

  • First parameter Sets β€œthis” context value for the function.
  • Make a function Reusable.
  • Apply the same function to different objects.
  • Others parameter passed with comma seperated
  • creates a copy of the original function and returns a new function
  • Need to call the returned function to access the original functionality

Image description

Key Differences between call() , apply() and bind() Methods :

Execution Timing:

  • call() and apply() invoke the function immediately.
  • bind() returns a new function without invoking it, allowing deferred execution.

Arguments Handling:

  • call() accepts arguments as comma-separated values.
  • apply() requires arguments as an array.
  • bind() also uses comma-separated arguments, but these are pre-set for later when the function is invoked.

Return Value:

  • call() and apply() return the result of the function call.
  • bind() returns a new function with the specified this context.

Write down in the comment box your knowledge about the call(), apply(), and bind() methods.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)