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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:17:06+00:00 2026-06-07T10:17:06+00:00

I saw this code (explicly it’s in jQuery, with modification) (function(window,undefined){ var jQuery=(function(){ var

  • 0

I saw this code (explicly it’s in jQuery, with modification)

(function(window,undefined){
    var jQuery=(function(){
        var jQuery=something;
        jQuery.xxx=xxx;
        //...
        return jQuery;
    })();
    //...
    window.jQuery=window.$=jQuery;
})(window);

While I understand wrapping code in a inline function call can clearly define the variable scope, I don’t understand the benefits of

  1. passing window with a parameter instead of using it directly,
  2. getting an instance of undefined by a undefined parameter, and also
  3. defining jQuery by the return value of another inline function call.
    Can somebody explain a bit?

EDIT write #3 more clearly:

What I understand is that the code defines jQuery inside another function then return it.

//(function(window,undefined){
var jQuery=(function(){
    // Inside this function defines jQuery and return it?
    var jQuery=function(selector,context){
        return new jQuery(selector,context); //simplified
    };
    jQuery.xxx=xxx;
    //...
    return jQuery;
})(); // This executes the inline function and assign `jQuery` with the return value???
//... })(window);

This is more like the following:

function define_jQuery(){
    // Inside this function defines jQuery and return it?
    var jQuery=function(selector,context){
        return new jQuery(selector,context); //simplified
    };
    jQuery.xxx=xxx;
    //...
    return jQuery;
}

//(function(window,undefined){
var jQuery=define_jQuery(); // This executes the inline function and assign `jQuery` with the return value???
//... })(window);

Wouldn’t it be more simpler to do:

//(function(window,undefined){
var jQuery=function(selector,context){
    return new jQuery(selector,context); //simplified
};
jQuery.xxx=xxx;
//...
//... })(window);
  • 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-07T10:17:08+00:00Added an answer on June 7, 2026 at 10:17 am

    Answering these questions each separately:

    1. Why window passed in? Because dereferencing a variable in JavaScript is painful. Passing in an instance means you don’t have to. Typically the mechanism looks like this:

      (function (window, document, $) {
      }(window, window.document, jQuery));
      

      In this scenario, one need not go to the global scope to dereference any of these three (and jQuery can be in .noConflict() to boot).

    2. This is valid JavaScript: undefined = 2;. I grant that is very foolish, but it is possible. But if one accepts one more argument in a function than is passed, one is confident it is truely undefined and not a hacked copy of it.

    3. Returning jQuery from a previous function allows method chaining: $('#sel').func1().func2(). This is possible because func1 probably looks something like this:

      jQuery.fn.func1 = function () {
          return $(this).each(function () {
              // do something fabulous
          };
      };
      

    return $(this).bla_de_bla() is short-hand for:

        $(this).bla_de_bla(..);
        return $(this);
    

    It also presumes that .bla_de_bla() also returns $(this)

    EDIT: modified #3 to note that it’s best at chaining rather than circumnavigating around .noConflict() and mis-named $.

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

Sidebar

Related Questions

Today i saw this code, that was running in node.js environment. (>node.exe test.js) var
I just saw this code while studying the wordpress source code (PHP), You can
While I'm reading boost/shared_ptr.hpp, i saw this code: // generated copy constructor, destructor are
I saw this in some code: $(<p/>).append(<div>something</div>).appendTo(body); and noticed that it automatically closed the
I recently saw this code on another post ( jQuery Set Cursor Position in
I was reading John Resig's Secrets of JavaScript Ninja and saw this code: function
I saw this code sample without any explanation: var xhr = new XMLHttpRequest(); xhr.open('GET',
I saw this code in another SO post: jQuery UI Autocomplete with ASP MVC
I saw this code before, but I don't know what the meaning: var person1
I saw this code: this.vertical = vertical !== undefined ? !!vertical : this.vertical; It

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.