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

  • 2020-03-30 02:34:36
  • OfStack

 
function a(){ 
alert("fun a()"); 
} 
function b(){ 
alert("fun b()"); 
} 
var methodName = ""; 
//method1 
methodName = "a"; 
function method1(methodName){ 
//Initialize the this.func property,
this.func = function(){}; 
try{ 
//Using the eval method, assign the method represented by the method name we passed in as an object to the func property of method1.
//If the methodName 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(); 

 
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! "); 
} 

Related articles: