Skip to content

Instantly share code, notes, and snippets.

@dy-dx
Created March 10, 2014 16:57
Show Gist options
  • Save dy-dx/9469055 to your computer and use it in GitHub Desktop.
Save dy-dx/9469055 to your computer and use it in GitHub Desktop.
Inherited property shadowing in JavaScript
function Foobar () {
}
Foobar.prototype.foo = 1;
Foobar.prototype.bar = {qux: 1};
Foobar.prototype.baz = {qux: 1};
var a = new Foobar();
var b = new Foobar();
console.log(
a.foo, // 1
a.bar, // {qux: 1}
a.baz // {qux: 1}
);
console.log(
b.foo, // 1
b.bar, // {qux: 1}
b.baz // {qux: 1}
);
a.foo = 2; // shadows Foobar.prototype.foo
a.bar = {qux: 2}; // shadows Foobar.prototype.bar
a.baz.qux = 2; // modifies Foobar.prototype.baz
console.log(
a.foo, // 2
a.bar, // {qux: 2}
a.baz // {qux: 2}
);
console.log(
b.foo, // 1
b.bar, // {qux: 1}
b.baz // {qux: 2}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment