JQuery插件教程-init以外的方法如何访问选项



在jQuery插件教程中,其他方法(如show()、hide()或update())如何访问init()中传递的选项?在init()中处理它以便其他方法可以访问它的正确方法是什么?

(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 );

在我的插件中,我总是使用访问选项

this.options

但是。。。我使用不同的方法。。。

(function ($) {
    $.widget("my.plugin", {
        options: {
            op1: "op1"
        },
        _init: function () {
        },
        _create: function () {
        },
        show: function () {
        }
    });
})(jQuery);

最新更新