jquery插件调用其他公共函数内的public函数

我在http://docs.jquery.com/Plugins/Authoring上定义了我的插件

(function( $ ){ var methods = { init : function( options ) { }, show : function( options ) { }, hide : function( ) { }, update : function( content ) { // How to call the show method inside the update method // I tried these but it does not work // Error: not a function this.show(); var arguments = { param: param }; var method = 'show'; // Error: options is undefined methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 )); } }; $.fn.tooltip = function( method ) { // Method calling logic 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.tooltip' ); } }; })( jQuery ); 

如何在update方法中调用show方法?

编辑

show方法引用了this 。 使用methods.show(options)methods['show'](Array.prototype.slice.call( arguments, 1 )); 可以调用show方法,但是this.find(...) is not a function的引用似乎是错误的,因为我得到了一个this.find(...) is not a function错误。

show方法:

 show: function(options) { alert("Options: " + options); alert("Options Type: " + options.interactionType); var typeCapitalized = capitalize(options.interactionType); var errorList = this.find('#report' + typeCapitalized); errorList.html(''); }, 

 var methods = { init : function( options ) { }, show : function( options ) { }, hide : function( ) { }, update : function( options ) { methods.show.call(this, options); } }; 

你使用.apply就是把所有东西扔到这里。 你故意改变 this意味着什么,这使得this.show()无效。 如果你想让this继续成为methods (这很有意义),你可以简单地做到:

  return methods[ method ](Array.prototype.slice.call( arguments, 1 ));