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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:51:03+00:00 2026-06-05T20:51:03+00:00

Within a Javascript object ( testObject ) I am populating an array with objects

  • 0

Within a Javascript object (testObject) I am populating an array with objects from a jQuery.getJSON callback function. The array is defined first as an object property this.publicArray and var passedArray = this enables the inner callback function to access the public object.

Using a public method to the object i can expose the contents of the populated array as a child property of the object, but not the contents of the array directly.

function testObject() {

  this.publicArray = [];
  var passedArray = this;

  $.getJSON('http://...',
    function(data) {
      passedArray.publicArray = [{key:'value'},{key:'value'},{key:'value'}]  
  });

  this.testMethod = function() {
    console.log(this);
    console.log(this.publicArray);
  };
}

myObject = new testObject(); // [1]
myObject.testMethod(); // [2]

returns:

 [1] > thisArray: Array[3]
         0: Object
           key: "value"
         1: Object
           key: "value"
         2: Object
           key: "value"
         length: 3

 [2] > []

If i populate the array with exact same objects but not from a callback function it behaves as I expect it to, so I don’t understand:

  1. How to access the contents of the array from the object method
  2. Why the array contents are visible to Developer Tools and not to my script.

Thanks – and please do feel free to educate me on anything else i have screwed up.

  • 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-05T20:51:05+00:00Added an answer on June 5, 2026 at 8:51 pm

    There are a few issues with what you are trying to do.

    For one, when you create the new instance of ‘testObject’ you are making an ajax request to populate the array property ‘itsArray’. This property will have it’s default value ([]) until the ajax request is complete.

    Because AJAX is asynchronous by default (meaning the code will execute your ‘getJSON’ call and continue to the next statement immediately), any calls to ‘testMethod’ will return an empty array until the ajax request is complete.

    What you can do is use the ‘promise’ that an ajax request returns (Jquery Deferred Objects) and wait for the promise to be resolved, like so:

    function testObject() {
    
      this.itsArray = [];
      var passedArray = this;
    
      this.getJSONPromise = $.getJSON('http://...',
        function(data) {
          passedArray.itsArray = [{key:'value'},{key:'value'},{key:'value'}]  
      });
    
      this.testMethod = function() {
        console.log(this);
        console.log(this.itsArray);
      };
    }
    

    Now that we have a ‘promise’ object to work with, we can wait for it to complete to call the testMethod and we will have our data.

    myObject = new testObject();
    $.when(myObject.getJSONPromise).then(function()
    {
        myObject.testMethod();
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having difficulty referencing this from within a javascript inline function, within an object
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
I'd like to have JavaScript objects within another JavaScript object as such: Issues: -
I have some javascript code (within an object) : toggle: function() { var me
I am creating a Javascript object that contains a function that executes a jQuery
I have a Knockout observable array that I wish to edit from within Javascript
Below I am creating an object in JavaScript. Within the constructor I am setting
I wonder if it's possible to load jQuery within Opera User JavaScript , so
I have a javascript object which has some defined variables and attaches some event
Suppose I have javascript within ASP.NET UserControl: function setValue(DataItem) { var selectedDate = DataItem.getMember('DomainNameKey').get_object();

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.