0
我对Javascript中的“call”函数有所怀疑。我有这样的jQuery插件:对jQuery插件的回调函数
(function($) {
var methods = {
method1 : function(settings, callback) {
// do stuff
if($.isFunction(callback)){
callback.call(this, $(list));
}
},
method2 : function(settings, callback) {
// do stuff
if($.isFunction(callback)){
callback.call(this, $(list));
}
},
method3 : function(settings, callback) {
// do stuff
if($.isFunction(callback)){
callback.call(this, $(list));
}
},
};
$.fn.jPlugin = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
}
else if (typeof method === 'object') {
$.error('Expected two (2) parameters: parameter 1 must be the method name to call. Parameter 2 must be an object containing the settings for this method.');
}
else {
$.error('Method ' + method + ' does not exist');
}
};
而且我有点困惑该行的jQuery插件文档中:
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
插件按预期工作编号回调传递。但是,如果我像这样调用插件,我应该如何将回调传递给正确的方法?
$('#my-div').jPlugin('method1', settings);
如果回调函数的部分设置对象,或者我会适应的插件能接受吗?
$('#my-div').jPlugin('method1', settings, callback);
谢谢各位!
'$( '#我-DIV')jPlugin( '方法1',设置回调);'将与您当前的插件代码工作。你到底有什么问题? – 2012-01-27 18:02:25
我只是完全不了解“通话”这一段。我认为(参数1)引用了$('#my-div')的第二个参数。jPlugin('method1',settings);,... so“settings”。我不知道是否应该在其他地方添加回调。 – luso 2012-01-27 18:12:01
我猜,“参数”本身是指调用的所有参数,所以设置和回调,就是这样吗? – luso 2012-01-27 18:13:01