0

I have the following code:

function Bar() {
}

Bar.prototype.c = 0;

var foo = new Bar();
foo.c = 20;

var test = new Bar();
console.log(test.c);

The value I get in the console is 0. I thought that these objects would share the same prototype object, thus the output would be 20. Why is this not so?

user1830568
  • 495
  • 1
  • 7
  • 12
  • Its called shadowing, providing a value on the instance or closer to the instance in the prototype chain. That and more is explained here: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 mutate a prototype value on an instance and it'll change the prototype. – HMR Nov 30 '14 at 13:31

1 Answers1

1

They do share the same prototype.

However, when you set foo.c, you're setting a property on foo, not its prototype.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964