很多都是exports,而没有继承,我想问下,exports方式有没有提供继承! 还是说继承的话要使用global来应用! 不知道大家有没有遇到需要继承的情况!
11 回复
继承用**util.inherits(constructor, superConstructor)**嘛,官方文档中的例子:
var util = require("util");
var events = require("events");
function MyStream() {
events.EventEmitter.call(this);
}
util.inherits(MyStream, events.EventEmitter);
MyStream.prototype.write = function(data) {
this.emit("data", data);
}
var stream = new MyStream();
console.log(stream instanceof events.EventEmitter); // true
console.log(MyStream.super_ === events.EventEmitter); // true
stream.on("data", function(data) {
console.log('Received data: "' + data + '"');
})
stream.write("It works!"); // Received data: "It works!"