1

Im porting a prototype plugin to jQuery.

The plugin uses the proscribed method of collecting all the plugins methods in an object literal and then calling them like [object].[method]

What I dont understand is, in any of those methods there is use of properties (defined at the begging of the script i.e. var x = 0, var y = 0, etc) that appear to be global and not passed as arguments or properties of a specific method.

How would I do this in jQuery and is it possible?

Please refer to 'var1' in the code below. Where would this be set so that all methods have access to it?

Example:

;(function($){

    var methods = {
        init : function(options) {

            var config = {
            // default options...
            }

            // overide config
            var settings = $.extend(config, options);

            return this.each(function() {
                        // init code goes here...
            });
        },

        function1 : function() {
            function2();
        },

        function2 : function() {
                $(selector).css({
                  width : var1,
                });             
        },
    }

    $.fn.[PLUGINNAME] = 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);
4

2 に答える 2

5

自己喚起関数内で変数を宣言する必要がありますが、他の関数の外側で宣言する必要があります。

(function($){
    // This variable will be available to all methods, 
    // but not outside the plugin
    var var1,
        methods = {
            init : function(options) {
                ...
            }
            ...
        };
})(jQuery);

たとえば、初期化プロセスの一部であり、単なる静的変数ではない場合、init メソッドを使用して適切な値を設定できます。

JavaScript は関数を使用して変数のスコープを宣言するため、外側の自己喚起関数は、変数がグローバル スコープまで「リーク」しないようにしますが、内側の関数の外側で宣言されるため、プラグイン内のすべての機能。

于 2012-08-19T13:23:50.510 に答える
2

最上位の関数内で他のすべての前に定義すると、他のすべてのメソッドからアクセスできます。

(function($){
    var var1 = "some value";

    var methods = {
        init : function(options) {

            var config = {
            // default options...
            }

            // overide config
            var settings = $.extend(config, options);

            return this.each(function() {
                        // init code goes here...
            });
        },

        function1 : function() {
            function2();
        },

        function2 : function() {
                $(selector).css({
                  width : var1,
                });             
        },
    }

    $.fn.slideshow = 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);
于 2012-08-19T13:23:19.120 に答える