Node.js - 从模块发出事件的最佳方法
我一直在使用 EventEmitter
,但我对应该如何从模块中实现它感到困惑。我见过几种不同的方法,它们似乎都有效。以下是我见过的一些:
来自 此处:
var Twitter = function() {...};
Twitter.prototype = new events.EventEmitter;
但随后在 "Mastering Node" 他们这样做:(
function Dog(name) {
this.name = name;
EventEmitter.call(this);
}
Dog.prototype.__proto__ = EventEmitter.prototype;
为什么你需要.call它?)
然后在我的自己的代码我尝试了另一种方式:
function Class() {}
Class.prototype = EventEmitter.prototype;
它们都只是以自己的方式继承 EventEmitter,所以最简单的解决方案不是最好的吗?
I've been playing around with the EventEmitter
, but I'm confused about how exactly I should implement it from a module. I've seen a few different ways, and they all seem to work. Here are a few I've seen:
From here:
var Twitter = function() {...};
Twitter.prototype = new events.EventEmitter;
But then in "Mastering Node" they do it this way:
function Dog(name) {
this.name = name;
EventEmitter.call(this);
}
Dog.prototype.__proto__ = EventEmitter.prototype;
(why would you need to .call it?)
And then in my own code I tried yet another way:
function Class() {}
Class.prototype = EventEmitter.prototype;
They're all just inheriting from EventEmitter in their own way, so wouldn't the simplest solution be the best?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Node 有一个库函数 util.inherits,它比公认的答案稍微简单一些。下面的代码是根据 v0.8.12 文档修改的。
Node has a library function, util.inherits, that is slightly more straightforward than the accepted answer. Code below is modified from the v0.8.12 docs.
您应该使用
__proto__
继承风格。 这假设您仅针对 Node 进行编码,或者仅支持您最喜欢的浏览器。另外,如果您关心基本原型构造函数中的任何逻辑,则必须使用Base.call(this)
。引用基本原型的 __proto__ 技术将确保instanceof 运算符正确识别原型的实例。子类实例的
.constructor
属性将引用您期望的构造函数。它还具有不实例化基本原型的新实例的优点。new Base()
样式还将确保instanceof
为您提供正确的答案,但它将运行 Base 的构造函数。通常不是问题,但如果您的基本构造函数具有必需的参数,则可能会出现问题。它还会将 .constructor 属性设置为基本构造函数,将类的原型设置为基类的原型会使
instanceof
感到困惑,因为基类的任何后代也将显示为孩子。清澈如泥,对吧?这个例子应该有帮助:
You should use the
__proto__
style of inheritance. This assumes you're coding solely for Node, or only supporting your favorite browsers. Also,Base.call(this)
is necessary if you care about any logic in the constructor of your base prototype.The
__proto__
technique to reference a base prototype will ensure that theinstanceof
operator correctly identifies instances of the prototype. The.constructor
property of instances of the child class will reference the constructor you expect it to. It also has the benefit of not instantiating a new instance of the base prototype.The
new Base()
style will also ensure thatinstanceof
gives you the correct answer, but it will run the constructor for Base. Generally not an issue, but can be problematic if your base constructor has required arguments. It will also set the.constructor
property to the base constructor, not the descendant constructor.Setting the prototype of your class to the prototype of the base class will confuse
instanceof
as any descendants of the base will also appear to be instances of the child.Clear as mud, right? This example should help: