EDIT
I was a little quick, 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);
console.log(info.a);
return info;
}
Is this clearer?
End edit
So, I have a little problem.
I have a function that returns a fairly simple object that looks something like this:
{
a: 'x',
b: 'y',
c: 'z'
}
I save it as a variable:
var something = functionThatReturnsObject(someargument);
console.log(something);
console.log(something.a);
console.log(something['a']);
Why is this? I think I'm losing my mind here, I must have missed something ...
The really strange part happens if instead
var something = functionThatReturnsObject(someargument);
I write
window.something = functionThatReturnsObject(someargument);
console.log(something);
console.log(something.a);
console.log(someting['a']);
If now I access the object directly from dev tools by typing
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?