How eval functions work

Source: Internet
Author: User

The eval function evaluates a given string containing JavaScript code and tries to execute expressions contained in the string or a series of legal JavaScript statements. The eval function uses the value or reference contained in the last expression or statement as the return value.
Example
Eval evaluates JavaScript expressions var bar = 'bar'; var foobar = eval ('"foo" + bar'); alert (foobar ); eval evaluates JavaScript statements var bar = 'bar'; // if variable bar equals 'bar', foobar is the result of // last executing statement: bar = "foo-bar "; var foobar = eval ('If (bar = "bar") {bar = "foo-bar";} else {bar = "bar-foo ";}'); alert (foobar); // change the valuebar = 'foo'; // now our the last executed statement is: bar = "bar-foo "; // therefore the value of variable foobar has been changed // into 'bar-foo' foobar = eval ('If (bar = "bar ") {bar = "foo-bar";} else {bar = "bar-foo";} '); alert (foobar); JSON format
JSON format is composed of braces and name-value pairs consisting of colons. Note the difference between JSON format and object literal (object literals): The JSON name is strictly represented by quotation marks + name.
Example
Object literal var objectLiteral = {name: "Objector. L ", age:" 24 ", special:" JavaScript ", sayName: function () {return this. name ;}; JSON object var jsonFormat = {"summary": "Blogs", "blogrolls": [{"title": "Explore JavaScript", "link ": "http://example.com/" },{ "title": "Explore JavaScript", "link": "http://example.com/"}]}; eval and JSON
Due to the rise of Ajax, the lightweight JSON data format is gradually becoming popular as the transmission format between the client and the server, the problem is how to convert the JSON Data Built on the server to available JavaScript objects. Using eval functions is undoubtedly a simple and direct method. During conversion, enclose the JSON string with parentheses:
Var jsonObject = eval ("(" + jsonFormat + ")"); why is brackets required?
The purpose of parentheses is to force the eval function to forcibly convert the expressions in parentheses into objects when evaluating JavaScript code, rather than executing them as statements. For example, if no outer brackets are added to the object literal {}, eval identifies the braces as the start and end mark of the JavaScript code block, then {} is considered to have executed an empty statement. The following two execution results are different:
Alert (eval ("{}"); // return undefinedalert (eval ("({})"); // return object [Object] Why is the JSON format name enclosed in quotation marks?
Because the eval function interprets {foo: "bar"} as a legal JavaScript statement, not an expression. But what people often want is to let eval interpret this code as an object. Therefore, the JSON format will force you to add quotation marks and parentheses on the outer side of the name, so that eval will not incorrectly interpret JSON as a code block.
Example
Eval error parsing semantics alert (eval ('{foo: "bar"}'); // return "bar ", incorrecteval correctly parses JSONalert (eval ('({"foo": "bar"})'); // return JSON object, correct conclusion
Understand the working principle of eval and the strict format of json, and apply eval and json to JavaScript data transmission and object conversion.
Following this format:
Eval ('{' + jsonString + ')');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.