It seems to me that, in ES6, the following two functions are very nearly identical:
function () {
return this;
}.bind(this);
() => {
return this;
};
The end result seems the same: arrow functions produce a JavaScript function object with their this
context bound to the same value as the this
where they are created.
Obviously, in the general sense, Function.prototype.bind
is more flexible than arrow functions: it can bind to values other than the local this
, and it can bind any function's this
at any point in time, potentially long after it is initially created. However, I'm not asking how bind
itself is different from arrow functions, I'm asking how arrow functions differ from immediately calling bind
with this
.
Are there any differences between the two constructs in ES6?
bind
you are essentially creating two functions. Other than that, the things you mentioned and the fact that arrow functions have a more concise syntax, there is no difference.bind
over an arrow function, I have asked a new question about when to do so: runkit.com/embed/yhv29j5sybvnbind
version it returns a new function which partially applies the arguments tobind
. Arrow doesn't havethis
so use of it is as any free variable from a higher scope.