class A {
getA() {
console.log(this);
}
}
let a = new A();
let funcA = a.getA;
funcA();//undefined
代码如上,是因为class么?我试了function A结果是window对象,为什么用class就会是undefined?难道funcA中的this不指向window?
求帮忙解答谢谢!
class A {
getA() {
console.log(this);
}
}
let a = new A();
let funcA = a.getA;
funcA();//undefined
代码如上,是因为class么?我试了function A结果是window对象,为什么用class就会是undefined?难道funcA中的this不指向window?
求帮忙解答谢谢!
因为class xxx{}中的代码都是严格模式的。等于是自动加上"use strict"
在严格模式下禁止this指向全局对象(window)。