en.javascript.info/1-js/04-object-basics/06-constructor-new/2-calculator-constructor/task.md
2022-08-28 16:07:53 -06:00

507 B

importance: 5


Create new Calculator

Create a constructor function Calculator that creates objects with 3 methods:

  • read() prompts for two values and saves them as object properties with names a and b respectively.
  • sum() returns the sum of these properties.
  • mul() returns the multiplication product of these properties.

For instance:

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

alert( "Sum=" + calculator.sum() );
alert( "Mul=" + calculator.mul() );

[demo]