en.javascript.info/1-js/6-objects-more/3-constructor-new/2-calculator-constructor/solution.md
2015-01-14 10:23:45 +03:00

424 B

//+ run demo
function Calculator() {

  this.read = function() {
    this.a = +prompt('a?', 0); 
    this.b = +prompt('b?', 0);
  };

  this.sum = function() {
    return this.a + this.b;
  };

  this.mul = function() {
    return this.a * this.b;
  };
}

var calculator = new Calculator();
calculator.read();

alert( "Сумма=" + calculator.sum() );
alert( "Произведение=" + calculator.mul() );