EVOperating principle of the Al Function
EVThe Al function evaluates a givenBytesCodeAnd tries to execute the expressions contained in the string or a series of legal javasExplain statement. EVThe Al FunctionLastThe value or reference contained in an expression or statement as the return value.
Example
- EVAl evaluate javasLimit expression
VaRBar= 'Bar';
VaRFoobar= Eval('"Foo" + bar');
Alert(Foobar);
- EVAl evaluate javasExplain statement
VaR Bar = 'Bar' ;
// If variable bar equals 'bar', foobar is the result
// Last executing statement: bar = "foo-bar ";
VaR Foobar = Eval ( 'If (bar = "bar") {bar = "foo-bar";} else {bar = "Bar-foo ";}' );
Alert ( Foobar);
// Change the value
Bar = '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
var objectliteral = {
name : "objector. L " ,
age : "24" ,
special : "JavaScript" ,
sayname : function () {
return This . name ;
}
} ;
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, then the question arises: how to convert the JSON Data Built on the server to available javasUpload object. Using EVThe Al function is undoubtedly a simple and direct method. During conversion, enclose the JSON string with parentheses:
VaRJsonobject= Eval("(" +Jsonformat+ ")");
Why are Brackets added?
the purpose of adding parentheses is force EV Al function evaluates javas force the expression (expr ession) is converted to an object, instead of being executed as a statement . For example, if no outer brackets are added to the object literal, ev Al recognizes braces as the start and end mark of the javas quit code block. Then, {} is considered to have executed an empty statement. The following two execution results are different:
Alert(Eval("{}");// Return undefined
Alert(Eval("({})");// Return object [object]
Why?The JSON format name is enclosed in quotation marks.?
Because EVThe Al function interprets {FOO: "Bar"} as a legal javas.Explain statement, not an expression. But what people often want is to make EVAl interprets this code as an object. Therefore, the JSON format willAdd quotation marks and parentheses to the outer of the name, so that EVAl will not interpret JSON as a code block..
Example
- EVAl error parsing Semantics
Alert(Eval('{FOO: "bar "}'));// Return "bar", incorrect
- EVAl parses JSON correctly
alert ( eval ( '({"foo ": "bar"}) ' ); // return JSON object, correct