0

私はオーサリングプラグインをいじっているので、$.fn オブジェクトを乱雑にしないことと、単一のプラグインが $.fn 名前空間以上のものを要求しない方法について話している jQuery ドキュメントに目を向けました...

(function( $ ){

  var methods = {
    init : function( options ) { 
      // THIS 
   },
    show : function( ) {
      // IS
    },
    hide : function( ) { 
      // GOOD
    },
    update : function( content ) { 
      // !!! 
    }
  };

  $.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 );

// calls the init method
$('div').tooltip(); 

// calls the init method
$('div').tooltip({
  foo : 'bar'
});

私は以下まですべてを理解しました:

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

部。

そのビットを単純に変更しました:

$.fn.tooltip = function(method)
{
    if(myFunc[method])
    {
        return myFunc[method].apply();
    }
}

そして、それはうまくいきます...誰かが私に何が起こっているのか、なぜ私のものがうまくいくように見えるのかを正確に説明してもらえますか?

編集: ところで、if/else 構造を完全に継続していないことはわかっています。これは、if 構造の残りの部分ではなく、言及した行に純粋に焦点を合わせているためです。

4

1 に答える 1