按名称执行函数,将对象作为参数传递 [英] Executing a function by name, passing an object as a parameter

查看:90
本文介绍了按名称执行函数,将对象作为参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是问题 - 我知道函数的名称(并且该函数已经从外部脚本加载),但我没有实际的函数对象可供我调用。通常我会调用eval(function_name +(arg1,arg2)),但在我的情况下,我需要传递一个对象,而不是字符串。
简单示例:

Here's the problem - I know function by name (and that function has already been loaded form an external script), but I don't have an actual function object avail for me to call. Normally I would call eval(function_name + "(arg1, arg2)"), but in my case I need to pass an object to it, not a string. Simple example:

var div = document.getElementById('myDiv')
var func = "function_name" -- this function expects a DOM element passed, not id

如何执行此功能?

谢谢!
Andrey

Thanks! Andrey

推荐答案

您应该能够从顶级获取函数对象窗口。例如,

You should be able to get the function object from the top-level window. E.g.

var name = "function_name";
var func = window[name];
func( blah );

这篇关于按名称执行函数,将对象作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆