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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:39:31+00:00 2026-05-27T10:39:31+00:00

Consider the following code: <script> var i = 0; function test() { var _this

  • 0

Consider the following code:

<script>
    var i = 0;
    function test() {
        var _this = this;
        // foo and _this.foo are set to the same number
        var foo = _this.foo = i++;

        function wtf() {
            console.log(_this.foo, foo);
        }

        $("#thediv").click(wtf);
    };
    test();
    test();
    test();
</script>

It seems that console.log(_this.foo, foo) should always output equal numbers (i).

But clicking the div outputs 3 lines (for each console.log call):

2 0
2 1
2 2

It seems that _this.foo always refers to last this.foo. Why it is so?

  • 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-27T10:39:32+00:00Added an answer on May 27, 2026 at 10:39 am

    When test() is run, this is a reference to window for each of your three test() calls, so you are actually updating window.foo when you assign to _this.foo and referencing window.foo in your console.log.

    Then, when wtf() is invoked, the _this variables in each of the wtf() closures are the same – they all point to window

    See this jsfiddle: http://jsfiddle.net/8cyHm/

    I added some additional parameters to console.log to show what is happening

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

Sidebar

Related Questions

consider the following code: <script> function testFunc(){ alert('test') } $(function(){ var g = document.getElementById
Consider the following piece of code. <html> <body> <script> var x = 5; //globally
Consider the following code: (live example here ) $(function() { var wrapper = $(<div
Consider the following piece of code: <html><head></head> <body> <script type=text/javascript> var outside_scope = outside
Consider the following code: $(a).attr(disabled, disabled); In IE and FF, this will make anchors
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
Please consider the following code and the explanation from this Mozilla tutorial Using web
Consider the following html snippet <!DOCTYPE html> <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.js ></script> <script
This is rather interesting, I think. Consider following code, both the window.onload and body
Consider following JavaScript code (tested in Firefox): function f(a) { if (a == undefined)

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.