寻找节点“util.inherits”的JavaScript实现

我正在实现一个也可以在节点上运行的JavaScript库,并且我想尽可能地使用节点的API。 我的对象发出的事件,所以我发现这个很好的库称为eventemitter2 ,并重新实现JavaScript的EventEmitter。 现在我想find相同的util.inherits 。 有没有人听说过这样的项目?

你有没有尝试过使用Node.js实现? (它使用Object.create ,所以它可能会或可能不会在您关心的浏览器上工作 )。 以下是https://github.com/joyent/node/blob/master/lib/util.js的实现:

 inherits = function(ctor, superCtor) { ctor.super_ = superCtor; ctor.prototype = Object.create(superCtor.prototype, { constructor: { value: ctor, enumerable: false, writable: true, configurable: true } }); }; 

由CoffeeScript使用另一种方法编译

 class Super class Sub extends Super 

 var Sub, Super, __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; }; Super = (function() { function Super() {} return Super; })(); Sub = (function(_super) { __extends(Sub, _super); function Sub() { return Sub.__super__.constructor.apply(this, arguments); } return Sub; })(Super); 

你不需要使用任何外部库。 按原样使用javascrit。

B从Ainheritance

 B.prototype = Object.create (A.prototype); B.prototype.constructor = B; 

而在B的构造函数里面:

 A.call (this, params...); 

如果你知道javascript有一个名为构造函数的属性,那么避免它,不需要隐藏或不枚举它,避免避免避免。 没有必要有一个超级财产,只需使用A.call。 这是JavaScript,不要尝试像使用任何其他语言一样使用它,因为你会失败。