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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:03:36+00:00 2026-06-08T02:03:36+00:00

I’m learning jQuery, and am attempting to understand the following code structure. if(jQuery) (function($){

  • 0

I’m learning jQuery, and am attempting to understand the following code structure.

if(jQuery) (function($){
    $.extend($.fn, {
        MyPlugin: function(a, b) {
        ....        
        }
    });
})(jQuery);

From what I can tell, this first checks to see if the jQuery object exists. If it does, it defines an inline function, which extends the jQuery object using the ‘fn’ attribute.

I’ve seen this question asked in a couple other places, for example here, but the points I’m not understanding haven’t been addressed. Where I’m getting confused…

-why is the special jquery “$” object passed to the initial function?

-why is the inline function passed the jQuery object (on the last line)? ie: (function($) {…})(jQuery);

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-08T02:03:39+00:00Added an answer on June 8, 2026 at 2:03 am

    Simple explanation

    It just creates a closure where you can reference jQuery with $, even if it’s in noConflict mode.

    Also explained in jSang’s answer (+1)’s plugin authoring.

    Technically, it just defines an Anonymous function which takes one argument called $ and immediately calls it passing jQuery as the only argument, so $ serves as an alias to jQuery inside this function’s scope – note that the $ in the global scope is not accessible inside this closure then.

    It means, if you had Prototype using $ in the global scope, you wouldn’t be able to access Prototype using $ inside the closure, as the local scope $ which references jQuery takes precedence.

    Of course, if you need another library’s methods inside your closure, you can define it using 2 arguments: (function($j, $p) { and call it passing (jQuery, $) assuming another library is using the $ namespace. Or more simply, alias your closure’s local scope var to something differently than your other library’s global scope var: (function($j) {, which means the global $ will still be accessible inside the function’s scope. Note that this latter effect may be undesirable as you can’t be completely sure of what your user has assigned for $ unless you explicitly write it in your documentation. I guess plugins which require multiple libraries are a little off-topic so let’s leave it at that.


    Slightly more detailed

    A practical closure example from the noConflict docs page:

    jQuery.noConflict();
    (function($) { 
      $(function() {
        // more code using $ as alias to jQuery
      });
    })(jQuery);
    

    That closure aliases jQuery as $ inside its scope, very useful in case your user has Prototype or another library using the $ global namespace, for instance. This makes your plugin much more sturdy and less error-prone.

    The closure can be used whether jQuery has control of the $ variable or not, hence it’s ideal for plugin authoring.

    Imagine if one of your end-users had jQuery in noConflict mode and Prototype using the $ namespace, and your plugin tried to extend $ with jQuery methods? The Chaos and Ponies!

    Of course, you could skip the closure and replace all $ with jQuery, but it’d make your code longer and less readable to many developers.

    Also, How do JavaScript closures work? has many useful answers if you have trouble understanding closures, even though this question shouldn’t require too much in-depth knowledge about closures. :)

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
Does anyone know how can I replace this 2 symbol below from the string
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is

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.