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
Add a ment  | 

1 Answer 1

Reset to default 13

You'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