admin管理员组文章数量:1313810
So I'm just picking up node and I'm wondering how do I add custom events to a class. Code below of my attempt. Essentially just creating a simple farm class, and every time the number of animals change, I display the new number. The event I'm trying to create is totalChanged.
let events = require('events');
class Farm{
constructor(totalAnimals){
this._totalAnimals = totalAnimals;
events.EventEmitter.call(this);
}
get totalAnimals(){
return this._totalAnimals
}
set totalAnimals(newTotal){
this._totalAnimals = newTotal;
}
sellAnimals(amount){
this._totalAnimals -= amount;
this.emit("totalChanged");
}
buyAnimals(amount){
this._totalAnimals += amount;
this.emit("totalChanged");
}
toString(){
return "Number of animals in farm: " + this._totalAnimals;
}
}
let testFarm = new Farm(100);
testFarm.on("totalChanged",testFarm.toString());
testFarm.buyAnimals(20);
So I'm just picking up node and I'm wondering how do I add custom events to a class. Code below of my attempt. Essentially just creating a simple farm class, and every time the number of animals change, I display the new number. The event I'm trying to create is totalChanged.
let events = require('events');
class Farm{
constructor(totalAnimals){
this._totalAnimals = totalAnimals;
events.EventEmitter.call(this);
}
get totalAnimals(){
return this._totalAnimals
}
set totalAnimals(newTotal){
this._totalAnimals = newTotal;
}
sellAnimals(amount){
this._totalAnimals -= amount;
this.emit("totalChanged");
}
buyAnimals(amount){
this._totalAnimals += amount;
this.emit("totalChanged");
}
toString(){
return "Number of animals in farm: " + this._totalAnimals;
}
}
let testFarm = new Farm(100);
testFarm.on("totalChanged",testFarm.toString());
testFarm.buyAnimals(20);
Share
Improve this question
asked Nov 29, 2019 at 7:38
abcabc
597 bronze badges
1 Answer
Reset to default 13You've got a couple options:
If you want to use instance.on
you have to inherit from EventEmitter
like so:
let EventEmitter = require('events').EventEmitter
class Farm extends EventEmitter {
constructor() {
super()
}
buyAnimals() {
this.emit('totalChanged', { value: 'foo' })
}
}
let testFarm = new Farm()
testFarm.on('totalChanged', value => {
console.log(value)
})
testFarm.buyAnimals()
If you prefer to use position instead of inheritance, you can simply instantiate EventEmitter
as a property and use instance.eventEmitter.on
like so:
let EventEmitter = require('events').EventEmitter
class Farm {
constructor() {
this.eventEmitter = new EventEmitter()
}
buyAnimals() {
this.eventEmitter.emit('totalChanged', { value: 'foo' })
}
}
let testFarm = new Farm()
testFarm.eventEmitter.on('totalChanged', value => {
console.log(value)
})
testFarm.buyAnimals()
本文标签: javascriptHow do I add custom events to classes in nodeStack Overflow
版权声明:本文标题:javascript - How do I add custom events to classes in node? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741929602a2405496.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论