util.inherits in coffeescript
我正在使用Node.js服务器,并使用咖啡脚本进行开发。
这是如何工作咖啡脚本?
EventEmitter = require('events').EventEmitter util.inherits(Connector, EventEmitter)
是吗?
EventEmitter = require('events').EventEmitter class @Connector extends EventEmitter
我基本上试图添加emit
到Connector
。 就像是:
this.emit('online')
是的, extends
和util.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.create
而extends
使用ES3兼容版本 -
util.inhirits
使子构造函数的constructor
属性不可枚举 -
extend
父构造函数的“静态”属性复制到子构造函数中 - 如果没有给出类的
constructor
,extends
关键字会自动调用超级构造constructor