在对象中调用动态jquery函数
您好,我找到一篇关于stackoverflow如何调用动态函数的文章:在对象中调用动态jquery函数,jquery,object,dynamic,call,Jquery,Object,Dynamic,Call,您好,我找到一篇关于stackoverflow如何调用动态函数的文章: function mainfunc(func) { this[func].apply(this, Array.prototype.slice.call(arguments, 1)); } 这是非常有用的。但是如何在我的对象函数中调用函数呢 我得到一个错误: “TypeError:此[func]未定义 this[func].apply(this,Array.prototype.slice.call(arguments
function mainfunc(func) {
this[func].apply(this, Array.prototype.slice.call(arguments, 1));
}
这是非常有用的。但是如何在我的对象函数中调用函数呢
我得到一个错误:
“TypeError:此[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);
}
});
}
}
您不需要使用该函数,您可以执行以下操作
var myScript = {
...
add : function() {
...
myScript.ajax('url.php', params, this.add2List);
},
add2List : function(data) {
console.log(data);
},
ajax : function(url, params, func) {
$.ajax({
type: "POST",
url: url,
data: params,
success: func
});
}
}
嗨,xdazz。非常感谢,它是这样工作的。但它不适用于“this”,我不得不写“myScript”。它找不到这个-不知道;)