SoFunction
Updated on 2025-04-03

In-depth analysis and detailed explanation of the role of eval

"JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of ECMAScript. Because it uses a language-independent text format and uses habits similar to the C language family, it has these features that make JSON an ideal data exchange language, which is easy to read and write, and is also easy to machine parse and generate (usually used to improve network transmission rates)."

Today, I would like to briefly talk about the() and () functions in jquery, and by the way, I will also mention the eval() function in native JS

(1) Function

Function: Convert JavaScript object notation (JSON) string to an object.

Syntax: (text [, reviver])

parameter:

text is required. A valid JSON string.

Reviver is optional. A function that converts the result. This function will be called for each member of the object.
Return value: an object or array

example:

var json = '{"name":"GDT","age":,"University":"GDUT"}';
var info = (json);//Resolve to JSON object( + ' is a student of ' +  + ' and he is ' +  + " years old."); /infoforObjectObject

(2)() function

Function: Convert JavaScript values ​​to JavaScript Object Notation (JSON) strings

Syntax: ( value [, replacer] [, space])

parameter:

The value is required, usually the JavaScript value that needs to be converted (usually an object or an array)

replacer is optional, a function or array used to convert results

Space is optional. Add indents, spaces, and line breaks to the return value JSON text to make it easier to read.

Return value: A string containing JSON text

example:

var info = {name:"GDT",age:,University:"GDUT"};
var json = (info); //Convert to JSON string(json); //outputfor{"name":"GDT","age":23,"University":"GDUT"}

(3) eval() function

Function: The eval() function can calculate a string and execute the JavaScript code in it.

Syntax: eval(string)

parameter:

string is required, a string to be calculated, containing the JavaScript expression to be calculated or the statement to be executed.

Return value: Return the value of the calculated string, if there is no return (return without any changes)

example:

eval("x=;y=;(x*y)"); //output is(eval("+"));//output isvar x=;
(eval(x+));//output is

Use the eval() function to parse JSON strings into objects. This function can complete the function of (), but there are differences, please see the following code

// ()
var json = '{"name":"GDT","age":,"University":"GDUT"}';
var info = (json);  //Resolve to JSON object(info); //output is [object Object]//eval()
var json = '{"name":"GDT","age":,"University":"GDUT"}';
var info = eval('(' + json + ')'); //Resolve to JSON object(info); //outputfor[object Object]

I don't know if you have noticed that eval() also needs to wrap the string in a pair of parentheses. I've found a better explanation for this:

Reason: It comes down to the problem of eval itself. Since json starts and ends in the "{}" way, in JS, it will be processed as a statement block, so it must be converted into an expression in a mandatory manner.

Solution: The purpose of adding parentheses is to force the eval function to force the expression in brackets to be converted into objects when processing JavaScript code, rather than executed as statements. For example, for example, if the object literal {} is not added with outer brackets, then eval will recognize the braces as the start and end marks of the JavaScript code block, and then {} will be considered to be executing an empty statement. Please see the difference of the following examples

alert(eval("{}")); // return undefined
alert(eval('('+'{}'+')')); // return object[Object] 

In addition, compared with () with strict writing format, eval() can parse any string. eval is not safe because eval is relatively loose and there will be potential security problems. For example, the following code:

var str = '{"a":"b"}';
(eval("("+str+")")); //Resolve normally as an objectvar str = '{"a": (function(){alert("I can do something bad!");})()}';
eval('('+str+')'); //Can be used to execute * scripts 

If a malicious user injects a script to insert a * link into the page into the json string, it can be operated with eval, and you don’t have to worry about this problem with (). It can be seen that although eval() is very powerful, there are not many opportunities to use it.

The time for personal summary is here. This is my first blog in my life. Fool's Day was born on April 1st. I hope you all forgive me for the poor writing. The technology is very bad now. I really hope that I can accumulate knowledge bit by bit now and lay a good foundation for future success. Fighting~