gpt4 book ai didi

javascript - 创建自定义 EventEmitter 时是否需要 events.EventEmitter.call(this)?

转载 作者:搜寻专家 更新时间:2023-10-31 22:43:55 25 4
gpt4 key购买 nike

有很多示例在自定义事件发射器构造函数中未使用 events.EventEmitter.call(this),而其他示例正在使用它 (official documentation):

var events = require('events')
, util = require('util');

var MyEmitter = function () {
events.EventEmitter.call(this);
};

util.inherits(MyEmitter, events.EventEmitter);

MyEmitter.prototype.write = function () {
this.emit('tick');
};

以我对 JavaScript 的基本了解,我不知道是否需要它。是否需要调用以初始化 EventEmitter 中的内容?

最佳答案

是的,是的。

在 Node 0.10 之前,如果您忘记它也不会崩溃。

现在,it will :

The EventEmitter constructor initializes various properties now. It still works fine as an OOP inheritance parent, but you have to do inheritance properly. The Broken-Style JS inheritance pattern will not work when extending the EventEmitter class. This inheritance style was never supported, but prior to 0.10, it did not actually break.

关于javascript - 创建自定义 EventEmitter 时是否需要 events.EventEmitter.call(this)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16674410/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com