admin管理员组

文章数量:1415467

I understand that the following code wraps a number into an object:

var x = Object(5);

I therefore expect and understand the following:

alert(x == 5); //true
alert(x === 5); //false

However, I also understand that an object is a list of key/value pairs. So I would have expected the following to be different:

alert(JSON.stringify(5)); //5
alert(JSON.stringify(x)); //5

What does the structure of x look like? And why does it not appear to be in key/value pair format?

I understand that the following code wraps a number into an object:

var x = Object(5);

I therefore expect and understand the following:

alert(x == 5); //true
alert(x === 5); //false

However, I also understand that an object is a list of key/value pairs. So I would have expected the following to be different:

alert(JSON.stringify(5)); //5
alert(JSON.stringify(x)); //5

What does the structure of x look like? And why does it not appear to be in key/value pair format?

Share Improve this question asked Jan 3, 2016 at 20:31 SandySandy 1,5342 gold badges18 silver badges36 bronze badges 5
  • What's at issue here is the behavior of JSON.stringify(), is that right? – Pointy Commented Jan 3, 2016 at 20:41
  • I was not sure. I guess I took the definition that all objects are key/value pairs too literally and as a result, expected something in a key/value pair format. – Sandy Commented Jan 3, 2016 at 21:06
  • Well that's kind-of what I mean; the output from JSON.stringify() in your question is the result of how that particular API works. It explicitly understands that Number instances should be treated as numeric values in the resulting JSON. – Pointy Commented Jan 3, 2016 at 21:07
  • Similarly, String instances are treated as if they were string primitives, and Boolean instances as boolean primitives. – Pointy Commented Jan 3, 2016 at 21:07
  • But .... the object still has key/value pairs, the Number object has a primitiveValue property, where the value is 5, it's just like Pointy explains, some API's understand the difference between a "regular" object, and an object just holding a primitive value. – adeneo Commented Jan 3, 2016 at 21:10
Add a ment  | 

3 Answers 3

Reset to default 11

The Object constructor creates an object wrapper for the given value, of a Type that corresponds to the value.

So you get a Number object with the primitive value 5 when passing a number to Object

var x = Object(5);

it's exactly the same as doing

var x = new Number(5);

when calling valueOf() on both, you get the primitive value 5 back again, which is why stringifying it gives the same as stringifying the number 5, the object is converted to it's primitive value before stringifying

The specification for JSON.stringify says

Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.

var x = Object(5);

This boxes 5 as an object, so Stringify just unboxes the value.

If you assign a key/value to an object, Stringify will display as such :

var x = {};
x.foo = "bar";

This is javascript Duck Typing - basically if it looks like a duck, and sounds like a duck, it must be a duck however replace duck with a data type, such as int or collection... https://en.m.wikipedia/wiki/Duck_typing

In a javascript console, I entered the following:

> var x = Object(5);
> x
[Number: 5]
> JSON.stringify(5)
'5'
> JSON.stringify(x)
'5'

When you use Object(5), you are creating an object with key/value pairs. However, JSON.stringify() is turning that object into it's string representation -- "5". Calling JSON.stringify() on a literal value like the primitive number 5 also returns it's string representation -- "5". You're converting both the object and the primitive to strings, that's why they are equal.

本文标签: Understanding JavaScript Object(value)Stack Overflow