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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:57:02+00:00 2026-06-15T19:57:02+00:00

Say I have a set of js that looks like this: function a() {

  • 0

Say I have a set of js that looks like this:

function a() {
this.meow = 0;
    var go = setTimeout(function() {
        this.parent.meow++;
    }, 500);
}

var woof = new a();

Why does woof.meow not increment and if I am referencing it wrong then why does this work:

(function() { 
   this.meow = 'woof'; 
   var go = setTimeout(function() {
      alert(this.parent.meow); 
   },500); 
   return true; 
})();

and even more confusingly then why doesn’t this work:

(function() { 
   this.meow = 0; 
   var go = setTimeout(function() {
      alert(this.parent.meow++); 
   },500); 
   return true; 
})();
  • 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-15T19:57:03+00:00Added an answer on June 15, 2026 at 7:57 pm

    parent has no special meaning in JavaScript (although it does on browsers). In your timeout function, this does not refer to your a instance, it refers to the window object. It happens that the window object has a property called parent, but that parent probably doesn’t have a property called meow. The reason this is window in your timeout function is that in JavaScript, this is defined entirely by how functions are called, not where they’re defined. See links below.

    Since your timeout function is a closure over the context of the call to new a, you can do this:

    function a() {
    var self = this;
    
    this.meow = 0;
        var go = setTimeout(function() {
            self.meow++;
        }, 500);
    }
    

    What that does is set a variable called self to the value of this within the call to a. The timeout function closes over self and so can use self.meow.

    if I am referencing it wrong then why does this work

    The code you’ve quoted doesn’t work (live example), but I suspect you saw something similar to it work because in that code, this references the window object, and it’s pretty easy to end up calling a function such that this references window. So meow wold have ended up being a global variable (all properties you put on window are globals).

    More on closures and this (on my blog):

    • Closures are not complicated
    • Mythical methods
    • You must remember this
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a class that looks like this (get/set omited): class InfoClass{ String
I have a block of code that looks like this: def create_page_object(url) begin page
Say I am being provided a json object that looks like this: {someList: [{id:
Say I have a set myset of custom objects that may be equal although
let's say I have this set of HTML-markup and CSS #CSS .inputhelp_text { background:
This is CakePHP 1.3 I have a 5-step order form that stores the new
I have a bash script file which starts with a function definition, like this:
Let's say I have an object like this: public class Widget { public string
I have this function: function disableDiv(divId, action){ var divId = byId(divId); if(action==true){ divId.style.display='none'; }
Sometimes I come across this problem where you have a set of functions that

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.