Why does this this point to the obj object?

the first this is 1990. Why is the second obj?

var obj = {
    birth: 1990,
    getAge: function () {
        var b = this.birth; // 1990
        var fn = () => new Date().getFullYear() - this.birth; // thisobj
        return fn();
    }
};
obj.getAge(); // 25
Apr.19,2021

to put it more vulgar, it should be able to explain clearly:

functionfunctionthisthis
thiswindow  {}(

in this case, the arrow function is defined in the function getAge, so getAge is its father, and the this mentioned in the arrow function is the this of getAge. After obj.getAge () is executed, the this of getAge points to obj, so the this in the arrow function also points to obj

aside:
I have seen your interaction with other people. Are you still not clear about the this understanding of ordinary functions? The this, of an ordinary function, such as var b in this case, uses the ordinary function this, the this runtime, to determine which object to point to: the this points to the person on whom the binding is executed. For example:

  Arrow function this pointer  

Menu