Personally, I would suggest the introduction of another variable, providing a "sticky this", which would stay bound to the this-object of the scope, the function was declared in. But what would be the reserved word to be used for it? (This would have to be restricted to arrow-functions, since there would be inconsistencies when assigning a method to an other object.)
back
In my view on `this`, changing the semantics would always harm the generality of functions as first class entities. In case I would assign a function to an object as a method, what would this mean for the semantics of `this`? Evaluating the this-object just as late as possible gives a general approach that isn't prone to fail. By changing semantics, we're always introducing some edge-cases, for which there would have to be special behavior and constraints to be defined. Even with binding there would have to be some constraints to be added. (We have been doing great things with the current implementation for nearly 20 years by now, so it can't be that wrong.)
3 comments
It also makes the arrow shorthand less useful.
Instead of -> {...} replacing function() {...}
We get => {...} replacing (function() {...}).bind(this)
Why should the less common case get the more convenient syntax? Especially considering when we already have Function.prototype.bind to do this. How could they possibly agree on this but not the thin arrow?
There has been some discussion lately of JS being too verbose – this seems to be adding semantic sugar to address this. But introducing ambiguous semantics is quite an other thing.
I really hate the technical choice TC39 made with the arrow function. IT should have been a replacement for ANY kindof function. This is a total let down. Coffeescript syntax makes way more sense.
How does an arrow function's implicit bind(this) poses a problem or limits generality? (Honest question; I'm not a JS wizard.)
> How does an arrow function's implicit bind(this) poses a problem or limits generality?
Well you cant use it unbound. I dont want to declare functions bound to a scope I want a quick way to declare functions.
Dear down-voter: The case of an arrow function asserting the type "function" and its prototype being `Function.prototype`, while not being subject to `call` or `apply` (for `this` being lexically bound to the parent object) is a practical case of introducing extra constraints – think of callbacks and the use of `apply` or `call` on them. (See the Harmony specs.)
So this is a) a personal view on this (as pointed out clearly) and b) not raised deliberately.
Edit: I think, explicitly asserting the binding by an assignment ("var that = this;") isn't the worst option and you may always trace back the variable to the binding-point. It may be an extra line of code, but we can live with that perfectly.