Skip to content

Instantly share code, notes, and snippets.

@vampaynani
Created April 2, 2018 06:57
Show Gist options
  • Save vampaynani/651dc6b9a47c24c80e351ec1296e2972 to your computer and use it in GitHub Desktop.
Save vampaynani/651dc6b9a47c24c80e351ec1296e2972 to your computer and use it in GitHub Desktop.
How to share changes between instances with prototype(aka static properties)
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