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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:07:52+00:00 2026-05-16T17:07:52+00:00

I use the following pattern in my JS: var lib = { module_one: {

  • 0

I use the following pattern in my JS:

var lib =
{
  module_one:
  {
      init: function () {
          ...
      }
  },
  module_two:
  {
      init: function () {
          ...
      }
  }
};

Question is, whats the best way to add:

(function ($) {          
  ...
})(jQuery);

I tried to put it around the var lib, but that didnt work. To add it inside each function worked, but seems a bit messy..?

Is it possible to add it to the init: function($) somehow?

Quite new on jQuery, so if you have any other suggestions around this pattern, let me know 🙂

  • 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-16T17:07:53+00:00Added an answer on May 16, 2026 at 5:07 pm

    Basically, you can do this:

    (function() {
        var global, lib, oldlib;
    
        // We call this anonymous scoping function directly, so we know that
        // within it, `this` is the JavaScript global object. Grab a
        // local reference to it so we can use it within functions that get
        // called via dotted notation and so have different `this` values.
        global = this;
    
        // Remember any value that already exists for the `lib` property
        // of the global
        oldlib = global.lib;
    
        // Define our lib, assigning it to a local variable
        lib = {
            /* ...your stuff as usual, plus: */
    
            noConflict: function() {
                global.lib = oldlib;
                return lib;
            }
        };
    
        // Publish our lib externally on the global object
        global.lib = lib;
    })();
    

    …which can then be used like this:

    var alias = lib.noConflict();
    

    Here’s how that works:

    1. We define a scoping function and then immediately call it.
    2. Within the scoping function, we grab the this value as a variable called global. This will be the JavaScript global object because of the way we’re calling the scoping function. (The global object on browsers is window, but there’s no need to limit this to browsers, hence getting global this way).
    3. The first thing we do is save any old value the lib property of the global object had, in a local variable in our scoping function called oldlib.
    4. We set up our new value for lib.
    5. Our noConflict function restores the earlier value of the lib property, and returns our lib reference so someone can use it as an alias.

    BTW, when you use a scoping function, you can also switch over to using named functions rather than anonymous ones, which has several benefits. Here’s the above updated to use a named function for noConflict.

    (function() {
        var global, lib, oldlib;
    
        // We call this anonymous scoping function directly, so we know that
        // within it, `this` is the JavaScript global object. Grab a
        // local reference to it so we can use it within functions that get
        // called via dotted notation and so have different `this` values.
        global = this;
    
        // Remember any value that already exists for the `lib` property
        // of the global
        oldlib = global.lib;
    
        // Define the functions for our lib. Because they're defined
        // within our scoping function, they're completely private
        function lib_noConflict() {
            global.lib = oldlib;
            return lib;
        }
    
        // Define our lib, publishing the functions we want to be public
        lib = {
            /* ...your stuff as usual, plus: */
    
            noConflict: lib_noConflict
        };
    
        // Publish our lib externally on the global object
        global.lib = lib;
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just use following code to add an image to my project, var paper
Let's say I made use of the following module-kind pattern in JavaScript: var myModule
I saw the following pattern and tried adapting it to my own use: var
What is the best design pattern for achieving the following (which doesn't work)? var
I frequently use the following pattern to set an upper bound to the running
I try use string as a regular expression pattern but I've following errors PHP
When i use following: var child:DisplayObjectContainer = dso.getChildAt( dso.numChildren -1 ) as DisplayObjectContainer; trace(child);
In principle, I want to use the following to generate a named pattern for
In an app I am building I use the following pattern: If the user
Given this function: function Repeater(template) { var repeater = { markup: template, replace: function(pattern,

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.