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

  • SEARCH
  • Home
  • 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 6342687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:17:18+00:00 2026-05-24T20:17:18+00:00

I’m following the guidelines for plugin authoring on jquery’s website, but I’m having trouble

  • 0

I’m following the guidelines for plugin authoring on jquery’s website, but I’m having trouble figuring out how to call a main plugin method from another method within the same plugin.

I have a plugin like this:

(function($){
var methods = {
    init: function(options) {
        var settings = {};

        //this == context element of plugin, already jquery
        return this.each(function() {
            var $this = $(this);
            if( options ) {
                settings = $.extend({}, settings, options);
            }
            var data = $this.data('PluginData');
            if(!data) {
                //set up                    
            }

        });
    },
    some_fn: function() {
        //do some stuff
    },
    another_fn: function() {
        //do other stuff, then somehow call some_fn(), maybe via methods.some_fn() ?    
    }
};

jQuery.fn.SomePlugin = function(method) {
    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 {
        console.log('there was an error');
    }
};
})(jQuery);

This is pretty much the skeleton code from jquery. However, what I’m having trouble with is figuring out the best way to create a “utility” function that is just for my plugin methods, or how to call one plugin method from another.

For example, in my plugin, I have 3 methods, init, some_fn, and another_fn. When I call $('#el').SomePlugin('another_fn'), within another_fn, I’d like to call some_fn. How can I do this? Calling methods.some_fn() will probably work, however, then this is dependent on the order the methods are defined within the methods object, correct? So I could call some_fn from another_fn, but not vice versa?

Additionally, what’s the correct way to create a utility function that all the methods in my plugin can use, so that I am not cluttering the global namespace? Do I just define the utility functions at the beginning of my plugin, right before the call to var methods?

EDIT: Thanks to Matt Ball, I have confirmed that methods.some_fn() DOES work for calling other main methods. Now I just want to know what the best practice for creating a (private) utility function is

  • 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-24T20:17:19+00:00Added an answer on May 24, 2026 at 8:17 pm

    For a best practice you should check out: http://jqueryboilerplate.com
    They give examples for exactly your question. 🙂

    For your example you could take advantage of the scope of the init function:

    (function($){
    var methods = {
        init: function(options) {
            var settings = {};
    
            var privateMethod = function(){ ... }
    
            //this == context element of plugin, already jquery
            return this.each(function() {
                var $this = $(this);
                if( options ) {
                    settings = $.extend({}, settings, options);
                }
                var data = $this.data('PluginData');
                if(!data) {
                    //set up                    
                }
    
            });
        },
        some_fn: function() {
            //call private function
            privateMethod()
            // do some stuff
        }
    };
    
    jQuery.fn.SomePlugin = function(method) {
        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 {
            console.log('there was an error');
        }
    };
    })(jQuery);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I want to count how many characters a certain string has in PHP, but
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.