The call() method calls a function with a given
this
value and arguments provided individually.
The apply() method calls a function with a given
this
value, and arguments
provided as
an array
The bind() method creates a new function that, when called, has
its
this
keyword set
to the provided value, with a given sequence of arguments preceding any
provided when the new function is called.
While the syntax of this function is almost
identical to that of
apply()
, the fundamental difference is that call()
accepts an argument list, while apply()
accepts a single array of arguments.
The difference is that
apply
lets you invoke the function with arguments
as an array; call
requires the
parameters be listed explicitly. A useful mnemonic is "A for array and C for comma."
Pseudo syntax:
theFunction.apply(valueForThis, arrayOfArgs)
theFunction.call(valueForThis, arg1, arg2, ...)
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
call the function immediately, whereas
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
JavaScript Practical Applications of Call,Apply and Bind functions
https://www.youtube.com/watch?v=c0mLRpw-9rI
bind
returns
a function that, when later executed, will have the correct context set for
calling the original function. This way you can maintain context in async
callbacks and events.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
JavaScript Practical Applications of Call,Apply and Bind functions
https://www.youtube.com/watch?v=c0mLRpw-9rI