在 ES6 中,函数有两个不同的内部方法:[[call]]
和 [[Construct]]
。当通过 new 关键字调用函数时,执行的是 [[Construct]]
函数,它负责创建一个通常被称作实例的新对象。如果不通过 new 关键字调用函数,则执行 [[call]]
函数,从而直接执行代码中的函数体。
当调用函数的 [[Construct]]
方法时,new.target
被赋值为 new 操作符的目标,通常是新创建对象实例的构造函数。
function Person(name) {
console.log('typeof new.target:', typeof new.target);
console.log('new.target:', new.target);
if (typeof new.target !== 'undefined') {
this.name = name;
} else {
throw new Error('必须通过 new 关键字来调用 Person');
}
}
const person = new Person();
执行结果:
typeof new.target: function
new.target: function Person(name) {
console.log('typeof new.target:', typeof new.target);
console.log('new.target:', new.target);
if (typeof new.target !== 'undefined') {
this.name = name;
} else {
throw new Error('必须通过 new 关键字来调用 Person');
}
}
如果调用函数的 [[call]]
方法,则 new.target
的值为 undefined
。
function Person(name) {
console.log('new.target:', new.target);
}
const person = Person();
执行结果:
new.target: undefined