Go JavaScript implements reflection by dynamically calling function--js through parameters

Source: Internet
Author: User
Tags object object switch case

The following articles are from  http://blog.rongzhiwang.com/king/archive/2012/08/13/javascriptjseval.aspx    Today, people ask the question: do a menu with jquery, each time using simultaneous to enter a parameter, this parameter is a function name, how to execute the function in the menu.   Probably that's what it meant, when I heard the familiar feeling, some say why not switch case, but to tell the truth sometimes with the judge really uncomfortable, as if they have done before, but really do not think how to achieve. Then searched, found the Eval solution, decided to record, lest forget again.   Online to find a summary of the relatively good, turn, and pay tribute to the author (i lazy, do not blame):  eval function receives a parameter s, if S is not a string, then directly return S. Otherwise, the S statement is executed. If the S statement execution result is a value, this value is returned, otherwise undefined is returned.   It is important to note that the object declaration syntax "{}" does not return a value and needs to be enclosed in parentheses to return a value, a simple example is as follows: The copy code code is as follows: Var code1= ' "A" + 2 '; Expression varcode2= ' {a:2} '; Statement alert (Eval_r (Code1)); ' A2 ' Alert (Eval_r (Code2)); ->undefinedalert (Eval_r (' (' + Code2 + ') '); ->[object object]   can see that, for an object declaration statement, it is simply execution and cannot return a value. In order to return an object declaration statement such as a commonly used "{}", it must be enclosed in parentheses to convert it to an expression in order to return its value. This is one of the basic principles of AJAX development using JSON. As you can see clearly in the example, the second alert statement outputs undefined, and the third one with parentheses outputs the object represented by the statement.   Now the focus of this article is how to execute global code within a function. To illustrate this problem, let's look at an example: Copy code code as follows: Var s= ' global '; Define a global variable function demo1 () {eval_r (' var s= ' local ');} Demo1 (); alert (s); ->global   well understood that the above demo1 function is equivalent to: FUnction Demo1 () {var s= ' local ';}, where a local variable s is defined. So the final output is global, which is not a strange thing, after all, we can clearly distinguish between local variables and global variables.   A closer look at the characteristics of the Eval function, which is always executed within the context variable space (also known as: Package, closure) that invokes it, whether it is a variable definition or a function definition, so the following code produces an undefined error: The copy code code is as follows: Var s= ' function test () {return 1;} '; A function defines the statement function Demo2 () {eval_r (s);} Demo2 (); Alert (test ()); ->error:test is not defined  this is because the test function is defined in the local space and can be accessed within the DEMO2 function, and is inaccessible outside. In the actual Ajax development, sometimes we need to get the code from the server dynamically, to alleviate the problem of loading too much code one time, or some code is generated by JavaScript itself, want to use the Eval function to make it execute.   But this kind of dynamic fetch code work is generally done within the function, such as: Copy code code as follows: function Loadcode () {varcode=getcode (); Eval_r (code);}   See Eval can not be implemented in the global space, which brings a lot of problems in development, but also seen a lot of people depressed. But now I finally found a solution, hey, can be compatible with IE and Firefox, the method is as follows: Copy code code as follows: Var x2={}//my namespace:) X2. Eval=function (code) {if (!! (window.attachevent &&!window.opera)) {//ieexecscript (code);} Else{//not Iewindow.eval_r (code);}}   Now if you want to define global code within a function, you can call the X2.eval_r code method, an example of which is as follows: The code is copied as follows: Var s= ' global ', function Demo3 () {x2.eval_r (' var s= "Local");} Demo3 (); alert (s); ' Local '   visible, redefine global variable s within DEMO3 function= "local". It is important to note that X2.eval does not return a value, and if the expression is evaluated, the system's eval function is used. X2. The eval is designed to be used only for global code definitions.   actually see here, perhaps some people feel that the problem is too easy to solve the point, hehe, but found that this method would need some luck and skill: (1) for IE browser, the default has provided such a function: ExecScript, used in the global space to execute code, just know not many people. (2) for the Firefox browser, the Eval function is called directly, then executes in the caller's space, if the call to Window.eval is performed in the global space. The person who knows is probably even less. After all, alert (eval==window.eval) returns true! The features of the Eval function in Firefox are really surprising, but they can also be found from the JavaScript specification: If value of the Eval property is used on any way other than a direct CA ll (that's, other than by the explicit use of Itsname as an Identifier which are the memberexpression in a callexpression) , or if the Eval property is assigned To,an Evalerror exception might be thrown. That means that the Eval function's execution is related to the caller, but does not say that its execution context is a problem. Therefore, IE and Firefox is the right one, it is difficult to say, we know the solution is good.   Detailed source reference: http://www.jb51.net/article/30008.htm

[Go]javascript--js in eval in dynamic call function through parameters

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.