Skip to content
Advertisement

Javascript call() & apply() vs bind()?

I already know that apply and call are similar functions which set this (context of a function).

The difference is with the way we send the arguments (manual vs array)

Question:

But when should I use the bind() method ?

var obj = {
  x: 81,
  getX: function() {
    return this.x;
  }
};

alert(obj.getX.bind(obj)());
alert(obj.getX.call(obj));
alert(obj.getX.apply(obj));

jsbin

Advertisement

Answer

I created this comparison between function objects, function calls, call/apply and bind a while ago:

enter image description here

.bind allows you to set the this value now while allowing you to execute the function in the future, because it returns a new function object.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement