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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:54:20+00:00 2026-05-23T08:54:20+00:00

I have two situations in each testcases. I want to understand the difference and

  • 0

I have two situations in each testcases. I want to understand the difference and want to understand closures more.

test 1:

somefunction(someobj);

and

somefunction(function(){ return someobj; });

test 2

for(;;){
  someoperations;
}

and

for(;;)(function(iterator){
  someoperations;
})(iterator);

test 3:

var x = (function() {
  return {};
})();

and

(function() {
  //this = window
  var x = function() {
    //this = instance of x
    this.something = somethingelse;
  }
  //making it global OR making it available outside closure.
  return (this.x=x);
})();

I need an explanation of what is the advantage of using it in the second way in each testcase.

  • 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-23T08:54:20+00:00Added an answer on May 23, 2026 at 8:54 am

    In javascript, the only way to create a new variable scope is in a function.

    Whether or not the new scope is needed will depend entirely upon the circumstance. As such, the generic examples you presented don’t allow a definitive answer.


    To give something more concrete, take we can your test 2, and apply this very common scenario where the someoperations is an asynchronous call like a setTimeout:

    for( var i=1; i<4; i++ ) setTimeout( function() { alert( i ); }, 1000 * i);
    

    The trouble is that each function created in the loop (and passed to the setTimeout) references the same i variable, and because it is a setTimeout, which is non-blocking, the loop finishes in its entirety before any of the functions created in the loop are ever invoked.

    The result is that each function will alert 4 since that was where the value of i was left after the loop.

    Example: http://jsfiddle.net/Ng3rr/


    On the other hand, if you invoke a function inside the loop that sets up your setTimeout, passing the value of i to that function, then the variable referenced by each function sent to setTimeout will be the local inner_i, which referenced the value that was passed into that outer invocation.

    (I called it inner_i to differentiate between the two, but you could name the inner variable i as well. Doing so is called variable shadowing.)

    for( var i=1; i<4; i++ )(function( inner_i ){
      setTimeout( function() { alert( inner_i ); }, 500 * inner_i);
    })( i );
    

    Now each alert displays the value that was received during each iteration.

    So as you can see, it depends entirely on the situation.

    Example: http://jsfiddle.net/Ng3rr/1/


    To add some contrast to the examples above, if we didn’t run any asynchronous code in the loop, the outer function would be unnecessary.

    This:

    for( var i=1; i<4; i++ ) alert( i );
    

    Example: http://jsfiddle.net/Ng3rr/2/

    …and this:

    for( var i=1; i<4; i++ )(function( inner_i ){
        alert( inner_i );
    })( i );
    

    Example: http://jsfiddle.net/Ng3rr/3/

    …will have identical behavior, making the outer function unnecessary.


    So invoking an outer function creates a new variable scope. In that new scope, the variables will be retained by any additional nested functions created in that scope.

    Also, while variables created in that scope are accessible in the functions nested in that scope, they’re unavailable outside that scope. This keeps you from polluting the surrounding scope with additional variable names.

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

Sidebar

Related Questions

I have two applications written in Java that communicate with each other using XML
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two classes, and want to include a static instance of one class
Say I have two tables, users and groups each of which has an auto-incrementing
I have a situation where I have two entities that share a primary key
I have a situation where I have two models, companies and permissions, where companies
I have a rather classic UI situation - two ListBoxes named SelectedItems and AvailableItems
Imagine a situation, I have PC with two lan cards, one is connected to
I have two arrays of animals (for example). $array = array( array( 'id' =>
I have two elements: <input a> <input b onclick=...> When b is clicked, I

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.