JS Saves the method name according to the variable and executes the method example

Source: Internet
Author: User
Tags eval

Use the Eval method to assign the method represented by the method name as an object to the Func attribute of Method1, and the friends who need it can refer to the

The code is as follows: function A () {alert ("Fun A ()");  Function B () {alert ("Fun B ()");  } var methodname = "";  Method1 methodname = "a";  function Method1 (methodname) {//Initialize This.func property, This.func = function () {};  try{//Here using the Eval method, the method represented by the method name we pass in is assigned to the Method1 Func property as an object.  If the MethodName object is not found, the Eval method throws an exception This.func = eval (methodname); }catch (e) {alert (methodname+) does not exist!  ");  } var c = new M (methodname);    C.func ();  /** * METHOD2, relatively concise * * * methodname = "B";  function Method2 (methodname) {This.func = new function (methodname+ "();");  var C = new M (methodname);  try{C.func (); }catch (e) {Ext.Msg.alert (methodname+) does not exist!  "); }

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.