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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:45:39+00:00 2026-05-14T19:45:39+00:00

If the with statement in Javascript creates a new scope, shouldn’t clicking on the

  • 0

If the with statement in Javascript creates a new scope, shouldn’t clicking on the links show a different x which are in different scopes? It doesn’t.

<a href="#" id="link1">ha link 1</a>
<a href="#" id="link2">ha link 2</a>
<a href="#" id="link3">ha link 3</a>
<a href="#" id="link4">ha link 4</a>
<a href="#" id="link5">ha link 5</a>


<script type="text/javascript">

    for (i = 1; i <= 5; i++) {

        with({foo:"bar"}) {
            var x = i;
            document.getElementById('link' + i).onclick = function() { alert(x); return false; }
        }

    }

</script>
  • 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-14T19:45:40+00:00Added an answer on May 14, 2026 at 7:45 pm

    The with statement doesn’t creates a full new lexical scope, it just introduces an object in front of the scope chain, for example, if you capture the i variable, it will work:

    for (var i = 1; i <= 5; i++) {
      with({x:i}) {
        document.getElementById('link' + i).onclick = function() {
          alert(x);
          return false;
        };
      }
    }
    

    Let me try to explain it better with another example:

    var x = 10, y = 10;   // Step 1
    
    with ({x: 20}) {      // Step 2
    
      var x = 30, y = 30; // Step 3
    
      alert(x); // 30
      alert(y); // 30
    }
    
    alert(x); // 10
    alert(y); // 30
    

    In the Step 1, the x and y variables are declared and they are part of the first object in the scope chain, the global object.

    In the Step 2, a new object ({x:20}) is introduced into the scope chain by the with statement, now the scope chain looks something like this:

       ________              ________
      | x = 10 | <--------- | x = 20 |
      | y = 10 |             ¯¯¯¯¯¯¯¯¯
       ¯¯¯¯¯¯¯¯ 
    

    In the Step 3, another var statement is executed, but it has no effect because as I said before, only functions create a full lexical scope.

    The var statement has no effect, but the assignment has, so when the x variable is resolved, is reached on the first object on the scope chain, the one we introduced using with.

    The y identifier is resolved also, but it is not found on the first object in the chain, so the lookup continues up, and finds it on the last object, the scope chain after the assignments looks like this:

       ________              ________
      | x = 10 | <--------- | x = 30 |
      | y = 30 |             ¯¯¯¯¯¯¯¯¯
       ¯¯¯¯¯¯¯¯ 
    

    When the with statement ends, the scope chain is finally restored:

       ________ 
      | x = 10 |
      | y = 30 |
       ¯¯¯¯¯¯¯¯ 
    

    Edit:
    Let me expand a little bit and talk about functions.

    When a function is created its current parent scope is bound, for example:

    var fn;
    // augment scope chain
    with ({foo: "bar"}) {
      fn = function () { // create function
        return foo;
      };
    }​​
    // restored scope chain
    fn(); // "bar", foo is still accessible inside fn
    

    A new lexical scope is created and added to the scope chain when the function is executed.

    Basically all identifiers (names) of function arguments, variables declared with var and functions declared with the function statement, are bound as properties of a new object created behind the scenes, just before the function itself executes (when controls enters this new execution context).

    This object is not accessible through code, is called the Variable Object, for example:

    var x = 10, y = 10;   // Step 1
    
    (function () {        // Step 2
      var x, y; 
    
      x = 30;             // Step 4
      y = 30; 
    
      alert(x); // 30
      alert(y); // 30
    })();                 // Step 3
    
    alert(x); // 10       // Step 5
    alert(y); // 10
    

    In the Step 1, again as in my first example, the x and y variables are declared and they are part of the first object in the scope chain, the global object.

    In the Step 2, a new function object is created, the parent scope is stored in this moment, into the [[Scope]] of that function, containing now x and y.

    In the Step 3, the function is invoked, starting the Variable Instantiation process, which creates a new object in the scope chain, containing the locally scoped x and y variables declared inside this new function, the scope chain at this moment looks like this:

      parent scope           Variable Object
       ________              _______________
      | x = 10 | <--------- | x = undefined |
      | y = 10 |            | y = undefined | 
       ¯¯¯¯¯¯¯¯              ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯ 
    

    Then in the Step 4, the assignment of x and y is done, but since the new lexical scope has been created, it doesn’t affect the outer values.

      parent scope           Variable Object
       ________              ________
      | x = 10 | <--------- | x = 30 |
      | y = 10 |            | y = 30 | 
       ¯¯¯¯¯¯¯¯              ¯¯¯¯¯¯¯¯ 
    

    And finally, in the Step 5, the function ends and the scope chain is restored to its original state.

       ________ 
      | x = 10 |
      | y = 10 |
       ¯¯¯¯¯¯¯¯ 
    

    Recommended lectures:

    • ECMA-262-3 in detail. Chapter 4: Scope chain
    • Scope Chain and Identifier Resolution
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 396k
  • Answers 396k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's a perfectly valid statement, not sure what your IDE… May 15, 2026 at 2:57 am
  • Editorial Team
    Editorial Team added an answer ActiveRecord::Relation is a fairly weak wrapper around Base#find_by_sql, so :include… May 15, 2026 at 2:57 am
  • Editorial Team
    Editorial Team added an answer see this page for a list, but be warned, some… May 15, 2026 at 2:56 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.