Consider:
function Panda() {
this.weight = 100;
return [1,2];
}
console.log(new Panda());
When we instantiate with new
keyword (new Panda()
), it will returns: [1,2]
Without return statement, it returns: { weight: 100 }
function Panda() {
this.weight = 100;
}
console.log(new Panda());
With a return statement like: return "Hello"
, it returns { weight: 100 }
function Panda() {
this.weight = 100;
return "Hello";
}
console.log(new Panda());
Why does it do this? Because is it must be an object?