从名称中获取JavaScript函数对象作为字符串? [英] Get JavaScript function-object from its name as a string?
本文介绍了从名称中获取JavaScript函数对象作为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在JavaScript中,如果我在变量中有一个字符串,有没有办法获得对具有该匹配名称的函数对象的引用?请注意,jQuery是可用的,所以我也可以使用它的任何帮助方法。
In JavaScript, if I have a string in a variable, is there a way to get a reference to the function object which has that matching name? Note that jQuery is available to me so I can use any of its helper methods also.
例如:
myFunction = function(){};
var func_name = "myFunction";
var myFunctionPtr = ??? //how to get the function from the name
谢谢
推荐答案
如果您知道它可以使用它的全局函数:
if you know that its a global function you can use:
var functPtr = window[func_name];
//functPtr()
否则将 window 替换为包含该函数的父对象。
Otherwise replace window with the parent object containing the function.
这篇关于从名称中获取JavaScript函数对象作为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文