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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:22:02+00:00 2026-06-08T10:22:02+00:00

Here is a common practice in JavaScript: (function($) { …code… })(jQuery); I understand the

  • 0

Here is a common practice in JavaScript:

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

I understand the wrapper function (it prevents pollution of the global namespace), but many libraries (like jQuery, Underscore, etc.) already define short names ($ and _, respectively) at global scope for me to use. I wonder what the advantage to this approach is. Just to rename jQuery to something shorter? Prevent me from overwriting $? Make it easier to swap in another library later? I guess none of these seem really convincing to me.

Furthermore, I have also seen this:

(function(_) {
    ...code...
})(_);

Nothing is even renamed here. I have even seen:

(function(global) {
    ...code...
})(this); // or window, perhaps

What is wrong with just using window directly?

So here’s what I’m asking:

  • Does this practice have a name?
  • What are the advantages to this practice?
  • Should I always pass in libraries I’m using rather than use them directly?
  • Should I pass in this or window as a reference to global scope?
  • 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-08T10:22:06+00:00Added an answer on June 8, 2026 at 10:22 am

    Does this practice have a name?

    The syntax is referred to as a self-executing anonymous function. I’m not aware of any special name for passing global objects to the function.

    What are the advantages to this practice?

    • As you’ve noted, var scoping variables within the function will
      help de-clutter the global namepace.
    • The jQuery example is typically used in plugins so that the plugin can make use of $ while remaining compatible with $.noConflict() (docs)

      (function($) {
          ...code...
      })(jQuery);
      
    • In general, passing global or reserved objects (like window, document) as parameters can help reduce the size of your script after minification:

      (function(window, document) {
          // A JS minifier can now minify the all occurrences of `window` and
          // `document` within this function.
      })(window, document); 
      

    Should I always pass in libraries I’m using rather than use them directly?
    Should I pass in this or window as a reference to global scope?

    Only if minification or conflicting library variable names are a concern.

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

Sidebar

Related Questions

Here's a common practice I see often (including from a very popular iPhone developer
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
The previous question is here : what is the common practice on limit the
In a way I am looking for best-practice here. I have a common project
Common practice might be to put asserts in code to check input parameters, data
I have read ( see here ) that common practice to print a stack
Here's a common, simple task: Read configuration settings from a configuration file, save the
Here's a problem I've had recently that just HAS to be a common pain
Here's (i think) an interesting question. With AJAX more and more common i feel
I am getting the common ASP.NET YSOD error. Here is the full error page

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.