Skip to content

Instantly share code, notes, and snippets.

@veryeasily
Created March 5, 2014 17:35
Show Gist options
  • Save veryeasily/9372176 to your computer and use it in GitHub Desktop.
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
### 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