keeping sync with nodejs' util.inherits()
Showing
1 changed file
with
13 additions
and
18 deletions
... | @@ -131,27 +131,22 @@ function format(f) { | ... | @@ -131,27 +131,22 @@ function format(f) { |
131 | exports.format = format; | 131 | exports.format = format; |
132 | 132 | ||
133 | /** | 133 | /** |
134 | * Inherit the prototype methods from one constructor into another, also | 134 | * Inherit the prototype methods from one constructor into another. |
135 | * exposes the `__super__` property to child class. | ||
136 | * | 135 | * |
137 | * @param Function child Constructor function which needs to inherit the | 136 | * @param {function} ctor Constructor function which needs to inherit the |
138 | * prototype. | 137 | * prototype. |
139 | * @param Function parent Constructor function to inherit prototype from. | 138 | * @param {function} superCtor Constructor function to inherit prototype from. |
140 | * @return Function The child class | ||
141 | */ | 139 | */ |
142 | function inherits(child, parent) { | 140 | function inherits(ctor, superCtor) { |
143 | for (var key in parent) { | 141 | ctor.super_ = ctor.__super__ = superCtor; |
144 | if (Object.prototype.hasOwnProperty.call(parent, key)) { | 142 | ctor.prototype = Object.create(superCtor.prototype, { |
145 | child[key] = parent[key]; | 143 | constructor: { |
144 | value: ctor, | ||
145 | enumerable: false, | ||
146 | writable: true, | ||
147 | configurable: true | ||
146 | } | 148 | } |
147 | } | 149 | }); |
148 | function ctor() { | ||
149 | this.constructor = child; | ||
150 | } | ||
151 | ctor.prototype = parent.prototype; | ||
152 | child.prototype = new ctor(); | ||
153 | child.__super__ = parent.prototype; | ||
154 | return child; | ||
155 | } | 150 | } |
156 | exports.inherits = inherits; | 151 | exports.inherits = inherits; |
157 | 152 | ... | ... |
-
Please register or sign in to post a comment