为什么更改构造函数调用的顺序会导致它发生故障?

问题描述 投票:1回答:1

下面的函数创建一个计算器,该计算器最初仅支持两个数字的加减运算,但可以使用“ this.addMethod”进行扩展以容纳其他运算符,例如“ /”,“ *”和“ **” 。

但是,当我通过构造函数(“ new”)添加新的计算器并向其中添加新的操作时,只有先调用power操作(“ **”),才能正常工作。

下面的完整代码:

function Calculator() {
  this.supportedOperators = ['+', '-'];

  this.supportedMethods = [
    (a, b) => a + b,
    (a, b) => a - b
  ];

  this.calculate = (str) => {
    for (operator of this.supportedOperators) {
      if (str.includes(operator)) {
        let delimiter = operator.length; // for '**' operator
        let firstOperand = str.slice(0, str.indexOf(operator));
        let secondOperand = str.slice(str.indexOf(operator) + delimiter);

        return this.supportedMethods[this.supportedOperators.findIndex(item => item === operator)]
          (+firstOperand, +secondOperand);
        /* check the supported operators, then use it on operands
                           
                            A mess here, but i tried my best to make it more readable and understandable */

      } else console.log('Unsupported operation');
    }
  };

  this.addMethod = (operator, method) => {
    this.supportedOperators.push(operator);
    this.supportedMethods.push(method);
  }

}

let powerCalc = new Calculator;

powerCalc.addMethod("**", (a, b) => a ** b); // works fine
powerCalc.addMethod("*", (a, b) => a * b);
powerCalc.addMethod("/", (a, b) => a / b);


let result = powerCalc.calculate("4 ** 3"); // 64, as should be(other values also supported)
console.log(result);

但是,如果我在最后一个代码序列中更改顺序,那么添加“ **”操作就不再是第一个,就像这样:

function Calculator() {
  this.supportedOperators = ['+', '-'];

  this.supportedMethods = [
    (a, b) => a + b,
    (a, b) => a - b
  ];

  this.calculate = (str) => {
    for (operator of this.supportedOperators) {
      if (str.includes(operator)) {
        let delimiter = operator.length; // for '**' operator
        let firstOperand = str.slice(0, str.indexOf(operator));
        let secondOperand = str.slice(str.indexOf(operator) + delimiter);

        return this.supportedMethods[this.supportedOperators.findIndex(item => item === operator)]
          (+firstOperand, +secondOperand);
        /* check the supported operators, then use it on operands
                           
                            A mess here, but i tried my best to make it more readable and understandable */

      } else console.log('Unsupported operation');
    }
  };

  this.addMethod = (operator, method) => {
    this.supportedOperators.push(operator);
    this.supportedMethods.push(method);
  }

}

let powerCalc = new Calculator;

powerCalc.addMethod("*", (a, b) => a * b); // 
powerCalc.addMethod("/", (a, b) => a / b); // those two work fine regardless of order
powerCalc.addMethod("**", (a, b) => a ** b); // changed the order, no works fine

let result = powerCalc.calculate("4 ** 3"); // throws NaN with any value
console.log(result);

幂运算现在返回NaN。

我不知所措。请帮助。

javascript arrays object
1个回答
0
投票

如上面的注释所述,问题在于***运算符都包含“ *”符号。提出的解决方案之一是实施sort(),以便将最长长度的运算符放在首位。

© www.soinside.com 2019 - 2024. All rights reserved.