jquery命名空间:如何将默认选项从一个方法传递到子序列方法?

jquery命名空间:如何将默认选项从一个方法传递到子序列方法?,jquery,jquery-plugins,namespaces,javascript-namespaces,Jquery,Jquery Plugins,Namespaces,Javascript Namespaces,如何将默认选项从一种方法传递到子序列方法 例如,我在init方法中有一些默认设置。我想将这些设置用于其他方法,如show (function($) { var methods = { init: function(options) { var defaults = { element: "selection" } var options = $.extend(

如何将默认选项从一种方法传递到子序列方法

例如,我在
init
方法中有一些默认设置。我想将这些设置用于其他方法,如
show

(function($) {
    var methods = {
        init: function(options) {
            var defaults = {
                element:    "selection"
            }

            var options =  $.extend(defaults, options);
            var o = options;

            alert(o.element);
        },
        show: function(o) {
            alert(o.element);
        },
        hide: function() {
            alert(o.element);
        }
    };

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

var blah = $('#hello').plugin('show'); // TypeError: o is undefined
我想得到
选择
作为答案。。。可能吗

编辑:

(function($) {

    $.fn.plugin = 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' );
        }    

     }; 

     $.fn.plugin.defaults = {
        attr: 'checked',
        expected: true,
        onChange: function(){}
      }; 

    var methods = {

        init: function(options) {
            var options = $.extend({}, $.fn.plugin.defaults, options);
            var o = options;

            var $this = this;

            $this.show();
            //alert(o.attr);
        },
        show: function(options) {

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

            alert(o.expected);
        },
        hide: function(object) {
            alert(o.element);
        }
    };


})( jQuery );

现在我得到了
#hello world
(正确)

但是,


我想得到
true
作为答案,因为
init
正在访问
show
。那么,我如何从方法中访问其他方法呢?

在我看来,您正在尝试重新设计可扩展的jquery插件(又称jquery UI小部件)。我可以教你很多如何完成你想做的事情,但这真的是重新发明轮子。jQueryUI小部件工厂非常适合这种类型的东西

--这是他们的旧文档,但它比其他任何东西都更能勾勒出小部件工厂的轮廓,IMHO。他们在1.9/1.10中对其进行了一些小的修改,但大多数概念仍然是相同的

编辑

这是我对你的代码的修改。我相信这正是你想要的

(function($) {
    var methods = {
        init: function(options) {
            var defaults = {
                element:    "selection"
            }

            this.options =  $.extend(defaults, options);

            alert("init: " + this.options.element);
        },
        show: function() {
            alert("show: "  + this.options.element);
        },
        hide: function() {
            alert("hide: " + this.options.element);
        }
    };

 $.fn.plugin = function( method ) {
    if ( methods[method] ) {
      methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    } 
    return this; // this is needed to ensure chainability
 };

})( jQuery );

var blah = $('#hello').plugin('init').plugin('show');​

谢谢你的回答。我不做任何小部件。我只是想学习在插件中使用名称空间,但如果我想在方法之间共享一些基本的默认设置,名称空间似乎非常困难。另一个问题是-如何在另一个方法中调用一个方法?如果我坚持使用基本插件,那么我就不会有这样的问题……不要让术语
小部件
欺骗你。小部件仅仅是一个插件,与您正在构建的插件完全相同。但是widget工厂内置了可扩展性——解决了您将要面临的许多问题。如果你想让我告诉你如何让你的代码工作,我可以做到,但如果你真的对插件创作感兴趣,我强烈推荐小部件。我可以帮你其中一个/两个--只要告诉我你对哪个方向感兴趣就行了。你的编辑看起来不错。缺少链接性,这是大多数插件消费者期望插件做的。Javascript并不真正支持名称空间。不同的开发人员设计了不同的方法来模仿它们——有些人比其他人更成功。当然,每个人对各种方法都有自己的看法。如果您想了解使用jQueryUI小部件工厂的基础知识,我创建了一个简单的小部件——这些都是小部件工厂的一部分。我创建了一个关于使用工厂创建小部件的常见问题解答。如果它被发布,我会把链接发给你。
var blah = $('#hello').plugin('init'); // returns nothing
(function($) {
    var methods = {
        init: function(options) {
            var defaults = {
                element:    "selection"
            }

            this.options =  $.extend(defaults, options);

            alert("init: " + this.options.element);
        },
        show: function() {
            alert("show: "  + this.options.element);
        },
        hide: function() {
            alert("hide: " + this.options.element);
        }
    };

 $.fn.plugin = function( method ) {
    if ( methods[method] ) {
      methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof method === 'object' || ! method ) {
      methods.init.apply( this, arguments );
    } else {
      $.error( 'Method ' +  method + ' does not exist on jQuery.tooltip' );
    } 
    return this; // this is needed to ensure chainability
 };

})( jQuery );

var blah = $('#hello').plugin('init').plugin('show');​