Web/Javascript

[Javascript] 모던 JS 4.5 (new 연산자와 생성자 함수) 과제

동띵 2022. 5. 18. 17:20

1) 함수 두 개로 동일한 객체 만들기

=> 동일한 객체를 반환하게 하면 가능하다.

2) 계산기 만들기

function Calculator() {
    this.read = function() {
        this.a = parseInt(prompt("첫 번째 수를 입력하세요", 0));
        this.b = parseInt(prompt("두 번째 수를 입력하세요", 0));
    },
    this.sum = function() {
        return this.a+this.b;
    },
    this.mul = function() {
        return this.a*this.b;
    }
}


3) 누산기 만들기

function Accumulator(value) {
    this.value = value,
    this.read = function() {
        this.num = parseInt(prompt("더할 값을 입력하세요", 0));
        this.value += this.num;
    }
}