javascript - Is events.EventEmitter.call(this) required when creating a custom EventEmitter? -
there lot of example not using events.eventemitter.call(this)
in custom event emitter constructors, while other using (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'); };
with basic understandings of javascript don't know if need it. call necessary to initialization stuff inside eventemitter
?
yes, is.
before node 0.10, wouldn't break if forget that.
now, it will:
the eventemitter constructor initializes various properties now. still works fine oop inheritance parent, have inheritance properly. broken-style js inheritance pattern not work when extending eventemitter class. inheritance style never supported, prior 0.10, did not break.
Comments
Post a Comment