In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use the eval method in nodejs". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the eval method in nodejs".
This tutorial operating environment: windows10 system, nodejs version 12.19.0, DELL G3 computer.
How to use eval method in nodejs
The eval () function evaluates the JavaScript string and executes it as script code.
If the parameter is an expression, the eval () function executes the expression. If the argument is a Javascript statement, eval () executes the Javascript statement.
Grammar
Eval (string)
Parameter description
String is required. The string to evaluate that contains the JavaScript expression to evaluate or the statement to execute.
The parameter form of the eval () method
If the parameter s is not a string, but an integer or Function type, the integer or Function is returned directly
Take a chestnut.
Example 1:
Var x = 1; eval (x); / / eval () method returns 1var z = eval (1) / / eval () method returns 1
Example 2:
Var f = eval (function () {console.log ("1111"); return 1}); / * eval () method returns: function () {console.log ("1111"); return 1;} method * / var s = f (); / / execute method slog1, print log "1111" / * > * / var ff = function () {console.log ("1111") Return 1;}; var f = eval (ff); / / eval () method returns ff method var s = ff (); / / execute ff method swatches 1, print log "1111" / * * > * * / function ff () {console.log ("1111"); return 1;}; var f = eval (ff); / / eval () method returns ff method var s = ff (); / / execute ff method swatches 1, print log "1111"
If the parameter s is a string and there is an expression in the string, the eval () method evaluates the expression in the string and returns the result of the expression evaluation.
Take a chestnut.
Eval ("'true'"); / / return the string "true" eval ("ture"); / / return the trueeval of type Boolean ("1"); / / return the integer 1eval ("[0LJ 1J 2]"); / / return the array [0J 1J 2] eval ("2Q 2"); / / return the integer 4eval ("2 minutes 2'"); / / return the string "22"
If the parameter s is a string and the expression in the string is in JSON format (at this time the JOSN format is surrounded by a pair of parentheses around the JSON), the eval () method returns the JSON.
Take a chestnut.
Var j = "{'aa':11,'bb':22}"; eval ("(" + j+ ")"); / / return {"aa": 11, "bb": 22} eval ("({' aa':11,'bb':22})"); / / return {"aa": 11, "bb": 22} eval ("({aa:11,bb:22})") / the format {aa:11,bb:22} in js will be converted to the format eval ("{'aa':11,'bb':22}") of {' aa':11,'bb':22} when parsing; / / an exception will be thrown
Notes
For the eval () method, the JSON format mentioned above is also a kind of expression. But if there are no parentheses in the JSON format, the eval () method will not return the object.
For example, in the format eval ("{'aa':11,'bb':22}"), {' aa':11,'bb':22} is not returned.
Because the eval () method treats square brackets "{}" as a block of statements. Then the eval () method only executes the statement and does not return the value (as a result, an exception is thrown).
The correct way to write it is to add parentheses "()" to the JSON format to cast its type to JSON format (expression) and return the JSON value. This is the same as the instant function (function () {}) (), which casts the function into an expression and returns.
If the parameter s is a string and there is a statement or statement block in the string, the eval () method executes the statement or statement block and returns undefined
Take a chestnut.
Eval ("var xeroids 1mm;"); / / return undefinedconsole.log (x); / / print the string 1qpxx * > * * / eval ("var xeroids 1 * / eval;"); / / return undefinedconsole.log (z + "); / / print the string 3max strings * > * / s = eval (" function t () {alert (3)}; t (); "); / / execute the definition and call the method of t: pop up 3. And return undefinedconsole.log (s); / / print undefined/** > * / eval ("var j = {sex:' male', age:27}; alert (j.age);"); / * the json object here is not enclosed in parentheses "()". Because it executes the statement, not evaluates the expression * /
Notes
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 the json in the expression is enclosed is that eval () evaluates the expression and returns the result. On the other hand, eval () only executes the statement and does not care about the return value (because what is returned is (undefined)
Thank you for reading, the above is the content of "how to use the eval method in nodejs". After the study of this article, I believe you have a deeper understanding of how to use the eval method in nodejs, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.