본문 바로가기

JavaScript/javascript 기초 공부하기

prototype, class 비교하기

동일한 기능을 하지만 

유지보수에 용이한 리팩토링

 

function fullstack(backend, frontend) {
    this.backend = backend;
    this.frontend = frontend;

    fullstack.prototype.getBackend = () => this.backend;
    fullstack.prototype.setBackend = () => (this.backend = backend);

    fullstack.prototype.getFrontend = () => this.frontend;
    fullstack.prototype.setFrontend = () => (this.frontend = frontend);
}

const Fullstack = new fullstack('javascript', 'javascript');

console.log(Fullstack.getBackend());
console.log(Fullstack.getFrontend());

 

class fullStack {
    constructor(backend, frontend) {
        //async 사용불가
        this.backend = backend;
        this.frontend = frontend;
    }

    getBackend() {
        return this.backend;
    }

    getFrontend() {
        return this.frontend;
    }

    setBackend(backend) {
        this.backend = backend;
    }

    setFrontend(frontend) {
        this.frontend = frontend;
    }
}

const FullStack = new fullStack('javascript', 'javascript');

console.log(FullStack.getBackend());
console.log(FullStack.getFrontend());