eval 函数 代替函数
eval()函数 (eval() function)
eval() function is a predefined global function in JavaScript and it is used to evaluate (execute) an expression, which is passed to the function as a parameter. It can also evaluate any JavaScript code.
eval()函数是JavaScript中预定义的全局函数,用于评估(执行)表达式,该表达式作为参数传递给该函数。 它还可以评估任何JavaScript代码。
Syntax:
句法:
eval(expression);
Here, expression is the JavaScript code or any expression having values or variables.
此处, 表达式是JavaScript代码或具有值或变量的任何表达式。
Example:
例:
<html>
<head>
<title>JavaScipt Example</title>
</head>
<body>
<script>
var num1 = 100;
var num2 = 200;
var num3 = eval("num1 + num2"); //add num1 and num2
var num4 = eval("num3 + 100"); //add num3 and 100
var str = eval("num4.toString(16)"); //convert num4 value to hex
document.write("num3: " + num3 + "<br>");
document.write("num4: " + num4 + "<br>");
document.write("str: " + str + "<br>");
</script>
</body>
</html>
Output
输出量
num3: 300
num4: 400
str: 190
翻译自: https://www.includehelp.com/code-snippets/eval-function-with-example-in-javascript.aspx
eval 函数 代替函数