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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:10:45+00:00 2026-05-25T02:10:45+00:00

It is generally accepted that Javascript code should be wrapped in a function to

  • 0

It is generally accepted that Javascript code should be wrapped in a function to prevent leaking to the global scope and just assign whatever you need outside of each function to the head object (window in web browsers).

I’ve seen two primary methods of this in the wild:

Method 1:

(function() {
  // code here
}).call(this);

Method 2:

(function() {
  // code here
})();

Method 1 is from compiled CoffeeScript code, and Method 2 seems to be the preferred style for jQuery plugins:

Modified Method 2 for jQuery:

(function($) {
  // JQuery Code
})(jQuery);

Question: What is the difference between Method 1 and Method 2? CoffeeScript likes to focus on conciseness, so I figure there must have been a reason for the people behind CoffeeScript to choose Method 2 over Method 1.

  • 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-25T02:10:45+00:00Added an answer on May 25, 2026 at 2:10 am

    They are very different.

    Method 1 using call, allows you the change what this is inside the function. For example:

    (function() {
        alert(this); // DOMWindow
    }).call(window);
    
    
    var myObj = {a: 0};
    (function() {
        alert(this); // Object => myObj {a: 0}
    }).call(myObj);
    

    Be careful though as you can still dump stuff on the global scope with both approaches:

    (function () {
        a = 5; // global
        var p = 10; // private to this function
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It is generally accepted that the use of cursors in stored procedures should be
When writing application code, it's generally accepted that premature micro -optimization is evil, and
Is it generally accepted that you cannot test code unless the code is setup
It's generally accepted that copy and paste programming is a bad idea, but what
With SQLServer, it seems to be generally accepted that adding a SELECT SCOPE_IDENTITY() to
For the past few years, I've generally accepted that if I am going to
It is generally accepted (I believe!) that a lock will force any values from
Are there any generally-accepted coding standards (naming, casting etc) that apply specifically to iPhone/Cocoa/Objective-C?
The generally accepted answer is that you can't. However there is mounting evidence that
I'm interested in the computational power of neural nets. It is generally accepted that

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.