Yes, extends
does a similar thing as util.inherits
.
Implementation of util.inherits
:
inherits = function(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
Compilation of extends
:
var __hasProp = {}.hasOwnProperty,
__extends = function(child, parent) {
for (var key in parent) {
if (__hasProp.call(parent, key))
child[key] = parent[key];
}
function ctor() {
this.constructor = child;
}
ctor.prototype = parent.prototype;
child.prototype = new ctor();
child.__super__ = parent.prototype;
return child;
};
function Connector() {
return Connector.__super__.constructor.apply(this, arguments);
}
__extends(Connector, EventEmitter);
The differences are:
- The exact name of the
super
property on the child constructor
util.inherits
uses Object.create
while extends
does use an ES3-compatible version
util.inhirits
makes the constructor
property of the child constructor non-enumerable
extend
copies "static" properties of the parent constructor onto the child constructor
- The
extends
keyword automatically calls the super constructor if no constructor
is given for the class