본문 바로가기

NodeJS

ObjectSetPrototypeOf(ctor.prototype, superCtor.prototype); 에러가 뜸. Do it Node.js 예제 ch04_test4.js와 calc3.js

util.js:168
  ObjectSetPrototypeOf(ctor.prototype, superCtor.prototype);
  ^

TypeError: Object.setPrototypeOf called on null or undefined
    at setPrototypeOf (<anonymous>)
    at Object.inherits (util.js:168:3)
    at Object.<anonymous> (E:\nodejs_doit\calc3.js:10:6)
    at Module._compile (internal/modules/cjs/loader.js:1157:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1177:10)
    at Module.load (internal/modules/cjs/loader.js:1001:32)
    at Function.Module._load (internal/modules/cjs/loader.js:900:14)
    at Module.require (internal/modules/cjs/loader.js:1043:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    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 계산기 : "; 

 

--------------------------------------------------------------

 

 

728x90
300x250