Created
April 2, 2018 06:57
-
-
Save vampaynani/651dc6b9a47c24c80e351ec1296e2972 to your computer and use it in GitHub Desktop.
How to share changes between instances with prototype(aka static properties)
This file contains hidden or 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 Robot(){ | |
this.parts = []; | |
} | |
Robot.prototype.legs = 2; | |
Robot.prototype.changeLegs = function(num){ | |
this.__proto__.legs = num; | |
} | |
robby = new Robot(); | |
robby.parts.push('head'); | |
console.log(robby, robby.legs); | |
robby.changeLegs(1); | |
console.log('legs:', robby.legs); | |
cranky = new Robot(); | |
console.log(cranky, cranky.legs); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment