JSON.stringify()和JavaScript对象对象、JSON、stringify、JavaScript

2023-09-10 19:12:38 作者:咊風處

我在想,也许我错过了什么在JavaScript中的我只是拿起了。

我想这code在Chrome的控制台:

  A = [];
a.name =测试;
JSON.stringify(一);
//返回值[]
A =新的对象();
a.name =测试;
JSON.stringify(一);
//返回值{名:测试}
 

有什么区别? 我认为新的对象()是Microsoft JScript中的事情吗?我在想什么? 必须错过了一些东西在一个规范的地方。谢谢。

解决方案

  A =新的对象()
 

  A = []
 
jQuery,前端程序员必须要会的框架

是不等价的。但是,

  A = {}
 

  A =新的对象()
 

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.