Home  >  Article  >  Web Front-end  >  call() , apply() and bind() Method In JavaScript

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

Patricia Arquette
Patricia ArquetteOriginal
2024-10-26 08:14:30503browse

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

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

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

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

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

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

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.

The above is the detailed content of call() , apply() and bind() Method In JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn