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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:10:11+00:00 2026-05-31T00:10:11+00:00

I just recently learned about self calling anonymous functions. Some snippets of code that

  • 0

I just recently learned about self calling anonymous functions. Some snippets of code that I came across used the self calling function along with the $(document).ready. It seems redundant, or pointless, to use both.

Is there a case where you’d use

(function(){
   $(document).ready();
})();

vs.

$(document).ready(function(){
   (function(){})();
});

I figure that you’d either want the script to be executed right away or after the DOM loaded. I can’t see why you’d use both together.

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-05-31T00:10:12+00:00Added an answer on May 31, 2026 at 12:10 am

    There’s definitely a use case for the first example. If you have other JS libraries/scripts loaded on the same page, there’s no guarantee that they aren’t overwriting the $ variable. (This is very common when you have Prototype and jQuery on the same page).

    So if Prototype is using $, you would need to use jQuery anytime you wanted to work with jQuery. This can get quite ugly:

    jQuery(function(){
        jQuery('a', '#content').bind('click', function(){
            if(jQuery(this).attr('href') == 'www.google.com')
            {
                alert("This link goes to Google");
                jQuery(this).addClass('clicked'));
            }
        });
    })
    

    Remember, we can’t use $ because that is a method from Prototype in the global scope.

    But if you wrapped it inside of this..

    (function($){
        $(function(){
    
            // your code here
    
        });
    })(jQuery);
    

    The $ would actually reference the jQuery library inside while still referring to Prototype on the outside! This can help tidy up your code:

    (function($){
        $(function{}(
            jQuery('a', '#content').bind('click', function(){
                if(jQuery(this).attr('href') == 'www.google.com')
                {
                    alert("This link goes to Google");
                    jQuery(this).addClass('clicked'));
                }
            });
        ));
    })(jQuery);
    

    This is a common pattern with jQuery extensions to ensure that they are always added to the jQuery object, but can be written using $ to keep the code tidy.

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

Sidebar

Related Questions

I just recently learned about Scope Guard C++ idiom. Unfortunately I can't find any
I've recently started learning C#. I just learned about properties and decided to make
I've only just recently learned about Google's programming language, Go. I've been intrigued by
I recently learned that Unicode is permitted within Java source code not only as
I just recently learned about the Properties class in java, and I'm trying to
I've recently learned that the % sign is used to calculate the remainder of
I recently learned about the , operator and the fact that it introduces a
I've just recently learned the PureMVC framework, and am a little confused as to
I just recently started diving into Real World Haskell and the book provides some
I just recently heard of duck typing and I read the Wikipedia article about

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.