如何在jQuery自定义插件中访问本地变量以进行闭包?

时间:2011-09-17 08:37:34

标签: javascript jquery jquery-plugins

这是用于创建自定义插件的jQuery代码。

(function( $ ){

  var methods = {
    get_me_now: 'abc',
    init : function( options ) { // how to access get_me_now in here? },
    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 );

我有一个变量'get_me_now'。我称之为:$('#test')。tooltip('init'); 'init'函数如何获得'get_me_now'变量?

1 个答案:

答案 0 :(得分:4)

methods.get_me_now

您可以参考方法。