📌  相关文章
📜  javascript 按字符串名称执行函数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:08.830000             🧑  作者: Mango

代码示例2
//function to execute some other function by it's string name 
function executeFunctionByName(functionName, context , args ) {
  var args = Array.prototype.slice.call(arguments, 2);
  var namespaces = functionName.split(".");
  var func = namespaces.pop();
  for(var i = 0; i < namespaces.length; i++) {
    context = context[namespaces[i]];
  }
  return context[func].apply(context, args);
}

//my adding function, could be any function
function myAddFunction(a,b){
 return a+b;
}

//execute myAddFunction from string
var c=executeFunctionByName("myAddFunction", window, 3,4); //7