JavaScript Learning Notes 4 eval Function _ Basics

Source: Internet
Author: User
The effect of Eval is simply to pass a string to the JS interpreter, which the JavaScript interpreter interprets as JavaScript code and executes it.
For the simplest example:
Copy Code code as follows:

<script type= "Text/javascript" >
Eval ("Alert (1+1)");
</script>

Very simply, interpret the string as a JS code and execute it, pop 2.
Of course, the above example is just a toy, in practice, no one will be stupid to do so. I think the most basic use of the Eval function should be in the DOM, for example we have DIV1,DIV2,DIV3, So the easiest way to do this is to use eval to stitch up such a program in the For loop, when our IDs are not available at document.getElementById. For example this:
Copy Code code as follows:

<script type= "Text/javascript" >
for (var loop = 1; loop < loop++) {
Eval (' document.getElementById ("div" +loop). Innerhtml= "123");
}
</script>

The most basic use of the end, I believe that we still have to this function is not done, if this function is only such a point of use, it is too boring. Let's just take a little bit of the eval () function.
Let's start with the eval scope, and look at a function like this:
Copy Code code as follows:

<script type= "Text/javascript" >
Eval ("var i=3");
alert (i);
</script>

The code is simple, and the result can pop up to 3. Next, compare this code:
Copy Code code as follows:

<script type= "Text/javascript" >
var test = function () {
Eval ("var i=3");
alert (i);
}
Test ();
alert (i);
</script>

The result is a 3 first pop-up, then a undefined.
This means that the code that the eval () function executes dynamically does not create a new scope, and its code is executed in the current scope. So that is, the eval () function can also fully use objects such as the this,argument of the current scope.
In IE, support for such a function similar to eval () is called: Execscript (). We can write a simple piece of code.
Copy Code code as follows:

<script type= "Text/javascript" >
var test = function () {
Execscript ("var i=3");
alert (i);
}
Test ();
alert (i);
</script>

The result pops up 2 3, which shows the characteristics of the Execscript function, first of all similar to eval, which can interpret the string as a JS code and execute it, but his scope is not the current scope, but the global scope. When we put the above code in Firefox and Google browser to try: found in Firefox execscript code is invalid, it also illustrates a problem, execscript code browser compatibility is problematic.
So the question is, how can we put the "advantages" of these two functions together, that is, global + browser compatibility. Search the Internet, I have summed up a bit, presumably this:
Copy Code code as follows:

<script type= "Text/javascript" >
var strongeval = function (code) {
if (Window.navigator.userAgent.indexOf ("MSIE") >= 1) {
Execscript (code);
}
if (Window.navigator.userAgent.indexOf ("Firefox") >= 1) {
Window.eval (code);
}
else {
Execscript (code);
}
};
var Test = function () {
Strongeval ("var i=3");
}
Test ();
alert (i);
</script>

This makes it perfectly compatible with FF and IE, and its intrinsic code is that eval and window.eval are not equivalent in FF, which is a wonderful thing.
In addition, we can also use Eval+with to achieve some of the strange sexual skills.
We can write this code in the general sense:
Copy Code code as follows:

var obj = function () {
THIS.A = 1;
this.b = 2;
THIS.C = 5;
This.fun = function () {
THIS.C = This.a + this.b;
}
};
var o = new obj ();
O.fun ();
alert (O.C);

Or is this:
Copy Code code as follows:

var obj = {
A:1,
B:2,
C:5,
Fun:function () {
THIS.C = This.a + this.b;
}
}

or so:
Copy Code code as follows:

var obj = function () {
THIS.A = 1;
this.b = 2;
THIS.C = 5;
};
Obj.prototype.fun = function () {
THIS.C = This.a + this.b;
}
var o = new obj ();
O.fun ();
alert (O.C);

Anyway, are you tired of this? So let's take a very different approach and make it a little more comfortable for the senses at least.
Copy Code code as follows:

<script type= "Text/javascript" >
var funtemp = function () {
c = a + B;
}
var obj = {
A:1,
B:2,
C:5
};
var fun;
With (obj) {
Eval ("fun =" + funtemp);
}
Fun ();
alert (OBJ.C);
</script>

This is very reluctantly, so good that we don't discuss what looks comfortable and uncomfortable. Let's discuss a situation like this.
Copy Code code as follows:

<script>
var Dbcommon = function () {
Alert ("1."); CreateConnection ();
Alert ("2."); OpenConnection ();
Alert ("3."); CreateCommand ();
Alert ("4."); Excutecommand ();
Alert ("5."); CloseConnection ();
}
var Sqlservercommon = {
Createconnection:function () {alert ("Establish SQL Server Connection");
Openconnection:function () {alert ("Open SQL Server Connection");
Createcommand:function () {alert ("Create ¨sql Server command");
Excutecommand:function () {alert ("Execute dsql Server command");
Closeconnection:function () {alert ("Turn off SQL Server Connection");}
};
var Oraclecommon = {
Createconnection:function () {alert ("Establish ¢oracle connection");
Openconnection:function () {alert ("Open aoracle Connection");
Createcommand:function () {alert ("Create ¨oracle command");
Excutecommand:function () {alert ("Execute doracle command");
Closeconnection:function () {alert ("Off? Oracle connection "); }
};
With (Sqlservercommon) {
Eval ("forsqlserver=" + Dbcommon);
}
With (Oraclecommon) {
Eval ("fororacle=" + Dbcommon);
}
Forsqlserver ();
Fororacle ();
</script>

Can we think of this as a primitive pattern of template methods? Oh. We can also refer to this as using eval and with to change the context of a function.
But then again, Eval is rarely used in general situations, and we can totally avoid it.
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.