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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:04:20+00:00 2026-05-14T14:04:20+00:00

I’ve been reading through quite a few articles on the ‘this’ keyword when using

  • 0

I’ve been reading through quite a few articles on the ‘this’ keyword when using JavaScript objects and I’m still somewhat confused. I’m quite happy writing object orientated Javascript and I get around the ‘this’ issue by referring the full object path but I don’t like the fact I still find ‘this’ confusing.

I found a good answer here which helped me but I’m still not 100% sure. So, onto the example. The following script is linked from test.html with <script src="js/test.js"></script>

if (!nick) {
    var nick = {};
}

nick.name= function(){
    var helloA = 'Hello A';
    console.log('1.',this, this.helloA);

    var init = function(){
        var helloB = 'Hello B';
        console.log('2.',this, this.helloB);
    }

    return {
        init: init
    }
}();

nick.name.init();

What kind of expected to see was

1. Object {} nick.name, 'Hello A'
2. Object {} init, 'Hello B'

But what I get is this?

1. Window test.html, undefined
2. Object {} init, undefined

I think I understand some of what’s happening there but I would mind if someone out there explains it to me.

Also, I’m not entirely sure why the first ‘console.log’ is being called at all? If I remove the call to the init function //nick.name.init() firebug still outputs 1. Window test.html, undefined. Why is that? Why does nick.name() get called by the window object when the html page loads?

Many thanks

  • 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-14T14:04:20+00:00Added an answer on May 14, 2026 at 2:04 pm

    Also, I’m not entirely sure why the first ‘console.log’ is being called at all?

    nick.name = function(){
        // ...
    }();
    

    Here you define a function, call it immediately (hence ()) and assign its return value ({init: init}) to nick.name

    So the execution is:

    1. Create a variable called nick if there isn’t one with a non-falsey value already
    2. Create an anonymous function that…
    3. Creates a variable called helloA in its own scope
    4. Outputs data using console.log containing “1” (as is), this (the window because the function is executing in the global context instead of as a method), and this.helloA (window.helloA, which doesn’t exist.
    5. Defines a function called init
    6. Returns an object which gets assigned to nick.name
    7. Then you call nick.name.init() which executes the init function in the context of name.
    8. This defines helloB
    9. Then it console.logs with “2” (as is), this (name), and this.helloB (nick.name.helloB – which doesn’t exist)

    So the first output you get is from console.log('1.',this, this.helloA);

    I think your main problem is that you are confusing this.foo (properties on the object on which a method is being called) with variable scope (variables available to a function)

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.