On the JavaScript eval () function

Source: Internet
Author: User
Tags string format javascript eval

People with JS should know the eval () function, although the function is very small, but it is powerful, then the question, why not used? The reason is simple, because the eval () function is the dynamic execution of the string in which there may be a script, then it is possible to raise the security problems of the system, so you can not use it, but at least to know its usage.

The role of the Eval () function is simply to execute the string in parentheses as code, for example, eval ("2+3"), which returns 5. It is important to note that the method is only subject to the original string parameter, and if not, it will not return anything as it is, so it is useless for you to pass an object to it.

Let's take a look at the following two examples:

<script type= "Text/javascript" >eval ("X=10;y=20;document.write (x*y)") document.write (eval ("+ +)") var x= 10document.write (eval (x+17)) </script>

After this code executes, it will output:

200

4

27

It seems to be very simple, but just so it seems that it is not useful, it seems to be used for funny, then generally used it to do? Yes, if you know json, you know it can be used to convert JSON from a string format to a JSON object, and in an asynchronous request, if the server returns a JSON string format, you won't be able to use it directly, so turn it into a JSON object format and then combine the for In the loop, you can go through it, extract the information you need, then how to use it, and see an example:

For example, this is the server back to the JSON string, then how to deal with it, very simple:

var dataobj=eval ("(" +data+ ")");

That's OK, isn't it simple, what? You ask why parentheses, which is also very simple, because if you do not add parentheses, the Eval function will be in the braces in the JSON as a statement block processing, then it changed, you know, such as your girlfriend or boyfriend changed, you will be sad, so to take a way to stop it, is to add a parenthesis, Forced to convert it to an object to handle, avoid as a statement block to execute, such as:

Although this is true, if something is too strong it will be dangerous, so it is not good, then how to solve the problem of processing JSON string, this is certainly not a problem, there are two ways:

One is to use the function object to complete, its typical application is in jquery in the Ajax method of success and so on for the return data of the parsing:

At this point, data is a JSON object.

Another way is to use the library to solve, commonly used is jquery,jquery encapsulated in a very perfect Ajax method, as long as the return format is set correctly, you do not have to convert, such as:

It is not felt that the library world has become beautiful, but original aim, and then how to learn the original things first, so use the library to be comfortable, well, the Eval function said so much, of course, to say, the above examples are reference online resources, if you have seen, right when you look again.

On the JavaScript eval () function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.