Nick Ginanto
Nick Ginanto

Reputation: 32130

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

Upvotes: 1

Views: 1210

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173572

Plugin.prototype.privateFunc isn't actually private at all; being part of the prototype implies public visibility; that means you can access it like so:

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

Upvotes: 2

Related Questions