Created
August 13, 2014 05:49
-
-
Save rikan/5c7dd35dc21b1d2a9630 to your computer and use it in GitHub Desktop.
来自typescript
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
| var __extends = this.__extends || function (d, b) { | |
| for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | |
| //__.constructor = __.prototype.constructor | |
| function __() { this.constructor = d; } | |
| __.prototype = b.prototype; | |
| d.prototype = new __(); | |
| }; | |
| //Note:类似于下面方式和属性拷贝的综合 | |
| function extend(Child, Parent) { | |
| var F = function(){}; | |
| F.prototype = Parent.prototype; | |
| Child.prototype = new F(); | |
| Child.prototype.constructor = Child; | |
| Child.uber = Parent.prototype; | |
| } | |
| //使用例子 | |
| var Animal = (function () { | |
| function Animal(name) { | |
| this.name = name; | |
| } | |
| Animal.prototype.move = function (meters) { | |
| alert(this.name + " moved " + meters + "m."); | |
| }; | |
| return Animal; | |
| })(); | |
| var Snake = (function (_super) { | |
| __extends(Snake, _super); | |
| function Snake(name) { | |
| //使用call或apply方法实现继承,将父对象的构造函数绑定在子对象上 | |
| _super.call(this, name); | |
| } | |
| Snake.prototype.move = function () { | |
| alert("Slithering..."); | |
| _super.prototype.move.call(this, 5); | |
| }; | |
| return Snake; | |
| })(Animal); | |
| var sam = new Snake("Sammy the Python"); | |
| sam.move(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment