关于 JavaScript 中不同地方的 this 指向。
本文中所有 Demo 均在 RunJS 中运行。
函数对象本身
当函数作为构造器被调用时,this 指向自身。
function constructor(){
console.log(this);
}
new constructor();
箭头函数不能作为构造器
const arrowCanNotBeConstructor = () => {
console.log(this);
}
new arrowCanNotBeConstructor();
// TypeError: arrowCanNotBeConstructor is not a constructor