JS Dynamic Invocation Method Name Example Introduction

Source: Internet
Author: User
Tags eval javascript eval

  How to dynamically invoke method names in JS, presumably a lot of friends will not bar, the following for you to illustrate the specific method of calling

First look at the JS function     JavaScript eval () function   definition and usage     the eval () function computes a string and executes the JavaScript code in it.   Grammar     eval (string)     parameter description     string required. The string to evaluate, which contains the JAVASCRIPT expression to evaluate or the statement to execute.     return value     by calculating the value of string (if any).     Description     This method only accepts the original string as an argument, and if the string argument is not the original string, the method returns without any changes. Therefore, do not pass a String object as an argument for the eval () function.     If you attempt to overwrite the Eval attribute or assign the eval () method to another property and call it through this property, the ECMAScript implementation allows a Evalerror exception to be thrown.     Throw     If there are no valid expressions and statements in the argument, the SyntaxError exception is thrown.     throws a Evalerror exception if the eval () is invoked illegally.     If the Javascript code passed to eval () generates an exception, eval () passes the exception to the caller.     Tips and comments     hint: Although the eval () is very powerful, it is not very much used in practice.     Instance     Example 1    In this case, we'll use Eval () on several strings, and look at the returned results:  code as follows: <script type= "Text/java Script >    eval ("X=10;y=20;document.write (x*y)")     document.write (eval ("2+2"))     VAR x=10  docuMent.write (eval (x+17))     </script>    output:    200  4  27    Example 2& nbsp   See in other cases, the results of the eval () return:  code as follows: eval ("2+3")//return 5  var myeval = eval; Evalerror exceptions may be thrown   myeval ("2+3"); Evalerror exceptions may be thrown     You can use the following code to detect if the parameters of eval () are legitimate:    code as follows: Try {  alert ("Result:" + eval (Prom PT ("Enter An expression:", "")); }    catch (Exception) {  alert (Exception); }    The first method is to use JS inside the eval    Below is the example of your own write     code as follows: Call ("ShowMsg");    function call (functionname) {  eval ("this." +functionname+ "()"); }  function showmsg () {  alert ("Success"); }    Eval can automatically recognize the string you splice as a method and call it.     But the drawbacks are also great, imagine a person changing the way you call the method name, you can call any of your methods.     The second method is primarily used as a method of its own definition     is primarily the second method that requires a specific way to write the     code as follows: function call (functionname) {  showmsgs["ShowMsg"] (); }    var showmsgs = {Showmsg:fUnction () {  alert ("Success"); } }  Call ("ShowMsg"); 
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.