Created
March 10, 2014 16:57
-
-
Save dy-dx/9469055 to your computer and use it in GitHub Desktop.
Inherited property shadowing in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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