Friday, 17 March 2017

NodeJS/JavaScript: TypeError: person.on is not a function

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

var person = function (name) {
    this.name = name;
};

util.inherits(person, events.EventEmitter);

var james = new person('james');
var people = ['james'];

people.forEach(function (person) {
    person.on('speak', function (msg) {
        console.log(person.name + 'said:' + msg);
    });
});

james.emit('speak', 'hey hello');

I am getting an error

person.on('speak', function (msg) {

       ^  TypeError: person.on is not a function

events & util are built in modules



via Mr.H

No comments:

Post a Comment