Skip to content

Instantly share code, notes, and snippets.

@revathskumar
Forked from SanderSpies/Protected Inheritance
Created August 30, 2013 05:24
Show Gist options
  • Save revathskumar/6386556 to your computer and use it in GitHub Desktop.
Save revathskumar/6386556 to your computer and use it in GitHub Desktop.
var Parent = function Parent(){
};
Parent.prototype = {
a:function a(){
console.log(333);
}
};
// make sure that prototype of parent is protected and can't be changed by children
Object.freeze(Parent.prototype);
var Child = function Child(){
};
Child.prototype = Object.create(Parent.prototype);
Child.prototype.a = 5; // should not work
Child.prototype.z = 2;
var c = new Child();
console.log(c.a, c.z);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment