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>eval learning</title><script type="text/javascript">/*eval("expression"); execute expression statement eval("("+javascript type+")"); convert to javascript object*/var jsonObj={"name":"ljl","data":123};//json, is the object of javascript jsonString='{"name":"ljl","data":123}';//javascript string type, string content conforms to the style of json format var objType=eval("("+jsonString+")");//Convert json characters into javascript object alert( typeof jsonString);//stringalert( typeof objType);//objalert( eval(123));//123alert(typeof eval("("+123+")"));//numbervar x=2;var y=eval('x+1');//Execute 2+1 expression alert('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 results. 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