考虑:
function Panda() {
this.weight = 100;
return [1,2];
}
console.log(new Panda());
当我们使用new
关键字(new Panda()
)进行实例化时,它会返回:[1,2]
如果没有return语句,则返回:{ weight: 100 }
function Panda() {
this.weight = 100;
}
console.log(new Panda());
使用返回语句:return "Hello"
,它会返回{ weight: 100 }
function Panda() {
this.weight = 100;
return "Hello";
}
console.log(new Panda());
为什么这样做?因为它必须是一个对象吗?