在jquery插件模式中从公共函数调用私有函数

时间:2014-06-29 13:12:00

标签: javascript jquery jquery-plugins

我是新来的,所以忍受我,

我有以下类型的插件模式

;(function ( $, window, document, undefined ) {
  'use strict';
        defaults = {..}
    // The actual plugin constructor
    function Plugin ( element, options ) {
        this.init();
    }
    Plugin.prototype = {
        init: function () {
            ....
        },
        privateFunc: function ( options) {
          //do private stuff
        }
    };

    $.fn.plugin = function (method, options ) {
        if (method === undefined){
          method = 'null';
        }
        switch(method){
          case 'null':
            return this.each(function() {
                if ( !$.data( this, "plugin_custom" ) ) {
                    $.data( this, "plugin_custom", new Plugin( this, options ) );
                }
            });
          case 'publicFunc':
           // do stuff with options and then call privateFunc with some data
           break;
      }
    };
})( jQuery, window, document );

我想从privateFunc致电publicFunc,但我不确定如何访问它

1 个答案:

答案 0 :(得分:2)

Plugin.prototype.privateFunc实际上并不是私人的;作为原型的一部分意味着公众可见度;这意味着您可以像这样访问它:

var plugin = $.data( this, "plugin_custom");
plugin.privateFunc();
相关问题