Created
November 29, 2011 20:43
-
-
Save thinkphp/1406419 to your computer and use it in GitHub Desktop.
inheritance $extend
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
| $extend = function(subClass,superClass) { | |
| function F() {}; | |
| F.prototype = superClass.prototype; | |
| subClass.prototype = new F(); | |
| subClass.prototype.constructor = subClass; | |
| subClass._base = superClass; | |
| subClass._super = superClass.prototype; | |
| }; | |
| var Person = function(name) { | |
| this.name = name; | |
| } | |
| Person.prototype.says = function(msg) { | |
| console.log(this.name + ' says: ' + msg); | |
| } | |
| var Ninja = function(name) { | |
| Ninja._base.call(this,name); | |
| } | |
| $extend(Ninja,Person); | |
| Ninja.prototype.says = function(msg) { | |
| return Ninja._super.says.call(this,msg + ' yarrrr'); | |
| } | |
| var p = new Person('adrian'); | |
| p.says('hello'); | |
| var n = new Ninja('rasmus'); | |
| n.says('high kick'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment