eval function in JavaScript __ block chain

Source: Internet
Author: User
Tags function definition object object

The Eval function receives an argument s, and if S is not a string, it returns directly to S. Otherwise execute the S statement. Such as

If the result is a value, it returns this value, otherwise returns undefined.

It is necessary to note that the object declaration syntax "{}" does not return a value, which needs to be enclosed in parentheses

To return the value, the simple example is as follows:

var code1= ' "A" + 2 '; An expression

var code2= ' {a:2} '; Statement

Alert (eval (code1)); -> ' A2 '

Alert (eval (code2)); ->undefined

Alert (eval (' + Code2 + ')); ->[object Object]

As you can see, for an object declaration statement, it is simply execution and cannot return a value. In order to return

Object declaration statements such as the usual "{}" must be enclosed in parentheses to convert it to an expression before

can return its value. This is also used in JSON

One of the fundamentals of line Ajax development. As you can see clearly in the example, the second alert statement outputs the

is undefined, and the third with parentheses is the object represented by the statement.

? Now the focus of this article is how to execute global code within a function. To illustrate this issue, first

Look at an example:

var s= ' global '; Define a global variable

function Demo1 () {

Eval (' var s= ' local ');

}

Demo1 ();

alert (s); ->global

Well understood, the DEMO1 function above is equivalent to: function Demo1 () {var s= ' local ';},

which defines a local variable s.

So the final output is global is not a strange thing, after all, everyone can be very clear of the district

Sub-division variables and global variables.

With a closer look, you can find the Eval function's feature, which always calls its context variable empty

Between (also known as: Package, closure), whether it is a variable definition or a function definition, the

The following code produces an error that is not defined by the function:

var s= ' function test () {return 1;} '; A function definition statement

function Demo2 () {

Eval (s);

}

Demo2 ();

Alert (test ()); ->error:test is not defined

This is because the test function is defined in the local space, the DEMO2 function can be accessed, the outside access is not

Here it is.

and in actual Ajax development, sometimes we need to dynamically get code from the server to execute to reduce

The problem of loading too many code at once, or some code is generated by JavaScript itself,

Look at the Eval function to make it execute.

However, this dynamic acquisition code is generally done within the function, such as:

function Loadcode () {

var code=getcode ();

eval (code);

}

It can be seen that eval is impossible to execute in the global space, which brings a lot of problems to development, and has seen

Many people are upset about this.

But now I finally found a solution, hey, can be compatible with IE and Firefox, such as

Under

var x2={}//my namespace:)

X2. Eval=function (code) {

if (!! (window.attachevent &&!window.opera)) {

Ie

Execscript (code);

}else{

Not IE

Window.eval (code);

}

}

Now if you want to define global code within a function, you can call the X2.eval (code) method by calling the

One example is as follows:

var s= ' global ';

function Demo3 () {

X2. Eval (' var s= ' local ');

}

Demo3 ();

alert (s); -> ' Local '

Visible, the global variable s= "local" is redefined within the DEMO3 function.

Note that X2.eval does not return a value, and if you want to evaluate the expression, use the system's

The Eval function. X2. Eval is designed to do only global code definitions.

Actually see here, maybe some people feel the problem is too easy to solve the point, hehe, but found that this office

The law needs some luck and skill:

(1) For IE browsers, the default already provides such a function: Execscript, for the global

Space to execute code, just know that there are not many people.

(2) for the Firefox browser, call the Eval function directly in the caller's space;

The result called Window.eval is executed in the global space. The person who knows it is estimated to be less. Bi

Unexpectedly alert (eval==window.eval) returns True.

The features of the Firefox eval function are really strange, but from JavaScript specifications

can find its source:

If value of the Eval property is used into any way other than a

Direct call (which, the other than by the explicit of the

Name as an Identifier which are the memberexpression in a

Callexpression), or if the eval assigned to,

A Evalerror exception may is thrown.

This means that the Eval function is executed in relation to the caller, but does not say that it executes up and down

The problem of the article. So IE and Firefox is the one who is not difficult to say, we know the solution is good

Related Article

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.