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 函數 代替函數