I'm thinking maybe I missed something in JavaScript that I'm just picking up now.
I tried this code in Chrome console:
a = [];
a.name = "test";
JSON.stringify(a);
// which returns value []
a = new Object();
a.name = "test";
JSON.stringify(a);
// which returns value {"name":"test"}
What is the difference? I thought new Object() was a Microsoft JScript thing? What am I missing? Must have missed something in a spec somewhere. Thanks.
a = new Object()
and
a = []
are not equivalent. But,
a = {}
and
a = new Object()
are.
new Object()
is equivalent to {}
(except when it's not because of weird redefinition issues - but ignore that for now.) []
is equivalent to new Array()
, to which you're then adding a .name
property. JSON stringifies arrays in a special way that doesn't capture arbitrary property assignment to the array itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With