JavaScript - 如何从字符串名称调用函数并传递数组对象?
我有一个用户控件,它允许用户提供自己的脚本名称,这些脚本名称由控件在特定事件上调用。
我有以下代码:
initialize : function()
{
// Call the base initialize method
Point74.WebAutoComplete.callBaseMethod(this, 'initialize');
$(document).ready(
Function.createDelegate(this, this._onDocumentReady)
);
},
_onDocumentReady : function()
{
var me = this;
$("#" + me.get_id()).autocomplete(me.get_ashxAddress(),
{
formatItem: function(item)
{
return eval(me.get_formatFunction() + "(" + item + ");");
}
}
).result(me.get_selectFunction());
}
me.get_formatFunction 包含函数的名称,即“FormatItem”。这个例子目前正在使用 eval,我不想使用它......而且这个例子无论如何也不起作用,但我想我会展示我想要得到的内容。
在上面的示例中,我收到一个值未定义错误,因为“item”是一个字符串数组,并且 eval 尝试将其转换为一个长字符串。
如何实现此功能仍将“item”作为字符串数组传递给指定函数?
如果传递命名函数是一个坏主意,还有其他选择吗?
这就是我的控件的声明方式:
<p74:WebAutoComplete runat="server" ID="ato_Test" AshxAddress="WebServices/SearchService.ashx"
FormatFunction="formatItem" SelectFunction="searchSelectedMaster" />
I've got a user control which lets users provided their own script names that are called by the control on specific events.
I have the following code:
initialize : function()
{
// Call the base initialize method
Point74.WebAutoComplete.callBaseMethod(this, 'initialize');
$(document).ready(
Function.createDelegate(this, this._onDocumentReady)
);
},
_onDocumentReady : function()
{
var me = this;
$("#" + me.get_id()).autocomplete(me.get_ashxAddress(),
{
formatItem: function(item)
{
return eval(me.get_formatFunction() + "(" + item + ");");
}
}
).result(me.get_selectFunction());
}
me.get_formatFunction contains the name of a function, i.e. "FormatItem". This example is currently using eval, which I do not want to use... plus this example doesn't work anyway, but I thought I'd show what I'm trying to get at.
In the example above, I get a value undefined error as 'item' is a string array and eval tries to convert it into one long string.
How can I achieve this functionality any still pass through 'item' as a string array to the named function?
If passing named functions is a bad idea, are there any alternatives?
This is how my control is declared:
<p74:WebAutoComplete runat="server" ID="ato_Test" AshxAddress="WebServices/SearchService.ashx"
FormatFunction="formatItem" SelectFunction="searchSelectedMaster" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的目的是将所有参数传递给传递给 formatItem() 的用户指定函数,则不要使用:
使用:
可以在函数对象上调用 apply() 方法,以便使用指定“this”和参数数组。请参阅: http://odetocode.com/blogs/scott/archive/2007/07/04/function-apply-and-function-call-in-javascript.aspx 有关 call() 和 apply( 的说明) JavaScript 中的函数。
然后你会希望 get_formatFunction() 返回一个函数对象,而不仅仅是函数的名称;或者您可以尝试:
...获取一个按其名称在“me”上定义的函数。 (请注意,如果 get_formatFunction() 返回字符串“myFunc”,则这相当于 me.myFunc)
[编辑:更改对“this”的引用以使用“me”代替]
If your intent is to pass all arguments to the user-specified function that are passed to formatItem(), then instead of using:
Use:
The apply() method can be called on a function object, in order to invoke that function using the specified "this" and argument array. See: http://odetocode.com/blogs/scott/archive/2007/07/04/function-apply-and-function-call-in-javascript.aspx for an explanation of the call() and apply() functions in javascript.
Then you will want get_formatFunction() to return a function object, rather than just the name of the function; or you can try:
...to get a function which is defined on 'me' by its name. (Note that if get_formatFunction() returns the string 'myFunc', then this is equivalent to me.myFunc)
[Edit: changed references to 'this' to use 'me' instead]
我不确定你的总体计划是什么,但你可以传递函数本身而不是它们的名称:
I'm not sure what your overall plan is, but you can pass functions around themselves instead of their names: