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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:41:26+00:00 2026-05-23T17:41:26+00:00

EDIT I was a bit quick there, the problem arises in the function and

  • 0

EDIT

I was a bit quick there, the problem arises in the function and not where I first said. Here is the function:

function returnAnObject(url) {
  var apiurl = 'http://url.com';

  var info = {};
  $.getJSON(apiurl, function(data) {
    $.extend(info, {
      a  : data.x,
      b  : data.y,
      c  : data.z
    });
  });

  console.log(info); // Shows object as usual
  console.log(info.a); // Shows undefined
  return info;
}

Does that make it clearer?

END EDIT

Ok so, I have a little problem.

I have a function that returns a fairly simple object, which looks something like this:

{
  a: 'x',
  b: 'y',
  c: 'z'
}

I save it to a variable like this:

var something = functionThatReturnsObject(someargument);
console.log(something); // In chrome dev tools, I see the object and its values
console.log(something.a); // This, however, logs undefined
console.log(something['a']); // This also logs undefined

Why is this? I think I’m going crazy here, I must have overlooked something…

The really weird part happens if instead of

var something = functionThatReturnsObject(someargument);

I write

window.something = functionThatReturnsObject(someargument);
console.log(something); // Still works, showing the object and properties
console.log(something.a); // Still doesn't work
console.log(someting['a']); // Still doesn't work

If I now access the object directly from the dev tools, inputting

something; // returns object, I can see everything in it etc.
something.a // Now, for some mysterious (to me) reason, this works, returning the value of a

So, does anyone understand what is going on here?

  • 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-23T17:41:26+00:00Added an answer on May 23, 2026 at 5:41 pm

    As I suspected. You’re assigning info in the success handler for an asynchronous function call. The success handler does not execute until AFTER the ajax call completes, but your function returns right after the ajax call starts (and long before it finishes and succeeds). You may not believe this, but this is the fourth time I’ve answered this exact same type of issue today. It’s a very common mistake. Because of the inline success handler, it appears that it all happens inside of the main function, but in reality it happens long after that function finishes.

    You can’t use the return result from the ajax call until after the success handler is called. If you want to pass that result on to subsequent code, you will have to call that subsequent code from the success handler, not continue it after the returnAnObject function call.

    It works in the dev tools because the ajax call completes by the time you type anything into the dev tools or look there. But, info.a is not available at the end of the returnAnObject function. It’s only available when the success handler for the ajax function is called.

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

Sidebar

Related Questions

EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
Edit : Solved, there was a trigger with a loop on the table (read
Edit: Prepare my objects for the use within a HashMap. after reading a bit
Edit: Building off of aL3xa's answer below, I've modified his syntax below. Not perfect,
This is my first question here so please bear with me - I apologise
Bit of an issue as to not knowing which is the best way to
Edit: This question was written in 2008, which was like 3 internet ages ago.
Edit: From another question I provided an answer that has links to a lot
EDIT: This was formerly more explicitly titled: - Best solution to stop Kontiki's KHOST.EXE
EDIT: This question is more about language engineering than C++ itself. I used C++

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.