0

我无法弄清楚我在这个插件中做错了什么,

(function($){

    var methods = {

        defaults: function(options) {

            // Default options.
            var defaults = {
                setup: {
                    tester1: "hello1",
                    tester2: "hello2",
                    tester3: "hello3"
                },
                tester: "hello world"               
            }

            // Always leave this line here.
            var options = $.extend(defaults, options);
            alert(options.setup.tester2);

            // Return the processed options.
            return options;

        },

        init : function( options ) {

            var $this = this;
            var o = $this.plugin('defaults',options);

        },

        something : function( options ) {


        }
    };

    $.fn.plugin = function( method ) {

        // @reference: http://docs.jquery.com/Plugins/Authoring#Namespacing
        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 );  // always change 'init' to something else if you different method name.
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.plugin' );
        }

        return this; 

    };

})(jQuery);

所以,

jQuery().plugin(); // return 'hello2' --> correct

但,

jQuery().plugin({setup:{tester1:"x"}}); // return 'undefined' --> should be 'hello2'

有什么想法我应该做什么或我错过了什么?

jsfiddle

4

2 回答 2

1

我使用这些代码来定义默认值和选项,它可以工作:

var defaults = {
    speed : 150 ,
    check_effect: "fade" ,
    uncheck_effect: "fade" ,
    };

var options = $.extend({}, defaults, options);

而且我不使用函数来定义你所拥有的默认值!

于 2013-07-13T21:52:09.400 回答
0

I think the answer is,

var options = $.extend( true, {}, defaults, options );
于 2013-07-14T09:07:47.753 回答