Why does this constructor not return a string?

Consider:

function Panda() {
   this.weight = 100;
   return [1,2]; 
}

console.log(new Panda());
Run codeHide result

When we create an instance with the keyword new( new Panda()), it returns:[1,2]

Without a return statement, it returns: { weight: 100 }

function Panda() {
   this.weight = 100;
}

console.log(new Panda());
Run codeHide result

Using the return statement: return "Hello"it returns{ weight: 100 }

function Panda() {
   this.weight = 100;
   return "Hello"; 
}

console.log(new Panda());
Run codeHide result

Why is he doing this? Because it must be an object?

+6
source share

All Articles