2015-9-15 16:26:05
I've seen this code these days:
var tojson=function(A) { try { ////run return in the body (return a); return a. return (new Function ("return" + A )) (); Catch (e) { return ""; } };
I don't know what that means. Asked a colleague later said that the string into JSON, or do not understand what the principle is. So Baidu, saw such a question and answer: (New Function ("return" + str)); Solve why it is wide to convert a JSON string to JSON. The JS master know the reason, pointing out, got a day did not understand why.
Answer: The new function () receives the string and converts it into a function. When converting "return" + str into a function, Str is converted to an object (not converted to JSON) and then used (function () {return obj;}) () method immediately executes the function generated by the new function (), which has only one return obj, and the result is the object that the STR string is converted from.
(New Function ("return" + JSON)) ();