Created
March 5, 2014 17:35
-
-
Save veryeasily/9372176 to your computer and use it in GitHub Desktop.
Never modify properties of object pointers on your prototype in javascript, or you break things higher up in the chain. I also show a good workaround
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
### BAD WAY ### | |
class Animal | |
Animal.prototype.foo = {bar: "baz"} | |
class Dog extends Animal | |
fred = new Dog() | |
fred.foo.bar = "broken" | |
alert(Animal.prototype.foo.bar) | |
# => "broken" | |
### WORKAROUND ### | |
class Animal | |
Animal.prototype.foo = {bar: "baz"} | |
class Dog extends Animal | |
fred = new Dog() | |
fred.foo = {} | |
fred.foo.bar = "broken" | |
alert(Animal.prototype.foo.bar) | |
# => "baz" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment