如何在课堂上设置局部变量

时间:2020-03-03 07:20:37

标签: javascript node.js class

class Calculator {
  constructor() {
    console.log(`Enter the numbers and the operation index you want to perform: 
        1) Addition 
        2) Substraction 
        3) Multiplication 
        4) Division`);

    this.calculate();
  }

  /** 
   * @param {number} firstValue get the first value
   *  @param {number} secondValue get the second value 
   */
  addition(firstValue, secondValue) {
    return firstValue + secondValue;
  }

  /**
   * @param {number} firstValue get the first value
   *  @param {number} secondValue get the second value 
   */
  subtraction(firstValue, secondValue) {
    return firstValue - secondValue;
  }

  /** 
   *  @param {number} firstValue get the first value 
   *  @param {number} secondValue get the second value 
   */
  multiplication(firstValue, secondValue) {
    return firstValue * secondValue;
  }

  /** 
   *  @param {number} firstValue get the first value 
   *  @param {number} secondValue get the second value 
  */
  division(firstValue, secondValue) {
    if (secondValue != 0) {
      return firstValue / secondValue;
    }
    return 'Cannot perform division by 0';
  }

  calculate() {
    prompt.get(propertiesPrompt, (error, values) => {
      if (error) {
        throw new Error(error);
      }

      console.log(operationResult[values.operation](values.valueOne, values.valueTwo));
      prompt.get(choiceResponse, (responseError, response) => {
        if (responseError) {
          throw new Error(responseError);
        }

        if (response.optionSelected.toLowerCase() == 'y') {
          this.calculate()
        }
      });
    });
  }
}

在上面的代码中,我想删除加法,减法,mul和除法中的参数,并希望在类中设置变量属性,以便可以直接在方法中调用存储的值并执行操作。那怎么办?

4 个答案:

答案 0 :(得分:2)

本地类变量的正确名称是字段

class Calculator {
  constructor() {
    this.field = 10
  }

  show() {
    console.log(this.field)
  }
}

另外,类的函子的名称是方法

方法和字段共同为成员

答案 1 :(得分:1)

您可以在构造函数

中设置值
var prompt = require('prompt');

prompt.start();

class Calculator {
  /**
   * @param {number} firstValue get the first value
   *  @param {number} secondValue get the second value
   */
  constructor() {
    console.log(`Enter the numbers and the operation index you want to perform: 
        1) Addition 
        2) Substraction 
        3) Multiplication 
        4) Division`);

    this.firstValue = 0;
    this.secondValue = 0;

    this.calculate();
  }

  addition() {
    return this.firstValue + this.secondValue;
  }

  subtraction() {
    return this.firstValue - this.secondValue;
  }

  multiplication() {
    return this.firstValue * this.secondValue;
  }

  division() {
    if (this.secondValue != 0) {
      return this.firstValue / this.secondValue;
    }
    return 'Cannot perform division by 0';
  }

  calculate() {
    prompt.get(['firstValue', 'secondValue'], (error, values) => {
      if (error) {
        throw new Error(error);
      }

      this.firstValue = Number(values.firstValue);
      this.secondValue = Number(values.secondValue);

      prompt.get(['choice'], (responseError, response) => {
        if (responseError) {
          throw new Error(responseError);
        }

        let result = 0;

        switch (Number(response.choice)) {
          case 1:
            result = this.addition();
            break;
          case 2:
            result = this.subtraction();
            break;
          case 3:
            result = this.multiplication();
            break;
          case 4:
            result = this.division();
            break;
          default:
            console.log('>>>>>  Please select valid operation');
            break;
        }

        console.log('>>>>>  result : ', result);

        prompt.get(['optionSelected'], (responseError, response) => {
          if (responseError) {
            throw new Error(responseError);
          }

          if (response.optionSelected.toLowerCase() == 'y') {
            this.calculate();
          }
        });
      });
    });
  }
}

new Calculator();

答案 2 :(得分:0)

您可以使用关键字this在类中设置局部变量。只需在班级中的任何地方使用this.myVar = myValue进行设置。在分配变量之前不必先定义变量,并且可以用相同的方式在类中的任何地方访问该变量。请注意,它也可以从类外部访问(和写)。

答案 3 :(得分:0)

类不是具有局部作用域或全局作用域的函数。类具有字段,您可以按以下方式使用字段:

class Calculator {
    constructor() { 
         this.field = 10
    } 
    print() { 
        console.log(this.field)
     }
 };