Created
August 29, 2013 06:15
-
-
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
This file contains 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
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