如何制作像 calculate(x, y, operator) 这样的函数?

How to make a function like calculate(x, y, operator)?

如何实现这种功能?如果我像 calculate(2, 4, '*') 这样称呼它,它会 return 给我一个字符串 '2*4'。我也试过 parseInt 但没有成功,它只会让第一次出现在 String to Number.

function calculate(x, y, operation) {
  this.number = x + operation + y;
  return this.number;
}

这是一个基本的计算器,您已经编写了所有代码。只是缺少评估。

注意:请注意,对于简单的计算器,可以使用eval。如果您出于安全原因不在某些关键项目等中实现它,但如果它用于某些内部使用或用于分配,它还不错并且非常方便。

function calculator(x,y,operation){
  this.number=x+operation+y;
  return eval(this.number);
}
console.log(calculator(1,2,"+"));
console.log(calculator(1,2,"-"));
console.log(calculator(1,2,"*"));
console.log(calculator(1,2,"/"));

只要用想要的运算符创建一个对象,测试运算符是否存在,然后使用它。

var operators = {
        '+': function (a, b) { return a + b; },
        '-': function (a, b) { return a - b; },
        '*': function (a, b) { return a * b; },
        '/': function (a, b) { return a / b; }
    },
    calculate = function (val1, val2, sign) {
        if (sign in operators) {
            return operators[sign](val1, val2);
        }
    }

console.log(calculate(6, 7, '*'));

您的 operation 参数只是一个字符串,不是真正的运算符。这就是为什么它不能如您所愿地工作。所以,你有一些选择。

使用评估

您可以使用 eval 函数动态地 运行 一个字符串作为一段代码,这样:

function calculate(x, y, operation) {
  return eval(x + operation + y)
}

虽然它看起来如此简洁明了,但请记住,这通常是一种不好的做法,因为安全可维护性性能原因(more about it here)。


使用条件

有时最简单的想法是最好的选择:只需使用一些条件。

function calculate(x, y, operation) {
  if (operation === '+') return x + y
  else if (operation === '-') return x - y
  else if (operation === '*') return x * y
  else if (operation === '/') return x / y
  else if (operation === '%') return x % y
  else if (operation === '**') return x ** y
  else return NaN
}

这似乎有点重复,但请记住,您不需要处理很多算术运算。其实上面已经处理了all the binary arithmetic operators

另一种语法是 switch case 语句,但它只是一个更冗长的条件结构,ideia 保持不变。


使用带有函数的对象

另一种方法,受到 的强烈启发,是在对象的帮助下将运算符的字符串表示与其等效函数映射:

function calculate(x, y, operation) {
  var operators = {
    '+': function (a, b) { return a + b },
    '-': function (a, b) { return a - b },
    '*': function (a, b) { return a * b },
    '/': function (a, b) { return a / b },
    '%': function (a, b) { return a % b },
    '**': function (a, b) { return a ** b }
  }

  return operation in operators ? operators[operation](x, y) : NaN
}

如果在ES2015+环境下(see the compatibility),箭头函数可以很优雅:

function calculate(x, y, operation) {
  const operators = {
    '+': (a, b) => a + b,
    '-': (a, b) => a - b,
    '*': (a, b) => a * b,
    '/': (a, b) => a / b,
    '%': (a, b) => a % b,
    '**': (a, b) => a ** b
  }

  return operation in operators ? operators[operation](x, y) : NaN
}

虽然我个人仍然认为简单的条件语句足以满足这种情况,但这种对象方法非常有趣,可以显示 JavaScript 的灵​​活性。

你应该把它分成 4 种不同的情况:

calculate: function(x,y,operation){
      switch(operation){
         case "*":
           this.number = x*y;
           break;
         case "/":
           this.number=x/y;
           break;
         case "+":
           this.number= x+y;
           break;
         case "-":
           this.number= x-y;
           break;
         default:
            break;
      }

        return this.number;
}

扩展它也很容易...