Recently I have an argument with a friend who absolutely hate the this
keyword in Javascript. He claims that since the language has an ambiguous binding of this
in different situations, e.g. const f = obj.g
will lose the binding to obj
, obj.g.call(obj2)
will call g
in the context of obj2
..., the this
keyword in Javascript is simply one of the worst thing in Javascript.
He also claims that Javascript would be a lot easier to code/maintain by avoiding the use of this
keyword. He advocates the following pattern for object creation:
function Car() {
const car = {};
car.position = 0;
car.move = () => car.position++;
return car;
}
new Car();
// or
Car();
I am, on the other hand, very comfortable with the this
keyword. I use it a lot and appreciate how bind
, call
and apply
works. However, I can't really find a legit argument against his pattern, because it really seem to be clearer for those who don't know much about Javascript?
What do you think? Give me some insights!