Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6018421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:16:23+00:00 2026-05-23T03:16:23+00:00

I’m struggling with how best to combine javascript Classes and jQuery plugins. This question

  • 0

I’m struggling with how best to combine javascript Classes and jQuery plugins. This question isn’t very specific, what I’m hoping for is pointers to more resources.

Basically, I want to store state data and private methods in a class, and then extend each jQuery object which I call my plugin on to have those private methods and properties. Such that inside the plugin I can call methods directly off the jQuery object.

I read jQuery plugin design pattern (common practice?) for dealing with private functions, specifically David’s answer, however this initializes a new Class each time, and thus can’t be used to save the state of the object.

I also found http://fuelyourcoding.com/jquery-plugin-design-patterns-part-i/, which recommends creating a class and then storing it in .data().

I think ideally what I want to end up with is code that looks like

(function( $ ){

  var methods = {
    init : function( options ) { // Initialize each object with a state and private methods },
    show : function( ) { 
      // testFoo() is a private method that checks the element's state
      if(this.testFoo()){
        // Relying on jQuery's html() method
        this.html() = this.fooTemplate();
      }
    }
  };

  // Boiler plate plugin from http://docs.jquery.com/Plugins/Authoring
  $.fn.myPlugin = 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.myPlugin' );
    }    
  };
})( jQuery );

Finally, it doesn’t seem like I can bake the private methods into the plugin directly because methods like “testFoo()” will return a boolean, and therefore aren’t chainable.

Thoughts? Am I approaching this the right way? Is there another design pattern I should be using? Perhaps not using jQuery plugin architecture at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:16:24+00:00Added an answer on May 23, 2026 at 3:16 am

    Here’s a proposed solution. It combines few different approaches (John Resig’s inheritance model and Alex Saxton’s plugin inheritance model).

    Define your inheritable plugin:

    (function ($) {
    
        My.Plugin = Class.extend({
    
            /*
            * Initialization (constructor)
            */
            init: function (element, meta) {
                var $meta = $.extend({ name: "pluginName" }, meta);
    
                // Call the base constructor
                this._super(element, $meta);
    
                // TODO: Add custom initialization code like the following:
                // this._testButton = $('.testButton', element).get(0);
            },
    
    
            /*
            * Public methods
            */
    
            show: function() {
                 alert('This is a public method');
    
            },
    
    
            /*
            * Private methods
            */
    
            // DEMO: Overriding the base _paint method:
            _paint: function () {
    
                // "this._super()" is available in all overridden methods
                // and refers to the base method.
                this._super();
    
                alert('TODO: implement myPlugin._paint!');
            }
    
    
        });
    
    
        // Declare this class as a jQuery plugin
        $.plugin('my_plugin', My.Plugin);
    
    
    })(jQuery);
    

    Define Base class

    (function () {
        var initializing = false, fnTest = /xyz/.test(function () { xyz; }) ? /\b_super\b/ : /.*/;
        // The base Class implementation (does nothing)
        this.Class = function () { };
    
        // Create a new Class that inherits from this class
        Class.extend = function (prop) {
            var _super = this.prototype;
    
            // Instantiate a base class (but only create the instance,
            // don't run the init constructor)
            initializing = true;
            var prototype = new this();
            initializing = false;
    
    
            // Copy the properties over onto the new prototype
            for (var name in prop) {
                // Check if we're overwriting an existing function
                prototype[name] =
                       typeof prop[name] == "function"
                    && typeof _super[name] == "function"
                    && fnTest.test(prop[name])
                        ? (function (name, fn) {
                            return function () {
                                var tmp = this._super;
    
                                // Add a new ._super() method that is the same method
                                // but on the super-class
                                this._super = _super[name];
    
                                // The method only need to be bound temporarily, so we
                                // remove it when we're done executing
                                var ret = fn.apply(this, arguments);
                                this._super = tmp;
    
                                return ret;
                            };
                        })(name, prop[name])
                        : prop[name];
            }
    
            // The dummy class constructor
            function Class() {
                // All construction is actually done in the init method
                if (!initializing && this.init)
                    this.init.apply(this, arguments);
            }
    
            // Populate our constructed prototype object
            Class.prototype = prototype;
    
            // Enforce the constructor to be what we expect
            Class.constructor = Class;
    
            // And make this class extendable
            Class.extend = arguments.callee;
    
            return Class;
        };
    })();
    

    Plugin Creation

    (function ($) {
    
        //  The "inheritance plugin" model
        //  [http://alexsexton.com/?p=51][1]
    
        $.plugin = function (name, object) {
            $.fn[name] = function (options) {
                var instance = $.data(this, name, new object(this, options));
                return instance;
            };
        };
    })(jQuery);
    

    Calling your plugin from javascript:

    $('#someElem').my_plugin({options: {}, data: {} /* you can modify your plugin code to accept anything */}).show();
    

    Note:

    Private methods here are marked as _methodName. It’s pure convention. If you really want to hide them, you can use module pattern (google for it or here’s one for starters: http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth)

    Is this what you’re looking for?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.