davethecoder
davethecoder

Reputation: 3932

unable to get value from json that exists

Am puzzled now, like really puzzled:

got this javascript object:

{
    x: 0,
    y: 0,
    w: 28,
    h: 25,
    itemid: "5FE6E709096C4B57999AABC3575AF5D8",
    moveable: true,
    type: "4",
    fontface: "",
    text: "ef00704dd40e4768a1b15c14af9b6c4b.png",
    fontsize: 0,
    color: "",
    opacity: "",
    align: 0
}​

if I do console.log(theaboveobject); it outputs fine.

however if I do console.log(object.w) or console.log(object.h) I get 0 as the response 2 nights in a row this has done my head in now, whats going on, what am i missing?

    console.log(artworkLine); // this outputs fine as above
    console.log(artworkLine.w);  // says 0
    console.log(artworkLine.h);  // says 0

Upvotes: 0

Views: 124

Answers (1)

zerkms
zerkms

Reputation: 254886

It's just a guess but personally I've experienced the similar things several times.

It's caused by how the browsers resolve reference to an object in console. It's done in runtime in a moment of unfolding it by clicking on it. And it's likely that on that moment the object is already filled with desired data.

More details about that: http://felix-kling.de/blog/2011/08/18/inspecting-variables-in-javascript-consoles/

If you try js debugger (i.e. the one built in chrome developer tools) and put a break to the line with console.log(object); - you would see the actual object values.

The issue can be demonstrated very easily:

  1. Open http://jsfiddle.net/8jzvd/
  2. Open console
  3. Press jsfiddle Run button
  4. Wait until unfold label appears
  5. Click on Object (assuming you use google chrome browser)
  6. You see the object with foo property specified

Now repeat the same, but on step 4 unfold the object before unfold label appears. And you'll see that results from step 6 are different.

Upvotes: 2

Related Questions