<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函数 - 将字符串参数转换为运算符

        Javascript function - converting string argument to operator(Javascript函数 - 将字符串参数转换为运算符)

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

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                  AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                  Checking Value of Radio Button Group via JavaScript?(通过 JavaScript 检查单选按钮组的值?)
                  <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'>

                        <tbody id='4Vgr4'></tbody>

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