Discussion

#121 F.prototype

easy
javascript

What would be the output of following code:

function Rabbit() {}
Rabbit.prototype = {
  eats: true
};

let rabbit1 = new Rabbit();

console.log(rabbit1.eats); // ?


Rabbit.prototype = {
  eats: false
};

cont rabbit2 = new Rabbit();

console( rabbit1.eats ); // ?

console( rabbit2.eats ); // ?

The output will be:

true
true
false
  • rabbit1.eats logs true because rabbit1 is created when Rabbit.prototype has eats set to true.
  • rabbit1.eats logs true again because reassigning Rabbit.prototype creates a new prototype object but does not affect existing instances.
  • rabbit2.eats logs false because rabbit2 is created after Rabbit.prototype is reassigned to a new object with eats set to false.