We use call, bind and apply methods to set the 'this' keyword independent of how the function is called. This is especially useful for the callbacks. Also, functions are a special kind of objects in JavaScript. So they have access to some methods and properties. Call, bind, and apply are some of the methods that every function inherits.
Sometimes the bind() method has to be used to prevent losing this, especially in callback. but actually, in ES6 we use the arrow function instead of bind hack to avoid such unwanted mistakes!✌
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
So, I know how these functions work, but I can't quite figure out when they are useful
when would you want to use call(function, arg) instead of function(arg)?
We use call, bind and apply methods to set the 'this' keyword independent of how the function is called. This is especially useful for the callbacks. Also, functions are a special kind of objects in JavaScript. So they have access to some methods and properties. Call, bind, and apply are some of the methods that every function inherits.
ah, got it
really hadn't thought about callbacks, but that makes some sense
Sometimes the
bind()
method has to be used to prevent losingthis
, especially in callback. but actually, in ES6 we use the arrow function instead of bind hack to avoid such unwanted mistakes!✌