私たちはこのオープンソースプロジェクトを世界中の人々に提供したいと考えています。このチュートリアルの内容をあなたが知っている言語に翻訳するのを手伝ってください。
レッスンに戻る

計算機を作成する

重要性: 5

3つのメソッドをもつ calculator オブジェクトを作りなさい。:

  • read() は2つの値を聞き、オブジェクトプロパティとしてそれらを格納します。
  • sum() は保存した値の合計を返します。
  • mul() は保存した値を掛け、その結果を返します。
let calculator = {
  // ... your code ...
};

calculator.read();
alert( calculator.sum() );
alert( calculator.mul() );

デモを実行

テストと一緒にサンドボックスを開く

let calculator = {
  sum() {
    return this.a + this.b;
  },

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

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

calculator.read();
alert( calculator.sum() );
alert( calculator.mul() );

サンドボックスでテストと一緒に解答を開く

Morty Proxy This is a proxified and sanitized view of the page, visit original site.