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?

1
  • Its called shadowing, providing a value on the instance or closer to the instance in the prototype chain. That and more is explained here: stackoverflow.com/questions/16063394/… mutate a prototype value on an instance and it'll change the prototype. Commented Nov 30, 2014 at 13:31

1 Answer 1

1

They do share the same prototype.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.