Categories
JavaScript

call(), apply() and bind()

enter image description here

https://stackoverflow.com/a/54562241

  • call and apply call a function while bind creates a function.
  • Use .bind() when you want that function to later be called with a certain context, useful in events.
  • Use .call() or .apply() when you want to invoke the function immediately, and modify the context.

call() & apply()

// "A for array and C for comma."

theFunction.apply(valueForThis, arrayOfArgs)

theFunction.call(valueForThis, arg1, arg2, ...)

Leave a comment