0

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
davethecoder
  • 3,856
  • 4
  • 35
  • 66

1 Answers1

2

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.

zerkms
  • 249,484
  • 69
  • 436
  • 539
  • the problem is one a web page, i have used console just to debug the object, and also stepped through the object, hell inside console i can get the value, but if i do console.log withing the code i get 0 – davethecoder Oct 01 '12 at 22:54
  • @davethecoder: have you tried js debugger? As I stated - it's possible to see in the `console.log()` not the "precise" values – zerkms Oct 01 '12 at 22:56
  • wtf ??? how can code not have a value, but chrome can okay shall investigate this further – davethecoder Oct 01 '12 at 22:59
  • @davethecoder: exactly. In debugger you always check the actual values, and with `console.log` you can see not entirely accurate data. – zerkms Oct 01 '12 at 23:00
  • very interesting, hmmmm never ever seen this behaviour before – davethecoder Oct 01 '12 at 23:01
  • got it, okay so your spot on actually, im doing a call to get my file details as json, this tells me what the default width and height is and updates the object, its then supposed to the make it moveable / resizable, however i have neglected to place the call inside my success, so my function that sets the object on stage is a few nano seconds behind because its async damn damn damn how did i miss that one :-) okay cool lesson learned :-) – davethecoder Oct 01 '12 at 23:08
  • 1
    @davethecoder: oh right. Anyway - good luck with your code :-) – zerkms Oct 01 '12 at 23:11