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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:08:55+00:00 2026-05-23T12:08:55+00:00

This code results in ! being logged on the console. var g = {};

  • 0

This code results in "!" being logged on the console.

var g = {};
(function() {
    var t = this;
    t.x = "x";
    g.a = function() {
        console.log(t.x);
    };
})();

(function() {
    var t = this;
    t.x = "!";
    g.b = function() {
        console.log(t.x);
    };
})();

g.a();

Do anonymous functions share a this? Am I using this wrong? I don’t really understand what’s going on here.

I’d like for g.a() to continue returning the value of x defined in the first anonymous function.

I’m using node.js if it makes a difference.

  • 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-23T12:08:55+00:00Added an answer on May 23, 2026 at 12:08 pm

    In the immediate functions, this refers to the global object [docs]. So in this case in both functions this indeed refers to the same element and you are overwriting x with the second call.

    What object this refers to is determined by how the function is called.

    • If you just execute a function with funcName();, then this refers to the global object.
    • If the function is assigned to a property of an object, obj.funcName() , this refers to the object.
    • If you call the function with the new operator, new funcName();, this refers to an empty object that inherits from the functions prototype.

    You can also explicitly set this by using call [docs] or apply [docs].


    Instead referring to this, you could create a new object in both functions:

    var t = {};
    

    Additional note: It makes no difference whether you run the code in the browser or with node.js. The global object is part of the specification and has to be provided by the execution environment. In browsers it is the window object, I don’t what it is in node.js, but it does not matter as long as it follows the specification.

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

Sidebar

Related Questions

Using this code, the following execution yields strange results: C 100 R W The
This XML code results in the checkbox and text being on the same line
I have this code that performs an ajax call and loads the results into
I've got this code snippet, and I'm wondering why the results of the first
here is my current line of code: $(<li>).text($(this).text()).appendTo(#theList); Which results in <ul id=theList> <li>blah</li>
This code produces the result as 56. function x ($y) { function y ($z)
This code, when compiled in xlC 8.0 (on AIX 5.3), produces the wrong result.
I'm currently using this code NSHost *host = [NSHost hostWithAddress:hostname]; if (host == nil)
I have seen it discussed somewhere that the following code results in obj being
I have this code: partialProg = 100 * ((i+1) / len); Log.i(null, 100 *

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.