The function of eval is actually very simple. It is to pass a string to the JS interpreter, and the Javascript interpreter will interpret the string into Javascript code and execute it. Take the simplest example: Copy the code The code is as follows: <br>eval("alert(1 1)"); <br>script> <br> </div> <br>It’s very simple, interpret the string into JS The code is executed and 2 pops up. <br><br>Of course, the above example is just a toy, and no one would be stupid enough to use it in reality. I think the most basic way to use the eval function is in the DOM. For example, if we have div1, div2, and div3, then there is no way to get our ID when using document.getElementByID. So the simplest way is to use it in a for loop. Use eval to splice such a program. For example: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="66004" class="copybut" id="copybut66004" onclick="doCopy('code66004')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code66004"> <br><script type="text/javascript "> <br>for (var loop = 1; loop < 10; loop ) { <BR>eval('document.getElementById("div" loop).innerHTML="123"'); <BR>} <BR>script> <br> </div> <br>After talking about the most basic usage, I believe everyone is still interested in this function. If this function only has this kind of usage, it would be too boring. Then let's take a look at the eval() function bit by bit. <br><br> Let’s start with the scope of eval and look at this function: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="43977" class="copybut" id="copybut43977" onclick="doCopy('code43977')"><u>Copy code</u></a></span> Code As follows: </div> <div class="codebody" id="code43977"> <br><script type="text/javascript"> <br>eval("var i=3"); <br>alert(i); <br>script> <br> </div> <br>The code is very simple and the result can pop up 3. Next, compare this code: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="83498" class="copybut" id="copybut83498" onclick="doCopy('code83498')"><u>Copy code </u></a></span> The code is as follows: </div> <div class="codebody" id="code83498"> <br><script type ="text/javascript"> <br>var test = function () { <br>eval("var i=3"); <br>alert(i); <br>} <br>test(); <br>alert(i); <br>script> <br> </div> <br>The result is that 3 pops up first, then undefined. <br><br>Then explain: the code dynamically executed by the eval() function does not create a new scope, and its code is executed in the current scope. So in other words, the eval() function can also use this, argument and other objects in the current scope. <br><br>In IE, a function very similar to eval() is supported: execScript(). We can write a simple code. <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="64936" class="copybut" id="copybut64936" onclick="doCopy('code64936')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code64936"> <br><script type="text/javascript"> <br>var test = function () { <br>execScript("var i=3"); <br>alert(i); <br>} <br>test(); <br>alert(i); <br>script> <br> </div> <br>The result is that two 3s pop up, which also shows the characteristics of the execScript function. First of all, it is similar to eval, and can interpret strings into JS codes and execute them. , but its scope is not the current scope, but the global scope. When we put the above code into Firefox and Google Chrome and try it: we find that the code on execScript is invalid on Firefox, which also illustrates a problem. There is a problem with the browser compatibility of the execScript code. <br><br>Then this leads to the question, how can we bring together the "advantages" of these two functions, that is, global browser compatibility. I searched online and summarized it myself. It looks like this: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="47182" class="copybut" id="copybut47182" onclick="doCopy('code47182')"><u>Copy the code </u></a></span> The code is as follows: </div> <div class="codebody" id="code47182"> <br><script type="text/javascript"> <br>var StrongEval = function (code) { <br>if (window.navigator.userAgent.indexOf("MSIE") >= 1) { <br>execScript(code); <br>} <br>if (window.navigator.userAgent.indexOf("Firefox") >= 1) { <br>window.eval(code); <br>} <br>else { <br>execScript(code); <br>} <br>}; <br>var Test = function () { <br>StrongEval("var i=3"); <br>} <br> Test(); <br>alert(i); <br>script> <br> </div> <br>This will be perfectly compatible with FF and IE. The essential code is that eval and window.eval are not equivalent in FF. This is a very strange thing. <br><br>In addition, we can also use eval with to achieve some weird tricks. <br><br>We can write code like this in a general sense: <br><div class="codetitle"> <span><a style="CURSOR: pointer" data="73885" class="copybut" id="copybut73885" onclick="doCopy('code73885')"><u>Copy code</u></a></span> The code is as follows:</div> <div class="codebody" id="code73885"> <br>var obj = function () { <br>this.a = 1; <br>this.b = 2; <br>this.c = 5; <br>this.fun = function () { <br>this.c = this.a this.b; <br>} <br>}; <br>var o = new obj(); <br>o.fun(); <br>alert(o.c ; <br>var obj = { </div>a: 1, <br>b: 2, <br>c: 5, <div class="codetitle">fun: function () { <span>this.c = this.a this.b ; <a style="CURSOR: pointer" data="93775" class="copybut" id="copybut93775" onclick="doCopy('code93775')">} <u>} </u></a> </span>Or this: </div> <div class="codebody" id="code93775"> <br><br><br>Copy code<br><br><br> Code As follows: <br><br> <br>var obj = function () { </div>this.a = 1; <br>this.b = 2; <br>this.c = 5; <div class="codetitle">}; <span>obj.prototype.fun = function () { <a style="CURSOR: pointer" data="60663" class="copybut" id="copybut60663" onclick="doCopy('code60663')">this.c = this.a this.b; <u>} </u>var o = new obj(); </a>o.fun() ; </span>alert(o.c); </div> <div class="codebody" id="code60663"> <br>No matter what, are you tired of this? Then let us take a very different approach, so that it may be more comfortable to the senses at least. <br><br><br><br><br>Copy code<br><br><br> The code is as follows:<br><br> <br><script type="text/javascript"> </div>var funtemp = function () { <br>c = a b; <br>} <div class="codetitle">var obj = { <span>a: 1, <a style="CURSOR: pointer" data="59999" class="copybut" id="copybut59999" onclick="doCopy('code59999')">b: 2, <u>c: 5 </u>}; </a>var fun; </span>with (obj) { </div>eval("fun = " funtemp); <div class="codebody" id="code59999">} <br>fun(); <br>alert(obj.c); <br>script> <br><br> <br>This is very forced, so good, we are not going to discuss whether it looks comfortable or not. Let's discuss such a situation. <br><br><br><br><br>Copy code<br><br><br> The code is as follows:<br><br> <br><script> </div>var DBCommon = function ( ) { <br>alert("1."); CreateConnection(); <br>alert("2."); OpenConnection(); <div class="codetitle">alert("3."); CreateCommand(); <span> alert("4."); ExcuteCommand(); <a style="CURSOR: pointer" data="35432" class="copybut" id="copybut35432" onclick="doCopy('code35432')">alert("5."); CloseConnection(); <u>} </u>var SQLServerCommon = { </a>CreateConnection: function () { alert(" Establish a SQL Server connection"); }, </span>OpenConnection: function () { alert("Open a SQL Server connection"); }, </div>CreateCommand: function () { alert("Create a SQL Server command"); }, <div class="codebody" id="code35432">ExcuteCommand: function () { alert("Execute DSQL Server command"); }, <br>CloseConnection: function () { alert("Close SQL Server connection"); } <br>}; <br>var OracleCommon = { <br>CreateConnection: function () { alert("Establish an Oracle connection"); }, <br>OpenConnection: function () { alert("Open an Oracle connection"); }, <br>CreateCommand : function () { alert("Create ¨Oracle command"); }, <br>ExcuteCommand: function () { alert("Execute DOracle command"); }, <br>CloseConnection: function () { alert("Close ?Oracle connection"); } <br>}; <br>with (SQLServerCommon) { <br>eval("forSQLServer=" DBCommon); <br>} <br>with (OracleCommon) { <br>eval(" forOracle=" DBCommon); <br>} <br>forSQLServer(); <br>forOracle(); <br>script> <br><br> <br>Can we think of this as a simple What about the template method pattern? hehe. We can also call this using eval and with to change the context of a function. <br><br>But then again, Eval is rarely used in general situations, and we can completely avoid using it. <br> </div>