简体   繁体   English

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

[英]calling private function from public function in jquery plugin pattern

I'm new at this, so bear with me, 我是新来的,所以请忍受,

I have the following type of plugin pattern 我有以下类型的插件模式

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

I want to call privateFunc from publicFunc but I am unsure on how to access it 我想从publicFunc调用privateFunc ,但是我不确定如何访问它

Plugin.prototype.privateFunc isn't actually private at all; Plugin.prototype.privateFunc实际上根本不是私有的。 being part of the prototype implies public visibility; 成为原型的一部分意味着公众的知名度; that means you can access it like so: 这意味着您可以像这样访问它:

var plugin = $.data( this, "plugin_custom");
plugin.privateFunc();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM