由于我想强制执行new,因此我创建了如下构造函数。它工作正常,即使我不使用new调用构造函数,实例仍然可以获得属性。但是我很困惑,既然在 if 语句中,它已经return,为什么可以调用 if 语句下面的语句?
function Car(){
if(!(this instanceof Car)){
return new Car();
console.log("can i arrive here?");
}
this.tires = "I have four tires";
console.log("yeah,I can arrive here");
}
//Note: without new
var car = Car();//output=> yeah,I can arrive here
console.log(car.tires);//output => I have four tires