this
:箭头函数不会创建自己的 this
。在箭头函数中,this
由外层作用域决定。new
关键字:因为箭头函数没有 [[Construct]]
内部方法,因此不能用作构造函数。当尝试使用 new
关键字调用箭头函数时,会抛出一个 TypeError
。尝试用箭头函数作为构造函数会导致错误:
const ArrowFunction = () => {
this.value = 42;
};
const instance = new ArrowFunction(); // TypeError: ArrowFunction is not a constructor
普通函数可以用作构造函数,使用 new
关键字调用时会创建一个新的对象实例,并且 this
绑定到这个新创建的对象上。
function RegularFunction() {
this.value = 42;
}
const instance = new RegularFunction();
console.log(instance.value); // 输出 42
箭头函数由于其特殊的 this
绑定机制,无法作为构造函数使用。如果需要使用构造函数来创建对象实例,应该使用普通函数或 ES6 的类。