util.js:168
ObjectSetPrototypeOf(ctor.prototype, superCtor.prototype);
^
TypeError: Object.setPrototypeOf called on null or undefined
at setPrototypeOf (<anonymous>)
[90m at Object.inherits (util.js:168:3)[39m
at Object.<anonymous> (E:\nodejs_doit\calc3.js:10:6)
[90m at Module._compile (internal/modules/cjs/loader.js:1157:30)[39m
[90m at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)[39m
[90m at Module.load (internal/modules/cjs/loader.js:1001:32)[39m
[90m at Function.Module._load (internal/modules/cjs/loader.js:900:14)[39m
[90m at Module.require (internal/modules/cjs/loader.js:1043:19)[39m
[90m at require (internal/modules/cjs/helpers.js:77:18)[39m
at Object.<anonymous> (E:\nodejs_doit\ch04_test4.js:1:20)
이런 에러가 떠서 구글링을 전전해봤지만... 해결이 안되었다.
해결한 코드 :
[ calc3.js ]
const util = require("util");
const EventEmitter = require("events").EventEmitter;
const calculator = function() {
const self = this;
this.on("stop", () => {
console.log("calculator에 stop event 전달됨");
});
};
util.inherits(calculator, EventEmitter);
//Calc 객체가 EventEmitter를 상속받음
calculator.prototype.add = (a, b) => {
return a + b;
};
module.exports = calculator;
module.exports.title = "calculator 계산기 : ";
[ ch04_test4.js ]
const calculator = require("./calc3");
const calc1 = new calculator();
calc1.emit("stop");
console.log(calculator.title + "에 stop 이벤트 전달");
-----------------------------------------
에러가 났던 지점 :
const util = require("util");
const EventEmitter = require("events").EventEmitter;
const calculator = function() {
const self = this;
this.on("stop", () => {
console.log("calculator에 stop event 전달됨");
});
}; -------> 이 부분을
util.inherits(calculator, EventEmitter);
//Calc 객체가 EventEmitter를 상속받음
calculator.prototype.add = (a, b) => {
return a + b;
};
module.exports = calculator;
module.exports.title = "calculator 계산기 : ";
-----------------------------------------------------------
const util = require("util");
const EventEmitter = require("events").EventEmitter;
const calculator = () => {
const self = this;
this.on("stop", () => {
console.log("calculator에 stop event 전달됨");
});
}; -------> 이 부분을 이렇게 하면 에러남!!!!!
util.inherits(calculator, EventEmitter);
//Calc 객체가 EventEmitter를 상속받음
calculator.prototype.add = (a, b) => {
return a + b;
};
module.exports = calculator;
module.exports.title = "calculator 계산기 : ";
--------------------------------------------------------------