Introduction to eval
---Eval is a method of global object prototype in ECMA definition;
--- The parameters accepted by eval are string format js code, which will be executed by the execution engine (remember 'advanced programming' or something, create a new execution engine at this time) and then return the result to the location of the eval call.
<!DOCTYPE html> <html> <head> <title>evalstudy</title> <script type="text/javascript"> /* eval("expression"); execute expression statement eval("("+javascript type+")"); converted to javascript object */ var jsonObj={"name":"ljl","data":123};//json, is a javascript objectvar jsonString='{"name":"ljl","data":123}';//Javascript's string type, string content conforms to the style of json formatvar objType=eval("("+jsonString+")");//Convert json characters into javascript object through eval functionalert( typeof jsonString);//string alert( typeof objType);//obj alert(eval(123));//123 alert(typeof eval("("+123+")"));//number var x=2; var y=eval('x+1');//Execute 2+1 expressionalert('y= '+y);//3 </script> </head> <body> </body> </html>
Supplement: Summary
eval is one of the dynamic features of js. Through it, it directly executes the js program and returns the result. The common use is to restore json data to js objects;
However, because it can dynamically change the context object at runtime, it brings the risk of injection attacks;
When using it, pay attention to the syntax of eval's strings. The common problem is the 'brace' problem