I know that arrow functions inherit this
from enclosing scope. Yet, still can't understand why this
in arrow function defined in object literal points to global object, while in constructor to created object.
Consider following code:
function Obj() {
this.show = () => {
console.log(this);
};
}
const o = new Obj();
const o2 = {
show: () => {
console.log(this);
}
}
o.show(); // o
o2.show() // window || undefinded
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…