- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我想要一些预定义的自定义监听器,这些监听器已经用类的定义 定义(如 'newListner'
事件中的构建)。所以我不想只在构造函数中绑定(bind)它们,因为它将在该类的每个新实例上执行。
如何做到这一点?修改原型(prototype)?有可能吗?
到目前为止我所拥有的:
class Cat extends EventEmitter {
// just added for demonstration, I don't want this!
constructor() {
super();
// does fire
this.on('wave', function() { console.log('constructor wave'); });
}
}
// compiles but does not fire
Cat.prototype.on('wave', function() { console.log('prototype wave'); });
var cat = new Cat();
cat.emit('wave');
最佳答案
您无法避免为每个实例分别注册监听器,而这样做的自然位置是在构造函数中1、2。但是,您可以避免创建新的监听器函数:
class Cat extends EventEmitter {
constructor() {
super();
this.on('wave', this.onWave);
}
onWave() {
console.log('prototype wave');
}
}
var cat = new Cat();
cat.emit('wave');
1:还有其他方法,例如 ._events
的 getter。你可以用它做各种花哨的东西,包括“默认”监听器的原型(prototype)继承,但这些都过于复杂并且很快就会让你忘记。只需在构造函数中添加一些通用代码,您也可以做一些花哨的事情——而且更简洁。
2:你也可以重写(特化)EventEmitters 的 init
方法,但归根结底是完全一样的。
关于javascript - 在扩展 EventEmitter 的 ES6 类定义中设置事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35796828/
我已经尝试了很长时间自己解决这个问题,但找不到适合我的问题的解决方案。 我有这个方法: handle_database = function(callback, req, res) { pool.ge
我差点把问题标题写成 - Communicating between eventEmitters 我有一个全局范围内的 node.js 模块 (X)(它使用 eventEmitter)。然后我有每个套
我正在尝试从一个 EventEmitter 发射到另一个 EventEmitter,或者寻找替代解决方案。下面的代码不起作用。 多发射器示例 var events = require('events'
有很多示例在自定义事件发射器构造函数中未使用 events.EventEmitter.call(this),而其他示例正在使用它 (official documentation): var event
假设我有这段代码: export class ProductsListComponent { @Output() onProductSelected: EventEmitter; constr
有时我们会遇到泛型变量应该被省略的情况。像这样: @Component( ... ) class MyComponent { @Output() public cancel = new Eve
我想做这样的事情: var events = require("events"); var emitterA = new events.EventEmitter(); var emitterB = n
EventEmitter.emit() 和 EventEmitter.next() 有什么区别?两者都将事件分派(dispatch)给订阅的监听器。 export class MyService {
我是 angular2 的新手,我对使用 EmitterService 的 EventEmitter 感到困惑,任何人都可以向我解释 Emitter 的用途。 在不使用@input 和@output
我正在学习 Node.js,对自定义 EventEmitter 有一些困惑。代码如下: var events = require("events"); function MyEmitter (name
如标题所述,我想用 TypeScript 重写下面的语句 EventEmitter = require('events').EventEmitter ES6风格 import {EventEmitte
obj.prototype.__proto__ = events.EventEmitter.prototype 我有时会看到上面的代码,我用谷歌搜索了一下,他们说这一行将所有 EventEmitter
我创建了一个抽象父类(super class)来处理我应用中各种表单组件的通用因素。 出于某种原因,我无法正确捕获由在父类(super class)中声明了 EventEmitter 的派生类发出的事
我尝试将表单从我的子组件传递给他的父组件。 child TS: @Output() submit: EventEmitter = new EventEmitter(); updateStream():
这真的很奇怪,也很难解释。 我用过EventEmitter在我的一些服务中,我一直在使用它来更改我的 View 数据。 我遇到了更改路线(通过链接或通过历史返回)的问题,它似乎被多次触发,因此它搞乱了
我正在开发一个 Angular2 组件,它使用网络摄像头将图片捕捉到 Canvas 元素,并将其提取为 Blob。 Blob 和事件分别正常工作,但是当我尝试从 toBlob 的回调函数内发出事件时,
当我有一个带有选择器'recursive-element'的模板时,它的模板看起来像这样 在我定义的类中 greetEvent = new EventEmitter
我非常喜欢将函数导出为 JavaScript 模块的主要 API 的模式。原因是在 JS 中,函数基本上可以做典型对象可以做的任何事情,然后再做一些。 所以这对我来说很典型: function stu
我有这个 Node js脚本: var EventEmitter = require('events').EventEmitter, util = require('util'); var p
在事件生成后附加事件处理程序有一个看似众所周知的问题。这主要是在调用遵循返回 EventEmitter 模式的函数时出现的问题,例如: var EventEmitter = require('even
我是一名优秀的程序员,十分优秀!