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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:13:56+00:00 2026-05-13T23:13:56+00:00

What is the best way to create a static variables for jQuery plugins? I

  • 0

What is the best way to create a static variables for jQuery plugins?

I have 2 example use cases to illustrate my thinking so far; each with some ideas.
Any other ideas welcomed of course…

One example is for a static variable containing: animation settings, layout settings, product details, etc; the other for a static variable caching data.

I hope ‘static’ is the correct terminolgy here… single globals. please correct if wrong.

Case 1: for settings

// defining globally
var anObject = {
 value1 = 0;
 value2 = 0;
}

jQuery.anObjectSet(partialObject) {
 anObject = jQuery.extend(anObject, partialObject);
}

jQuery.fn.myPlugin = function (partialObject) {
 obj = jQuery.extend(anObject, partialObject);
}

or maybe?

// adding to the jQuery object
jQuery.anObject = {
 value1 = 0;
 value2 = 0;
}

jQuery.anObjectSet(partialObject) {
 jQuery.anObject = jQuery.extend(jQuery.anObject, partialObject);
}

jQuery.fn.myPlugin = function (partialObject) {
 obj = jQuery.extend(jQuery.anObject, partialObject);
}

or maybe?

jQuery.anObjectSet(partialObject) {
 if(!jQuery.anObject) 
  jQuery.anObject = {
    value1 = 0;
    value2 = 0;
  }

 jQuery.anObject = jQuery.extend(jQuery.anObject, partialObject);
}

jQuery.fn.myPlugin = function (partialObject) {
 if(!jQuery.anObject) 
  jQuery.anObject = {
    value1 = 0;
    value2 = 0;
  }
 obj = jQuery.extend(jQuery.anObject, partialObject);
}

Case 2: for caching

jQuery.fn.myPlugin = function (newObject) {
 if(!cache[newObject])
  cache[newObject] = $(newObject);

 return cache[newObject];
}

or maybe? (I seen this method elsewhere)

window.$cache = {};

jQuery.fn.myPlugin = function (newObject) {
 if(!$cache[newObject])
  $cache[newObject] = $(newObject);

 return $cache[newObject];
}

Thanks. I want to build up a .js library, getting starting on the right track…

  • 1 1 Answer
  • 1 View
  • 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-13T23:13:56+00:00Added an answer on May 13, 2026 at 11:13 pm

    If you are looking to make a “global” or static variable for your library and your library alone, a closure is the way to go.

    A normal global is a bad thing because it pollutes the namespace, and you haven’t any idea who is going to be running your library.

    By defining the static variable inside an anonymous function, we give ourselves access to a a variable that no one can touch and is effectively “global” for the purposes of our functions. If I understand correctly what you are trying to do, this should accomplish it.

    (function($){
        var $cache = {};
    
        jQuery.fn.myPlugin = function (newObject) {
          if(!$cache[newObject])
            $cache[newObject] = $(newObject);
    
          return $cache[newObject];
        }
    
    })(jQuery);
    

    Edit:

    The $cache variable will only be accessible to functions defined within this anonymous function. So if the other functions need to access this file, they must also be defined inside this anonymous function.

    The only alternative to defining everything here (AFAIK) is to have some combination of accessor functions, a simple function defined inside this closure whose purpose is to provide outside functions access to the $cache.

    Preferably, I’d go with defining the necessary functions inside this closure. However that is ultimately an implementation detail, and is up to your discretion.

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

Sidebar

Ask A Question

Stats

  • Questions 368k
  • Answers 368k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer See PEP 302: New Import Hooks, for details on how… May 14, 2026 at 6:10 pm
  • Editorial Team
    Editorial Team added an answer You don't fix this: it isn't broken. Mutating globals, especially… May 14, 2026 at 6:10 pm
  • Editorial Team
    Editorial Team added an answer Essentially. You use a SQL Server Processor license when you… May 14, 2026 at 6:10 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.