Created
August 9, 2012 18:00
-
-
Save robdodson/3306571 to your computer and use it in GitHub Desktop.
inherit method
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
| // Optimized version of classical inheritance | |
| // Proxy constructor is stored in a closure which is | |
| // returned from a self executing function. | |
| var inherit = (function() { | |
| var F = function() {}; | |
| return function(C, P) { | |
| F.prototype = P.prototype; | |
| C.prototype = new F(); | |
| C._super = P.prototype; | |
| C.prototype.constructor = C; | |
| } | |
| })(); | |
| function Parent(a, b) { | |
| this.name = 'Adam'; | |
| } | |
| Parent.prototype.say = function() { | |
| console.log(this.name); | |
| } | |
| function Child() {} | |
| inherit(Child, Parent); // Must be called before manipulating Child's prototype | |
| Child.prototype.newShiz = function() { | |
| console.log('that new shit!'); | |
| } | |
| var child = new Child(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment