【发布时间】:2011-04-12 09:32:12
【问题描述】:
我正在开发一个简单的 jquery 插件,但在设置方法结构时遇到了困难。有人可以启发我。我正在使用官方 Jquery Authoring 文档中描述的插件结构。
我遇到的问题是在调用私有函数 _generateID 时,该函数实际上返回函数文本 (function() { return this.. ) 而不是 'hi'。
(function( $ ){
var methods = {
init : function( options ) {
return this.each(function() {
});
},
_generateID : function() {
return this.each(function() {
return 'hi';
});
},
create : function( options ) {
return this.each(function() {
var settings = {
'id' : methods._generateID,
};
if ( options ) { $.extend( settings, options ); }
$('<div>', {
id : settings.id,
}).appendTo(this);
});
},
destroy : function( id ) {
return this.each(function(){
$(window).unbind('#'+id);
$('#'+id).remove();
});
}
};
$.fn.workzone = 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 );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.workzone' );
}
};
})( jQuery );
【问题讨论】: