How to use the eval method in nodejs

WBOY
Release: 2022-01-10 11:28:33
Original
4791 people have browsed it

In nodejs, the eval() method is used to calculate a string and execute it as script code. The syntax is "eval(string)"; if the parameter is not a string, but an integer or Function type, the integer or Function is returned directly.

How to use the eval method in nodejs

The operating environment of this tutorial: windows10 system, nodejs version 12.19.0, DELL G3 computer.

How to use the eval method in nodejs

The eval() function calculates a JavaScript string and executes it as script code.

If the parameter is an expression, the eval() function will execute the expression. If the argument is a Javascript statement, eval() will execute the Javascript statement.

Syntax

eval(string)
Copy after login

Parameter Description

string Required. A string to evaluate that contains a JavaScript expression to evaluate or a statement to execute.

Parameter form of eval() method

If parameter s is not a string, but an integer or Function type, the integer or Function will be returned directly

For example

Example 1:

var x = 1;
var y = eval(x);//eval()方法返回1
var z = eval(1)//eval()方法返回1
Copy after login

Example 2:

var f = eval(
          function(){
            console.log("1111");
            return 1
           }
        );
/*
eval()方法返回:
    function(){console.log("1111");return 1;}方法
*/
var s = f();//执行方法  s=1,打印日志"1111"
/**>>>>>>>>>>>>>>>**/
var ff = function(){
   console.log("1111");
   return 1;
};
var f = eval(ff);//eval()方法返回ff方法
var s = ff();//执行ff方法  s=1,打印日志"1111"
/**>>>>>>>>>>>>>>>**/
function ff(){
   console.log("1111");
   return 1;
};
var f = eval(ff);//eval()方法返回ff方法
var s = ff();//执行ff方法  s=1,打印日志"1111"
Copy after login

If the parameter s is a string and the string is an expression, the eval() method will calculate the string An expression in returns the result of the expression's evaluation.

For example

eval("'true'");//返回字符串"true"
eval("ture");//返回Boolean类型的true
eval("1");//返回整数1
eval("[0,1,2]");//返回数组[0,1,2]
eval("2+2");//返回整数4
eval("2+'2'");//返回字符串"22"
Copy after login

If parameter s is a string, and the expression in the string is in JSON format (at this time, a pair of parentheses must be added outside the JSON format to surround the JSON up), the eval() method returns the JSON.

For example

var j = "{'aa':11,'bb':22}";
eval("("+j+")");
//返回{"aa":11,"bb":22}
eval("({'aa':11,'bb':22})");
//返回{"aa":11,"bb":22}
eval("({aa:11,bb:22})");
//js中{aa:11,bb:22}这种格式在解析时会转换成{'aa':11,'bb':22}这种格式
eval("{'aa':11,'bb':22}");
//抛出异常
Copy after login

Note

For the eval() method, the JSON format mentioned above is also a type of expression. But if there are no parentheses in the JSON format, the eval() method will not return the object.

For example: eval(“{‘aa’:11,’bb’:22}”) will not return {‘aa’:11,’bb’:22}.

Because the eval() method treats the square brackets "{}" as a statement block. Then the eval() method will only execute the statement and will not return the value (the result is an exception).

The correct way to write it is: add parentheses "()" outside the JSON format to force type conversion into JSON format (expression) and return the JSON value. This is the same as the immediate function (function(){})(). The function of () is to force the function into an expression and return it.

If the parameter s is a string, and the string is a statement or statement block, the eval() method will execute the statement or statement block and return undefined

For example

eval("var x='1';");
//返回undefined
console.log(x);
//打印字符串1
/**>>>>>>>>>>>>>>>**/
eval("var x=1;var y=2;var z=x+y;");
//返回undefined
console.log(z+"");
//打印字符串3
/**>>>>>>>>>>>>>>>**/
s = eval("function t(){alert(3)};t();");
//执行定义并调用t的方法:弹出3.并且返回undefined
console.log(s);
//打印undefined
/**>>>>>>>>>>>>>>>**/
eval("var j={sex:'男',age:27};alert(j.age);");
/*
此处的json对象不用小括号"()"括起来。因为该处是执行语句,而非计算表达式
*/
Copy after login

Note

In the eval() method, if the json format exists in the statement, then the json format does not need to be enclosed in parentheses "()". The reason why the json in the expression is enclosed is because eval() evaluates the expression and returns the result. And eval() only executes the statement and does not calculate the return value (because all returned are (undefined))

For more node-related knowledge, please visit: nodejs tutorial!!

The above is the detailed content of How to use the eval method in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!