Consider:
function Panda() {
this.weight = 100;
return [1,2];
}
console.log(new Panda());
Run codeHide resultWhen we create an instance with the keyword new( new Panda()), it returns:[1,2]
Without a return statement, it returns: { weight: 100 }
Using the return statement: return "Hello"it returns{ weight: 100 }
function Panda() {
this.weight = 100;
return "Hello";
}
console.log(new Panda());
Run codeHide resultWhy is he doing this? Because it must be an object?
source
share