function a(){
alert(\"fun a()\");
}
function b(){
alert(\"fun b()\");
}
var methodName = \"\";
//method1
methodName = \"a\";
function method1(methodName){
//初始化this.func属性,
this.func = function(){};
try{
//这里用eval方法,把我们传进来的这个方法名所代表的方法当作一个对象来赋值给method1的func属性。
//如果找不到methodName这个对应的对象,则eval方法会抛异常
this.func = eval(methodName);
}catch(e){
alert(methodName+\"()不存在!\");
}
}
var c = new m(methodName);
c.func();
/**
* method2, 比较简洁
*/
methodName = \"b\";
function method2(methodName){
this.func = new Function(methodName+\"();\");
}
var c = new m(methodName);
try{
c.func();
}catch(e){
Ext.Msg.alert(methodName+\"()不存在!\");
}
本文地址:https://www.stayed.cn/item/14023
转载请注明出处。
本站部分内容来源于网络,如侵犯到您的权益,请 联系我