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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:49:55+00:00 2026-06-17T08:49:55+00:00

I see a lot of code like: var myApp ={}; (function() { console.log(Hello); this.var1

  • 0

I see a lot of code like:

var myApp ={};
(function() {
    console.log("Hello");
    this.var1 = "mark";     //"this" is global, because it runs immediately on load.  Caller is global
    myApp.sayGoodbye = function() {
        console.log("Goodbye");
    };
})();

Which causes the anonymous function to execute immediately. But what is the advantage of this, compared to just putting the code inline?

var myApp ={};
console.log("Hello");
var1 = "mark";     
myApp.sayGoodbye = function() {
    console.log("Goodbye");
};

Apparently it’s to do with scope of the function, but as the function is anonymous and called by window, it’s scope (i.e. this) is global, no?

  • 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-17T08:49:56+00:00Added an answer on June 17, 2026 at 8:49 am

    Usually, you would have this :

            var myApp ={};
            (function() {
                console.log("Hello");
                var var1 = "mark";  
                myApp.sayGoodbye = function() {
                    console.log("Goodbye");
                };
            })();
    

    The main difference is that var1 doesn’t clutter the global namespace. After this call, var1 is still the same than before (generally undefined).

    As var1 can only be accessed from the function defineds in the closure, it is said “private”.

    Apart avoiding possible causes of conflicts, it’s just cleaner not to keep global variables when useless.

    Here, you don’t have a local variable but a global one defined as this.var1. It’s probably a bug, or the reason would be found elsewhere in the code.

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

Sidebar

Related Questions

In a lot of TDD tutorials I see code like this: public class MyClass
I see a lot of code similar to the following var customrs = MyDataContext.Customers.Where(...);
This code I am looking at has a lot of places where I see
In a lot of code, it's very common to see an init function be
My code is something like this: function attr(el, attr, value) { el.setAttribute(attr, value); }
I see a lot of code uses automatically generated property like {get; private set;}
I see a lot in autoconf code about stuff being dnl'ed and not dnl'ed.
I see a lot of people are using code under viewDidLoad to be able
I see a lot of objective-c code that just #defines local constants it needs,
I see a lot of this: 06-19 17:29:11.911: DEBUG/dalvikvm(10028): GC_FOR_MALLOC freed 729K, 54% free

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.