<tfoot id='IFUR3'></tfoot>
  • <small id='IFUR3'></small><noframes id='IFUR3'>

        <bdo id='IFUR3'></bdo><ul id='IFUR3'></ul>
        <legend id='IFUR3'><style id='IFUR3'><dir id='IFUR3'><q id='IFUR3'></q></dir></style></legend>
        <i id='IFUR3'><tr id='IFUR3'><dt id='IFUR3'><q id='IFUR3'><span id='IFUR3'><b id='IFUR3'><form id='IFUR3'><ins id='IFUR3'></ins><ul id='IFUR3'></ul><sub id='IFUR3'></sub></form><legend id='IFUR3'></legend><bdo id='IFUR3'><pre id='IFUR3'><center id='IFUR3'></center></pre></bdo></b><th id='IFUR3'></th></span></q></dt></tr></i><div id='IFUR3'><tfoot id='IFUR3'></tfoot><dl id='IFUR3'><fieldset id='IFUR3'></fieldset></dl></div>

      1. Javascript函数 - 将字符串参数转换为运算符

        时间:2023-10-21

            <legend id='9VKo4'><style id='9VKo4'><dir id='9VKo4'><q id='9VKo4'></q></dir></style></legend>
            • <bdo id='9VKo4'></bdo><ul id='9VKo4'></ul>

                <small id='9VKo4'></small><noframes id='9VKo4'>

                    <tbody id='9VKo4'></tbody>
                1. <i id='9VKo4'><tr id='9VKo4'><dt id='9VKo4'><q id='9VKo4'><span id='9VKo4'><b id='9VKo4'><form id='9VKo4'><ins id='9VKo4'></ins><ul id='9VKo4'></ul><sub id='9VKo4'></sub></form><legend id='9VKo4'></legend><bdo id='9VKo4'><pre id='9VKo4'><center id='9VKo4'></center></pre></bdo></b><th id='9VKo4'></th></span></q></dt></tr></i><div id='9VKo4'><tfoot id='9VKo4'></tfoot><dl id='9VKo4'><fieldset id='9VKo4'></fieldset></dl></div>
                  <tfoot id='9VKo4'></tfoot>
                  本文介绍了Javascript函数 - 将字符串参数转换为运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  抱歉,如果我的问题不清楚,不知道如何措辞!

                  我正在尝试创建一个函数,该函数接受两个数字和一个包含运算符(例如'+'、'-'、'*'、'/')的字符串.

                  I'm trying to create a function that takes two numbers and a string which contains an operator (e.g. '+', '-', '*', '/').

                  我在字符串上使用了 .valueOf() 来提取运算符,但是 num1 和 num2 参数似乎没有计算为传递的数字参数.为什么会这样?

                  I've used .valueOf() on the string to extract the operator, however the num1 and num2 arguments do not seem to evaluate to the passed number parameters. Why is this happening?

                  function calculate(num1, operator, num2) {
                    return `num1 ${operator.valueOf()} num2`;
                  }
                  undefined
                  
                  
                  calculate(2, '+', 1);
                  "num1 + num2"         //result
                  

                  推荐答案

                  如果我理解您的要求,您可以使用 eval() 来实现:

                  If I understand your requirements, you could use eval() to achieve this:

                  function calculate(num1, operator, num2) {
                    return eval(`${num1} ${operator} ${num2}`);
                  }
                  
                  console.log(calculate(2, '+', 1)); // 3
                  

                  或者,您可以通过使用开关块来避免使用 eval(),它 将使您的代码更易于调试并且可能更安全:

                  Alternatively, you could avoid the use of eval() by using a switch block, which would make your code easier to debug and potentially more secure:

                  function calculate(num1, operator, num2) {
                    switch (operator.trim()) { // Trim possible white spaces to improve reliability
                      case '+':
                        return num1 + num2
                      case '-':
                        return num1 - num2
                      case '/':
                        return num1 / num2
                      case '*':
                        return num1 * num2
                    }
                  }
                  
                  console.log(calculate(2, '+', 1)); // 3
                  

                  这篇关于Javascript函数 - 将字符串参数转换为运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:为什么`const arg = arg;`会产生“初始化前无法访问"?错误? 下一篇:如何获取选中的单选按钮的值?

                  相关文章

                  <tfoot id='4Vgr4'></tfoot>
                2. <legend id='4Vgr4'><style id='4Vgr4'><dir id='4Vgr4'><q id='4Vgr4'></q></dir></style></legend>
                  1. <i id='4Vgr4'><tr id='4Vgr4'><dt id='4Vgr4'><q id='4Vgr4'><span id='4Vgr4'><b id='4Vgr4'><form id='4Vgr4'><ins id='4Vgr4'></ins><ul id='4Vgr4'></ul><sub id='4Vgr4'></sub></form><legend id='4Vgr4'></legend><bdo id='4Vgr4'><pre id='4Vgr4'><center id='4Vgr4'></center></pre></bdo></b><th id='4Vgr4'></th></span></q></dt></tr></i><div id='4Vgr4'><tfoot id='4Vgr4'></tfoot><dl id='4Vgr4'><fieldset id='4Vgr4'></fieldset></dl></div>

                    <small id='4Vgr4'></small><noframes id='4Vgr4'>

                    • <bdo id='4Vgr4'></bdo><ul id='4Vgr4'></ul>