util.inherits in coffeescript

我正在使用Node.js服务器,并使用咖啡脚本进行开发。

这是如何工作咖啡脚本?

EventEmitter = require('events').EventEmitter util.inherits(Connector, EventEmitter) 

是吗?

 EventEmitter = require('events').EventEmitter class @Connector extends EventEmitter 

我基本上试图添加emitConnector 。 就像是:

 this.emit('online') 

是的, extendsutil.inherits类似。

util.inherits实现 :

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

汇编的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); 

差异是:

  • 子构造函数的super属性的确切名称
  • util.inherits使用Object.createextends使用ES3兼容版本
  • util.inhirits使子构造函数的constructor属性不可枚举
  • extend父构造函数的“静态”属性复制到子构造函数中
  • 如果没有给出类的constructorextends关键字会自动调用超级构造 constructor