wizzard
wizzard

Reputation: 472

Object is empty but object property is not?

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.

Upvotes: 0

Views: 1081

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382264

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.

Upvotes: 3

Related Questions