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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:14:09+00:00 2026-05-27T09:14:09+00:00

Perhaps this has already been addressed, but couldn’t find a suitable answer here. Apologies

  • 0

Perhaps this has already been addressed, but couldn’t find a suitable answer here. Apologies if it’s the case. Here’s the issue :

function Aclass(){

    this.value1 = "a first value"
    this.value2 = pulldata()

    function pulldata(){
        $.getJSON("data.json",function(data){
            return data
        })
    }
}

var obj = new Aclass()
console.log(obj.value1)
console.log(obj.value2)

Result : “a first value” then “undefined”
The data var isn’t available out of the pulldata’s scope, and I’m not sure what’s the best way around this.

  • 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-27T09:14:10+00:00Added an answer on May 27, 2026 at 9:14 am

    Your pullData() function doesn’t actually return anything explicitly, so its return is undefined – which means what you are seeing when you log obj.value2 is correct (though undesired).

    Inside pullData() you are calling $.getJSON() and passing it an anonymous function as a callback, and it is that anonymous function that returns data but the return doesn’t go anywhere because the ajax call is asynchronous and the callback doesn’t occur until later.

    You could try this:

    function Aclass(classInitialisedCallback){
        var self = this;
    
        this.value1 = "a first value";
        this.value2 = "not available yet";
    
        $.getJSON("data.json",function(data){
            self.value2 = data;
            if (typeof classInitialisedCallback === "function")
               classInitialisedCallback(self);
        });
    }
    
    var obj = new Aclass(function(newInstance) {
       // can use obj or newInstance to refer to the object
       console.log(obj.value1);
       console.log(newInstance.value2);
    });
    

    That is, set up your Aclass() constructor to take a parameter that is a callback function to be executed when the class is ready, i.e., after the result from $.getJSON() is available.

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

Sidebar

Related Questions

Perhaps this question has been asked elsewhere, but I'm unable to find it. With
Apologies if this has been answered already. There are similar topics but none that
Apologies if this question has been asked already, but suppose we have this code
Perhaps this has something to do with it being the mainForm, but I'll ask
Perhaps something similar has already been asked, and sure, it's a nitpick... I have
Ok, I know this has been asked a thousand times before, but no conclusive
According to this answer , Emacs + Slime already has much advanced functionality. So
I am going to ask that one question that perhaps has been already asked
This has always bugged me. Perhaps someone with some hardcore knowledge of .NET internals
Has anyone ever heard of overriding too many classes in Java? Perhaps this issue

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.