【发布时间】:2011-08-08 21:08:59
【问题描述】:
我想知道如何在通过对象定义的子函数中访问 jQuery 自定义函数。观察:
(function($){
var methods = {
init : function ( options ) {
// getting the variable "data".
$.each (data, function(itemid, toll){
$.notification("update", data);
});
},
update : function ( options ) {
// ...
}
};
$.fn.notification = function ( method ) {
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
}
};
})(jQuery);
上述方法,使用$.notification ("update", data);不起作用,因为函数正在定义中。那么,如何在不重复代码的情况下访问上述功能呢?即编写一个函数只是为了更新,在method 变量之外,然后将该函数放在方法变量的update 索引中?提前谢谢你。 :3
【问题讨论】:
标签: jquery function recursion user-defined-functions