user6819864
user6819864

Reputation: 29

How to set local variable in 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()
        }
      });
    });
  }
}

In the above code I want to remove the parameters in my addition, subtraction, mul and division methods and want to set a variable property in a class, so that I could directly call the stored values in the methods and do the operation. How could that be done?

Upvotes: 1

Views: 1530

Answers (4)

Jay
Jay

Reputation: 3107

You can set values in constructor

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();

Upvotes: 1

mohamed ibrahim
mohamed ibrahim

Reputation: 583

Classes is not a functions which have a local scope or global scope. Classes have fields and you can use your fields as follow:

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

Upvotes: 0

Henrik Hansson
Henrik Hansson

Reputation: 889

You can set a local variable in a class using the keyword this. Just set it by using this.myVar = myValue anywhere in your class. The variable does not have to be defined before you assign it and can be reached anywhere in your class in the same way. Note that it is also accessible (and writeable) from outside the class.

Upvotes: 0

oklas
oklas

Reputation: 8220

Correct name for local class variables is a fields:

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

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

Also the name of funcitons of class is a methods

Together methods and fields are members

Upvotes: 2

Related Questions