jQuery插件在其他公共函数内部调用公共函数

问题描述

| 我在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,1 ));
    } else if ( typeof method === \'object\' || ! method ) {
      return methods.init.apply( this,arguments );
    } else {
      $.error( \'Method \' +  method + \' does not exist on jQuery.tooltip\' );
    }    

  };

})( jQuery );
如何在更新方法内部调用show方法? 编辑:
show
方法引用
this
。使用
methods.show(options)
methods[\'show\'](Array.prototype.slice.call( arguments,1 ));
可以调用
show
方法,但是对
this
的引用似乎是错误的,因为我遇到了
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);

  }
};
    ,您对ѭ11的使用会把所有东西丢在这里。您正在故意更改
this
的含义,这就是使
this.show()
不起作用的原因。如果您希望
this
继续保持
methods
(这很有意义),则可以执行以下操作:
  return methods[ method ](Array.prototype.slice.call( arguments,1 ));