This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13991|Reply: 0

[JavaScript] In JS, the EVAL method is used to obtain the variable value or object attribute of the dynamically generated name

[Copy link]
Posted on 12/15/2017 10:23:56 AM | | | |

    In a JS program, sometimes when we want to get a variable value or an object's property, and the variable or object name may be dynamically generated, for example:


  At this time, we need to use the eval method, and the code modification is as follows:




The detailed explanation of eval is as follows:
The eval function takes a parameter s and returns s directly if s is not a string. Otherwise, the S statement is executed. If the result of the S statement execution is a value, it returns this value, otherwise it returns undefined.
It should be noted that the object declaration syntax "{}" does not return a value, it needs to be enclosed in parentheses to return a value, a simple example is as follows


It can be seen that for object declaration statements, it is only execution and does not return a value. In order to return a commonly used object declaration statement such as "{}", it must be enclosed in parentheses to convert it to an expression in order to return its value. This is also one of the basic principles of using JSON for Ajax development. In the example, it can be clearly seen that the second alert statement outputs undefined, while the third output with parentheses is the object represented by the statement.




Previous:mysqldump imports and exports database summaries
Next:AES, DES, MD5, RSA, SHA encryption interface
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com