The use of the Eval function in Ajax

Source: Internet
Author: User
Tags try catch

The definition and use of eval:

Eval It is used to calculate a string and execute the JavaScript code within it.

Grammar:

1) The Eval function accepts a string, and this parameter is required, which is the string to be computed. It can have a function containing JavaScript expressions, or statements to be executed.

Eval (String).

2) Then, since this is a function, it is possible to have a return value. Its return value is: If the string parameter has a return value, then the function returns the result of the string execution.

3) There is one place to note that this eval function only accepts the original string as a parameter. If the string parameter is not the original string, then the method will not make any changes to the return. Therefore, we do not pass some string objects as arguments for this function. Also, if we try to override the Eval property and assign the Eval function to another property, some exceptions may be thrown.

4) and, if there are no valid expressions or statements in this parameter, it is possible to throw a syntax error exception.

As you can see, our eval function is very powerful. Therefore, we should pay more attention to its legitimacy and rationality in practical application.

Instance:

Eval ("X=10;y=20;document.write (X*y)");

I passed it into some execution statement inside.

1) I first assign x=10, then y=20.

2) Then I'm going to do a multiplication for x and Y.

And then output their results.

Then our eval function will use these 3 statements as arguments for execution.

Output 200

See our X * y, which is 10 * 20, does equal 200.

Here are our multiple expression statements.

2. See another example: document.write (eval ("+ +"));

1) Inside we directly execute the performance, then it should be the result of 4.

2) then this eval function returns the output of this 4 to document.write, which should be output to 4.

Refresh the browser, and indeed get the result for 4.

3. Make a simple example again

1) For example, I define an X variable like this.

var x = 10;

2) then I output it in the inside with a variable to do a calculation.

document.write (eval (x+7));

3) We look at it is not returning the result we want, should be returning 17,

In fact, it is here that X is 10,10 plus 7 equals 17, then eval finishes only returns 17, printed by document.write on the page.

This is our use of the Eval function under the string

Applications in other situations

What is the result of its return?

1. For example, this: eval ("2+3");

We are directly passing a 2 + 3 inside, it should be executed, after execution it will be returned 5.

But we do not have any output here, so we should not see this effect, it is it will still be normal execution.

2. Let's assign this eval function to this variable :

var myeval = eval;

This can be thrown out of the ordinary, so we do not recommend this use in the actual project. Although we may not throw an exception in some browsers.

It is executed with the following code:

var x = 10;

document.write (eval (x+7));

Eval ("2+3");

var myeval = eval;

try {

Alert ("Result:" + eval (Prompt ("Enter An expression", "")));

} catch (e) {

Alert (e);

}

2) We have this refresh, it does not throw an exception, so it should be in the Chrome browser can be normal execution.

3) Let's take a look at this code, which is used to detect if the eval parameter is legal.

try {

Alert ("Result:" + eval (Prompt ("Enter An expression", "")));

} catch (e) {

Alert (e);

}

A) First use a try catch to wrap it up.

b) Then an alert function is executed, and the alert function is the result of the string + the return value of the Eval function.

c) eval inside execution, pop up a box, is an interactive dialog box for the user to enter an expression.

It then returns as the result of the execution.

For example, I enter 2+3, and after the Eval function executes this 2+3, it should return 5, return 5, and then pass the string to the alert function, so it pops up a 5.

How the Eval function parses a JSON-formatted string

1. What is a JSON-formatted string?


JSON is a lightweight data interchange format that is easy to read and write, and easy to machine generate and parse.

2. Below, I write a string in JSON format.

{"UserID": One, ' name ': ' Sunliyuan '}

1) Outside of the JSON string is a curly brace.

2) inside is similar to our dictionary.

3. What do we do when we use the built-in eval function of JavaScript to convert a JSON-formatted string into a JSON object?

1) We need to enclose this string with a parenthesis.

2) For example, the string above, I assign it a value of a txt such a variable.

var txt={"userid": One, ' name ': ' Sunliyuan '}

  

When assigning variables, we need to enclose them in parentheses.

When using double quotation marks outside, use single quotes inside.

var txt = "{' UserID ': one, ' name ': ' Liyi '}";

  

3) and then parse it to parse the string into a JSON object.

Then through our eval function, add a pair of parentheses inside.

Then connect the string together.

var txtobj = eval ("(" +txt+ ")");

  

4) At this point, we will output the userid of the parsed object once.

document.write (Txtobj.userid);

Refreshing the browser, it parses our JSON string into a JSON object. Output 11

Through this object's., output one of its properties. The UserID is 11. That's the right result we want.

There are 3 instances in our case.

1. The first type is the string, and we directly enter the execution statement.


It will return to normal results.

2. In the second case, we assign this eval to some properties, and we also implement some expressions directly in it, and also to detect if the eval parameter is valid.

3. Then we use Eval to parse this JSON-formatted string.

It parses a JSON-formatted string, and it parses our JSON string into a JSON object by enclosing the left and right sides of the original string with parentheses.

Then we can get the value inside by accessing the object's properties.

The use of the Eval function in Ajax

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.