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 ?
JavaScript
x
11
11
1
var obj = {
2
x: 81,
3
getX: function() {
4
return this.x;
5
}
6
};
7
8
alert(obj.getX.bind(obj)());
9
alert(obj.getX.call(obj));
10
alert(obj.getX.apply(obj));
11
Advertisement
Answer
I created this comparison between function objects, function calls, call/apply
and bind
a while ago:
.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.