Events Node.js-从模块发出事件的最佳方法

Events Node.js-从模块发出事件的最佳方法,events,javascript-events,node.js,Events,Javascript Events,Node.js,我一直在玩EventEmitter,但我不知道应该如何从模块中实现它。我见过几种不同的方法,它们似乎都很有效。以下是我看到的一些: 发件人: 但他们是这样做的: function Dog(name) { this.name = name; EventEmitter.call(this); } Dog.prototype.__proto__ = EventEmitter.prototype; (你为什么要这么做,就叫它吧?) 然后在我自己的代码中,我尝试了另一种方式: function

我一直在玩EventEmitter,但我不知道应该如何从模块中实现它。我见过几种不同的方法,它们似乎都很有效。以下是我看到的一些:

发件人:

但他们是这样做的:

function Dog(name) {
  this.name = name;
  EventEmitter.call(this);
}

Dog.prototype.__proto__ = EventEmitter.prototype;
(你为什么要这么做,就叫它吧?)

然后在我自己的代码中,我尝试了另一种方式:

function Class() {}

Class.prototype = EventEmitter.prototype;

它们都是以自己的方式从EventEmitter继承的,所以最简单的解决方案不是最好的吗?

您应该使用
\uuuu proto\uu
继承风格。另外,
Base.call(this)
是必要的,如果您关心基本原型的构造函数中的任何逻辑

引用基本原型的
\uuuu proto\uuu
技术将确保
instanceof
操作符正确识别原型的实例。子类实例的
.constructor
属性将引用您期望它使用的构造函数。它还具有不实例化基本原型的新实例的优点

newbase()
样式还将确保
instanceof
给出正确答案,但它将运行Base的构造函数。通常不是问题,但如果基本构造函数具有必需的参数,则可能会出现问题。它还将
.constructor
属性设置为基本构造函数

将类的prototype设置为基类的prototype将混淆
instanceof
,因为基类的任何后代也将显示为子类的实例

清澈如泥,对吗?这个例子应该有助于:

// Base constructor.
// A, B, and C will inherit from Base.
function Base() {
    this.name = 'base';
}

// new Base() style
function A() {
    Base.call(this);
}
A.prototype = new Base();

// __proto__ = prototype style
function B() {
    Base.call(this);
}
B.prototype.__proto__ = Base.prototype;

// prototype = protoype style
function C() {
    Base.call(this);
}
C.prototype = Base.prototype;

// create instances
var a = new A();
var b = new B();
var c = new C();

// are we who we think we are?
console.assert(a instanceof A);
console.assert(b instanceof B);
console.assert(c instanceof C);
// so far so good

// do we respect our elders?
console.assert(a instanceof Base);
console.assert(b instanceof Base);
console.assert(c instanceof Base);
// we have respect

// test to see that Base.call(this)
// functioned as expected
console.assert(a.name == 'base');
console.assert(b.name == 'base');
console.assert(c.name == 'base');
// ok, good...

// but now things get weird
console.assert(a instanceof C);
console.assert(b instanceof C);
// that's not right! a is not C, b is not C!

// At least A and B do not confuse identities
console.assert(!(a instanceof B));
console.assert(!(b instanceof A));

console.assert(!(c instanceof A));
console.assert(!(c instanceof B));

// so we've determined that style C is no good.
// C confuses the inheritance chain.

// B is the winner.

// Why? Only B passes this test
console.assert(b.constructor == B);

// a and c's constructors actually point to the Base constructor
console.assert(a.constructor == Base);
console.assert(c.constructor == Base);

// Word B.

节点有一个库函数util.inherits,它比接受的答案稍微简单一些。下面的代码是从中修改的


哇,谢谢你。我希望我能对这个答案投多一票。我有一个与此相关的答案。你介意看一下吗?另外你能解释一下
events.EventEmitter.call(这个)
为什么这是必需的,或者如果可以忽略它?@majidarif简短的回答是调用执行EventEmitter函数,MyStream实例作为“this”变量,因此它将完成MyStream实例的所有EventEmitter初始化。希望这是有道理的。
// Base constructor.
// A, B, and C will inherit from Base.
function Base() {
    this.name = 'base';
}

// new Base() style
function A() {
    Base.call(this);
}
A.prototype = new Base();

// __proto__ = prototype style
function B() {
    Base.call(this);
}
B.prototype.__proto__ = Base.prototype;

// prototype = protoype style
function C() {
    Base.call(this);
}
C.prototype = Base.prototype;

// create instances
var a = new A();
var b = new B();
var c = new C();

// are we who we think we are?
console.assert(a instanceof A);
console.assert(b instanceof B);
console.assert(c instanceof C);
// so far so good

// do we respect our elders?
console.assert(a instanceof Base);
console.assert(b instanceof Base);
console.assert(c instanceof Base);
// we have respect

// test to see that Base.call(this)
// functioned as expected
console.assert(a.name == 'base');
console.assert(b.name == 'base');
console.assert(c.name == 'base');
// ok, good...

// but now things get weird
console.assert(a instanceof C);
console.assert(b instanceof C);
// that's not right! a is not C, b is not C!

// At least A and B do not confuse identities
console.assert(!(a instanceof B));
console.assert(!(b instanceof A));

console.assert(!(c instanceof A));
console.assert(!(c instanceof B));

// so we've determined that style C is no good.
// C confuses the inheritance chain.

// B is the winner.

// Why? Only B passes this test
console.assert(b.constructor == B);

// a and c's constructors actually point to the Base constructor
console.assert(a.constructor == Base);
console.assert(c.constructor == Base);

// Word B.
var util = require("util");
var events = require("events");

function MyStream() {
  events.EventEmitter.call(this);
}

util.inherits(MyStream, events.EventEmitter);