JavaScript eval () function definition and usage
The eval () function computes a string and executes the JavaScript code in it.
Grammar
Eval (String)
Parameters |
Description |
String |
Necessary. The string to evaluate that contains the JAVASCRIPT expression to be evaluated or the statement to execute. |
return value
The value (if any) that is obtained by calculating the string.
Description
The method only accepts the original string as an argument, and if the string parameter is not the original string, the method returns without any change. Therefore, do not pass a String object as a parameter for the eval () function.
If you attempt to override the Eval property or assign the eval () method to another property and call it through the property, the ECMAScript implementation allows a Evalerror exception to be thrown.
Thrown
Throws an SyntaxError exception if there are no valid expressions and statements in the argument.
If Eval () is called illegally, a Evalerror exception is thrown.
If the Javascript code passed to eval () generates an exception, eval () passes the exception to the caller.
Example 1
In this example, we will use Eval () on a few strings and see the results returned:
<Scripttype= "Text/javascript">Eval ("x=10;y=20;document.write (x*y)") document.write (eval (" the"))varx=Tendocument.write (eval (x+ -))</Script>
Output:
200427
Example 2
Take a look at the result of the Eval () return in other cases:
Eval ("2+3") //return 5var myeval = eval; Evalerror exception Myeval ("2+3") may be thrown; Evalerror exceptions may be thrown
The following code can be used to detect whether the parameters of eval () are legitimate:
Try { alert ("Result:" + eval (Prompt ("Enter An expression:", ""))); } catch (Exception) { alert (exception); }
------------------------------------------------eval () Parse the JSON string as a function----------------------------------------------------
1.
var text = ' {' "name": "Runoob", "Alexa": "function () {return 10000;}", "Site": "www.runoob.com"} '; var obj = json.parse (text) ; obj.alexa = eval ("(" + Obj.alexa + ")"); document.getElementById ("Demo"). InnerHTML = Obj.name + "Alexa rank:" + obj.alexa ();
Above is the Remove function () {return 10000;}
Then through eval () to function
Why to Eval here to add "("("+data+");//"?
The reason is that eval itself is a problem. Since JSON starts and ends in the form of "{}", in JS, it is treated as a block of statements, so it must be coerced into an expression.
The purpose of the parentheses is to force the Eval function to force the expression in parentheses to be converted to an object while processing the JavaScript code, rather than being executed as a statement (statement) . For example, if the object literal {} is not enclosed, then eval will recognize the curly brace as the start and end tag of the JavaScript block, and {} will be considered an empty statement. So the following two execution results are different:
Alert (eval ("{}");//return undefined
Alert (eval ("({})");//return Object[object]
For this kind of writing, in JS, you can see everywhere.
such as: (function ()) {} (); Do the closure operation and so on.
alert (dataObj.root.length);//number of sub-objects of output root
$.each (Dataobj.root,fucntion (Idx,item) {
if (idx==0) {
return true;
}
Output the name and value of each root sub-object
Alert ("Name:" +item.name+ ", Value:" +item.value ");
})
Note: For general JS-generated JSON objects, you only need to replace the $.each () method with the For statement, and the other is unchanged.
2. For the JSON string returned by the server, if the jquery asynchronous request sets the type (typically this configuration property) to "JSON", or uses the$.getjson ()method to get the server back, then the eval () method is not required, because the result is already a JSON object, just call the object directly, here the $.getjson method as an example to illustrate the data processing method:
$.getjson ("http://www.phpzixue.cn/", {param: "Gaoyusi"},function (data) {
The data returned here is already a JSON object
The following other actions are the same as the first case
$.each (Data.root,function (Idx,item) {
if (idx==0) {
Return true;//with Countinue, returns false with break
}
Alert ("Name:" +item.name+ ", Value:" +item.value ");
});
});
In particular, it is important to note that the eval () method in mode 1 is the dynamic execution of strings (possibly JS scripts), which can easily cause system security problems. So you can use some third-party client script libraries that circumvent eval (), such as JSON in JavaScript, which provides a script library of no more than 3k.
JS Global function Eval Parse JSON string function