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和除法中的参数,并希望在类中设置变量属性,以便可以直接在方法中调用存储的值并进行操作。那怎么办?
您可以在构造函数中设置值
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();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句