Created
October 25, 2016 08:03
-
-
Save yangfch3/cb08ce4d1dfb7fbcfcebd8c576357dae to your computer and use it in GitHub Desktop.
JavaScript 继承最佳实践(寄生组合式继承)
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
| function inherit (SubType, SuperType) { | |
| if (Object.create) { | |
| var prototype = Object.create(SuperType.prototype); | |
| prototype.constructor = SubType; | |
| SubType.prototype = prototype; | |
| return; | |
| } | |
| function F() {} | |
| F.prototype = SuperType.prototype; | |
| var prototype = new F(); | |
| prototype.constructor = SubType; | |
| SubType.prototype = prototype; | |
| }; | |
| /** | |
| * SuperType(){} | |
| * | |
| * SubType(){ | |
| * SuperType.call(this); | |
| * ... | |
| * } | |
| * | |
| * inherit(SubType, SuperType); | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment