1

次の通常の標準を使用するjQueryプラグインがある場合:

(function( $ ){
  var methods = {
    init : function( options ) {
      var defaults = {
      }
      var options =  $.extend(defaults, options);
      return this.each(function(){
        var returnValue = myUniversalFunction();
      });
    },
    test : function( options ) {
      var defaults = {
      }
      var options =  $.extend(defaults, options);
      return this.each(function(){
        var returnValue = myUniversalFunction();
      });
    }
  };
  $.fn.jPlugin = 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.tooltip' );
    }    

  };
})( jQuery );

init メソッドと test メソッドの両方でアクセスできるが、プラグイン自体の外部では使用できない関数をどこに配置しますか?

4

2 に答える 2

5

次のように、 2行目の直後に配置し(function( $ ){ます。

(function( $ ){
    var inner_function = function() {
        // ...
    };
    var methods = {
        // ...
    };
    $.fn.jPlugin = function( method ) {
        // ...
    };
})( jQuery );

この機能inner_functionは、その内部のどこでも利用できます(function($){ ... })(jQuery);が、外部では利用できません。

于 2012-06-29T15:41:08.357 に答える
0

すぐ上。関数は、そのスコープ内のすべてで使用できます。

(function( $ ){
    function myFunc() {
        // ...
    }
于 2012-06-29T15:43:15.220 に答える