打印函数数组的函数名称
Print function names of an array of functions
假设我有数组[func1, func2, func3]。
我想打印成一个字符串:"func1, func2, func3"。但是,它会打印函数的全部内容。
我是否必须做一些正则表达式才能从输出中获取名称,或者有更简单的方法?
干杯。
使用 Function name
属性:
function doSomething() { }
alert(doSomething.name); // alerts "doSomething"
请注意,根据文档,这在 Internet Explorer 中不起作用。 如果这对您很重要,您可以考虑其他选项。
你想在列表中获取函数名称,对吧?如果是这种情况,这样的事情应该适合你。如果这不是您想要做的,请告诉我。JsFiddle 在这里工作代码
//declare the dummy functions
function funcOne(){
return null;
}
function funcTwo(){
return null;
}
function funcThree(){
return null;
}
//add the functions to the array
var functionArray=[funcOne,funcTwo,funcThree];
//declare an output array so we can then join the names easily
var output=new Array();
//iterate the array using the for .. in loop and then just getting the function.name property
for(var funcName in functionArray){
if(functionArray.hasOwnProperty(funcName))
output.push(functionArray[funcName].name);
}
//join the output and show it
alert(output.join(","));
相关文章:
- 使用jQuery浏览函数数组
- coffescript/javascript函数数组的行为真的很奇怪
- 返回多个变量的 JavaScript 函数:数组与对象.最佳实践是什么
- 如何在 JavaScript 中动态地编写可迭代的函数数组
- JavaScript 自包含函数数组访问自我索引
- 如何在javascript中调用函数数组的索引号
- 从外部函数(数组)了解返回函数(x)
- Javascript函数数组
- 引用函数数组中的另一个函数
- 文本框搜索/Javascript函数数组**不对应**
- 返回一个函数数组
- selenium-webdriver 将函数数组作为参数传递给 executeScript
- 70%的时间运行JavaScript函数,从函数数组中随机选择
- 如何循环函数数组并在单击时一次执行一个
- 如何在 Javascript 命名空间脚本中创建函数数组
- 创建函数数组
- 在 Jscript 中为 Q.all() 构建一个动态函数数组
- 打印函数数组的函数名称
- 循环遍历函数数组 - 对象不支持此属性或方法 (IE8)
- 将函数数组传递给另一个函数