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

The Archive Base Latest Questions

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

I was testing the callback mechanism in node.js to see the context in which

  • 0

I was testing the callback mechanism in node.js to see the context in which a callback is fired. While running the following code I’ve noticed a strange behavior and I wonder if you could explain:

var outID =2;

var closure = function (){
    var that = {};
    that.id = 1;
    outID = 3; //if line is commented out outID will always be 2.
    that.inside  = function(cb){
        console.log("level 0");
        console.log("thatID:" +that.id);
        console.log("outID:" +outID);
        setTimeout(function(){
            console.log("level 1");
            console.log("thatID:" +that.id);
            console.log("outID:" +outID);
            setTimeout(function(){
                setTimeout(cb,0,that.id);
            },0);
        }, 0);
    };
    return that;
};
var level3  = function(id){
    console.log("level 100S");
    console.log("id " + id);
    console.log(outID); // --- Interesting value is 3.
};
var cl = new closure();
cl.inside(level3);

Output is:

node: no process found
level 0
thatID:1
outID:3
level 1
thatID:1
outID:3
level 100S
id 1
3
[Finished in 0.1s]

Why is the last value 3 and not 2 ?

  • 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-11T08:55:41+00:00Added an answer on June 11, 2026 at 8:55 am

    outID is declared (i.e., using the var keyword) at the top level scope, and never redeclared in any other (function) scope. This means that when it’s assigned to, anywhere, it’s writing to the same variable, and when it’s referenced, anywhere, it’s reading from the same variable.

    To keep the outID=3 line in the inner function from changing the value printed last, you’d change it to var outID=3.

    Edit:

    The code posted has the following scopes:

    global 
        closure
             that.inside
                 $anonymous1 (outer setTimeout argument)
                      $anonymous2 (inner setTimeout callback)
        level3
    

    As that hopefully makes clearer, a functions scope inherits from the scope in which it is defined, not the scope in which it is called. Of course, you might be conflating scope with the value of this, which is another story…

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

Sidebar

Related Questions

Today I am testing callback function in node.js My code is function callback_test(callback) {
Testing out someone elses code, I noticed a few JSP pages printing funky non-ASCII
See my test code using mocha + tamejs: test/t.tjs require('should'); function inc(n, callback) {
I have the following callback: ExternalInterface.addCallback(wasCalledFromJS, callFromJavaScript); which is implemented like this: public function
I'm testing a model with an after create callback that I'd like to run
I'm trying to unit test, using VS unit testing facility following method. void Get(string
I'm experiencing a very strange issue: I use the NUnit framework for testing in
I'm building a hybrid mobile application (PhoneGap+JQM). While testing in the Android emulator, I'm
How do you setup correct scoping in a QUnit test environment for testing callback
There is a well-known problem when unit testing managed C++ code in Visual Studio:

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.