Skip to content

Instantly share code, notes, and snippets.

@SanderSpies
Created August 29, 2013 06:15
Show Gist options
  • Save SanderSpies/6374733 to your computer and use it in GitHub Desktop.
Save SanderSpies/6374733 to your computer and use it in GitHub Desktop.
Example for inheritance that protects the parent's functions by using Object.freeze
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