【发布时间】:2012-09-02 11:41:59
【问题描述】:
你好,我找到了一篇关于stackoverflow如何调用动态函数的文章:
function mainfunc(func) {
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
这非常有用。但是我怎样才能在我的对象函数中调用函数呢?
我得到一个错误:
"TypeError: this[func] 未定义 this[func].apply(this, Array.prototype.slice.call(arguments, 1));"
看起来像:
var myScript = {
...
add : function() {
...
myScript.ajax('url.php', params, 'myScript.add2List');
},
add2List : function(data) {
console.log(data);
},
ajax : function(url, params, func) {
$.ajax({
type: "POST",
url: url,
data: params,
success: function(data) {
console.log(func);
mainfunc(func, data);
}
});
}
}
【问题讨论】:
标签: jquery object dynamic call