ES6 arrow functions not working on the prototype?

Arrow functions provide a lexical this. It uses the this that is available at the time the function is evaluated.

It is logically equivalent to (the following isn’t valid code since you can’t have a variable named this):

(function(this){
   // code that uses "this"
 })(this)

In your 1st example the arrow function is within the constructor, and this points to the newly generated instance.

In your 3rd example, an arrow function isn’t used and standard this behavior works as always (the this in the function scope).

In your 2nd example, you use an arrow function but at the scope it’s evaluated, this is global / undefined.

Leave a Comment