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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:34:31+00:00 2026-06-15T07:34:31+00:00

What would be the difference between (function($){ $(document).ready(function() { $(this)/*… jQuery functions */ });

  • 0

What would be the difference between

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

        $(this)/*... jQuery functions */

    });
})(jQuery);

and this?

(function(jQuery){
    jQuery(document).ready(function() {

        var $ = jQuery;
        $(this)/*... jQuery functions */

    });
})(jQuery);

and when should I use each?

  • 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-15T07:34:33+00:00Added an answer on June 15, 2026 at 7:34 am

    The main difference between the two examples is that one is in a more local scope. In the first example, your redefinition is at a higher scope than in the second example where it’s a local variable. Theoretically, the one in local scope might be slightly faster to use, but whether that is a meaningful difference or would depend upon a lot of factors.

    Each nested function declaration has it’s own set of local variables. When the javascript interpreter goes to find the value of a variable like $, it looks first for it in the most local scope (the local variables of the function that is being executed at the moment). If it doesn’t find it there, it goes up the chain looking for other variables in a higher scope. If it is not found at the next higher level of scope, it keeps going up the chain. Eventually, it gets to the global scope and searches the global scope for the desired symbol. If it isn’t found there, then it is undefined.

    In your first code example, $ will be found at this higher scope as it is not directly a local variable of the document.ready callback function that was executing. In your second example, it will be a true local variable so it will be found slightly quicker.

    In addition, both cases assign $ to a non-global variable within your scope which protects it from begin hijacked by outside code.

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

Sidebar

Related Questions

What the difference between jQuery's functions val() and text() ? Where would you use
what would be the difference between this class Class1 { public string prop1 {
I currently have a jQuery Slider in progress: http://jsfiddle.net/hQj8a/ $(document).ready(function() { $('body').removeClass('no-js'); $('#my-carousel-2').carousel({ itemsPerPage:
Can you you tell me what would be the difference between of the ID:
If I extend a component like Form in Vaadin, would be any difference between
Would you please explain me the difference between these two CSS classes syntax: .element
I would like to know what is the difference between Sleep mode and Deep
I would like to ask whats the difference between for row in session.Query(Model1): pass
I would like to know what the difference between these instructions is: MOV AX,
What is the difference between those two AJAX calls and why would I choose

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.