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 8624669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:35:16+00:00 2026-06-12T07:35:16+00:00

I am working on a jQuery plugin and am a bit confused on sharing

  • 0

I am working on a jQuery plugin and am a bit confused on sharing functions and variables between methods within the same namespace. I know that the following will work:

    (function($){

    var k = 0;
    var sharedFunction = function(){
                //...
                }

    var methods = {

    init : function() { 
      return this.each(function() {
          sharedFunction();
        });
    },

     method2 : function() { 
      return this.each(function() {
          sharedFunction();
        });
    }
  };

$.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 here');
    }    
  };

})(jQuery);

However, I am wondering if there’s a better way to do this. While I understand that the variable “k” and the function “sharedFunction” are not technically global (in that they cannot be accessed directly outside of the plugin), this seems unsophisticated at best.

I know that $.data is an option, but if you have a great deal of variables and functions that need to be accessed by multiple methods in the plugin, this seems like it could turn into a tremendous mess.

Any insights would be appreciated. Thanks!

  • 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-06-12T07:35:18+00:00Added an answer on June 12, 2026 at 7:35 am

    One of the (arguably) more common pitfalls in Javascript is that { } do not define scope as other C-style languages; functions do.

    With that in mind, save for making your variables global, there are two ways (I generally use) to share variables between two separate functions:

    Declare the functions inside a common function

    This is what you’re demonstrating above. You’re declaring your two functions inside another function (which defines the scope). Anything declared in child-level of the container function is available anywhere in its scope, including the scopes of your two inner functions.

    // this is a self-calling function
    (function () {
    
        var foo;
    
        var f1 = function () {
            // foo will be accessible here
        },
    
        f2 = function () {
            // ... and foo is accessible here as well
        }
    
    })();
    

    Honestly, this isn’t “unsophisticated” at all, and is commonly done in lieu of not being able to define scope other than function scope in Javascript.

    Namespacing common members

    You can define an object in global scope, and then just extend that with your variables / functions. You do have to go global, but you’re minimizing your footprint by ensuring that you only do it once.

    window.app = {
        foo : 'bar'
    };
    
    (function () {
    
        var f1 = function () {
            // app.foo will be accessible here
        };
    
    })();
    
    (function () {
    
        var f2 = function () {
            // ... and here as well, even though we're 
            // in a totally different (parent) scope
        };
    
    })();
    

    Using $().data() may seem viable, but while it definitely has its uses, I somehow can’t recommend tacking on that additional overhead for providing the functionality you describe when it can be as easily (and natively) achieved by simple language mechanics (albeit, readability takes a bit of getting used to).

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

Sidebar

Related Questions

I am working with a jQuery plugin that has the following snippet in it:
I'm working with a jQuery plugin that has this: var $el = $(this), $wrapper
I have been working on a jquery plugin that uses a HTML5 audio player
I am working on a jQuery plugin that people can include in their own
I'm new to jQuery. Working with jQuery validation plugin & cufon at the same
I am working with a search form that using the jqTransform jquery plugin. Its
I have been working on a jQuery plugin that involves CSS Shapes . The
i am working on struts2-jquery plugin . the below snippet is working fine in
I am working on a jquery plugin which takes json data from the php
I am trying to get this awesome JQuery plugin working with SQL Server. I

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.