728x90

node.js 에서 유틸리티를 상속하는 예제이다.

ES6 이전버전에서는 유용하겠지만 ES6 이후는 클래스 상속으로 처리하면 된다.

 

// 모듈 로딩
const util = require('util');
 
// 유틸리티 상속
function Parent() {
}
Parent.prototype.sayHello = function () {
    console.log('Hello world, from Parent Class');
}
 
function Child() {
}
util.inherits(Child,Parent); // 상속관계 지정
 
const child = new Child();
child.sayHello();

 

ES6 클래스 상속

// 모듈 로딩
// const util = require('util');
 
// 클래스 구현 : 자바스크립트에서 클래스 사용은 ES6에서부터 지원
class Parent {
    sayHello() {
        return 'Hello world, from Parent Class';
    }
}
 
// 클래스 상속
class Child extends Parent {
}
 
const child = new Child();
console.log(child.sayHello());
 

 

블로그 이미지

Link2Me

,