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

The Archive Base Latest Questions

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

I just started with creating custom objects in javascript. I want to set this.someObjVar

  • 0

I just started with creating custom objects in javascript. I want to set this.someObjVar using code below, but something wrong in my approach. Maybe async response uses it’s own scope or thread.

// The code below used like:
someClass = new extfoo.SomeClass();
someClass.loadArrFromFile();
// this will be called far later after async returns
someClass.showSomeObjVar();

extfoo.js
=========
var extfoo = {};

extfoo.SomeClass = function() {
  this.someObjVar = [];
  this.showSomeObjVar = extfoo.showSomeObjVar;
  this.loadArrFromFile = extfoo.loadArrFromFile;
};
// Bad results here
extfoo.showSomeObjVar = function() {
  // results '0'
  console.log('showSomeObjVar: ' + this.someObjVar.length);
};

// Async array population
extfoo.loadArrFromFile = function() {
  var xhr = new XMLHttpRequest();
  xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
      this.someObjVar = xhr.responseText.split('\r\n')
      // results '23' elements
      console.log("someObjVar length: "+this.someObjVar.length);
    }
  }
  // request code ...
};
  • 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-31T01:39:51+00:00Added an answer on May 31, 2026 at 1:39 am

    Are you certain that this.someObjVar is referencing extfoo?

    There’s a chance that the this object in the extfoo.loadArrFromFile function is referencing the function itself (xhr.onreadystatechange), and not the extfoo object:

    extfoo.loadArrFromFile = function() {
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
    
          //I'm going to create a new variable for this function called this.someObjVar
          //Cause I don't know about extfoo's.
          this.someObjVar = xhr.responseText.split('\r\n')
          // results '23' elements
          console.log("someObjVar length: "+this.someObjVar.length);
    
          //Whup, end of the line, I guess I'll just forget about that variable I just made.
        }
      }
      // request code ...
    };
    

    There are several ways to fix this, but to test, try changing this in the extfoo.loadArrFromFile function to extfoo.

    Here’s how to fix it:

    extfoo.loadArrFromFile = function() {
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
    
          //OOOH you mean that object
          extfoo.someObjVar = xhr.responseText.split('\r\n')
          // results '23' elements
          console.log("someObjVar length: "+extfoo.someObjVar.length);
    
        }
      }
      // request code ...
    };
    

    One last thing… is extfoo.SomeClass.someObjVar supposed to be different from extfoo.someObjVar? This code will work if that’s the case, but otherwise, you’ll want to include a SomeClass in between each reference.

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

Sidebar

Related Questions

I just started creating my data-access layer using LinqToSql. Everybody is talking about the
I am just getting started creating an AJAX application using server side push. I
Ever since I started using .NET, I've just been creating Helper classes or Partial
I've just started learning about extending the Configuration file structure by creating custom section
I just started using Fiddler 2 to create request objects for REST,JSON, and SOAP
I've just started creating Unit Tests for my code again. I have had PHPUnit
I am new to Android and I just started creating an application. I want
I just started thinking about creating/customizing a web crawler today, and know very little
Just started with JQuery and I've managed to pass a parameter using POST, firebug
Just started my first MVC 2.0 .net application. And I set up some default

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.