0

Under what circumstances would it be possible that an object is empty, but a property of that object is not?

Code:

console.log('type: '+typeof(widget));
console.log('obj: '+JSON.stringify(widget));
console.log('data: '+JSON.stringify(widget.data));

Output:

[INFO] :   type: object
[INFO] :   obj: {}
[INFO] :   data: {"index":2}

Additionally, for (var prop in widget) does not execute, and trying to call widget.hasOwnProperty('data') throws an error.

Edited to add: I should have specified that this is in Titanium, not straight JS, hence the console calls are the Titanium calls and not Firebug etc.

wizzard
  • 472
  • 1
  • 4
  • 13
  • `console.log(widget)` will (at least in FF and Chrome) display the *full/live* object graph - note that this is off of the value, not the stringification of such. – user2246674 Jun 05 '13 at 19:48

1 Answers1

3

if data isn't an enumerable property, it's not stringified. That's probably what happens here.

See Object.defineProperty to have a deeper understanding of not enumerable properties and their creation.

Note that you can use the console in a more efficient way :

console.log(typeof(widget), widget);
console.dir(widget);

It's not just for strings.

As an aside I just coded today a stringifier taking not enumerable properties into account : JSON.prune.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • Thank you, I checked out defineProperty and figured this was the answer. However, I then tried the function here to output all properties, even enumerable and inherited: http://stackoverflow.com/questions/8024149/is-it-possible-to-get-the-non-enumerable-inherited-property-names-of-an-object ...and 'data' still is not listed. Any thoughts? – wizzard Jun 05 '13 at 20:05
  • For reference, here is the output of that function: `["__TI","constructor","toString","hasOwnProperty","valueOf","__defineSetter__","__lookupGetter__","toLocaleString","__defineGetter__","propertyIsEnumerable","__lookupSetter__","isPrototypeOf"]` – wizzard Jun 05 '13 at 20:07
  • @wizzard without your object it's hard to tell. You might try to stringify it with *my* stringifier (link in answer). But there might be other traps that I can't guess without code. – Denys Séguret Jun 05 '13 at 20:08
  • I wish I had the code. `widget` is created inside a compiled Titanium module, so I have no view into that. – wizzard Jun 05 '13 at 20:11
  • It might be interesting to also log `typeof widget.data`. Sometimes you encounter strange things, like that trick in V8 which makes some objects of type undefined.. – Denys Séguret Jun 05 '13 at 20:22
  • Wow. I won't say it was enlightening but it was certainly unexpected: Simply adding `console.log(typeof(widget.data));` caused the following error: `*** setObjectForKey: object cannot be nil (key: title)` – wizzard Jun 05 '13 at 22:52
  • Yeah, it's looking that way. Hopefully I can get a response from them. Thanks for all your help thus far. – wizzard Jun 06 '13 at 20:37
  • If you get an answer, please keep us informed. It might be very interesting (and I know a few good JS coders who would probably be interested too). – Denys Séguret Jun 06 '13 at 20:38