{active: " yes "}和{" active ": " yes "}之间的区别是什么?

时间:2022-04-07 13:26:34

I've used FireBug to test the two cases and they seem pretty similar by result:

我用FireBug测试了这两种情况,结果看起来非常相似:

>>> var x = {"active": "yes"}
>>> x.active
"yes"
>>> var x = {active: "yes"}
>>> x.active
"yes"

But I'm pretty sure there is some difference between these two, maybe even performance related difference. Bottom line - I'd like to know if there is a difference between {active: "yes"} and {"active": "yes"}.

但我很确定这两者之间有一些区别,甚至是性能相关的区别。底线-我想知道{active: "yes"}和{"active": "yes"}之间是否存在差异。

5 个解决方案

#1


28  

Both are valid. However there are certain keywords you cant use like delete so in order to avoid that you wrap them in quotes so they are not treated literally by the ECMAScript parser and instead are explicitly specified as strings.

两者都是有效的。但是有一些关键字是不能使用的,比如delete,为了避免用引号括起来,所以ECMAScript解析器不会逐字逐句地处理它们,而是显式地指定为字符串。

Additionally, the JSON spec requires that keys have quotes around them:

此外,JSON规范要求键周围要有引号:

A string begins and ends with
quotation marks

字符串以引号开始和结束

So {key:'value'} is not valid JSON but is valid JS, while {"key":"value"} is valid JS and JSON.

所以{key:'value'}不是有效的JSON,而是有效的JS,而{"key":"value"}是有效的JS和JSON。

Examples of keywords and invalid/ambiguous keys:

关键字和无效/不明确的例子:

>>> ({delete:1})
SyntaxError: Unexpected token delete
>>> ({'delete':1})
Object

Another example:

另一个例子:

>>> ({first-name:'john'})
SyntaxError: Unexpected token -
>>> ({'first-name':'john'})
Object
>>> ({'first-name':'john'})['first-name']
"john"

#2


11  

Both are valid JavaScript (although some names can only be used quoted, active isn't among them).

两者都是有效的JavaScript(虽然有些名称只能引用,但活动不在其中)。

The latter is invalid JSON (quoted names are mandatory in JSON).

后者是无效的JSON (JSON中必须使用引号)。

#3


3  

Every valid JSON is also valid JavaScript but not every valid JavaScript is also valid JSON as JSON is a proper subset of JavaScript:

每个有效的JSON都是有效的JavaScript,但不是每个有效的JavaScript都是有效的JSON,因为JSON是JavaScript的一个适当子集:

JSON ⊂ JavaScript

JSON⊂JavaScript

JSON requires the names of name/value pairs to be quoted while JavaScript doesn’t (as long as they are not reserved keywords).

JSON需要名称/值对的名称被引用,而JavaScript不需要(只要它们不是保留的关键字)。

So your first example {"active": "yes"} is both valid JSON and valid JavaScript while the second example {active: "yes"} is only valid JavaScript.

因此,第一个示例{"active": "yes"}是有效的JSON和有效的JavaScript,而第二个示例{active: "yes"}只是有效的JavaScript。

#4


2  

In JavaScript, {"active": "yes"}, {'active': "yes"}, {"active": 'yes'} and {active: 'yes'} are all the same -- if you are using a reserved keyword (as meder points out), you must quote the key -- otherwise, the key does not need to be quoted.

在JavaScript中,{"active": "yes"}, {'active': "yes"}, {"active": 'yes'}和{active: 'yes'}都是一样的——如果你使用一个保留的关键字(如meder所指出的),你必须引用这个关键字——否则,这个关键字不需要被引用。

In JSON, on the other hand all keys and values must be quoted with ".
{"active": "yes"} is valid JSON.
{'active': "yes"}, {"active": 'yes'} and {active: 'yes'} are not.

另一方面,在JSON中,所有键和值都必须用“”进行引号。{"active": "yes"}是有效的JSON。{“活跃”:“是”},{“活跃”:“是的”}和{活跃:'是的' }。

#5


0  

If you are using this for JSON, the name (active) must be enclosed in quotes. It will still work in JavaScript without it, but it's technically malformed JSON.
See: http://json.org/
Note that the object, requires a string for the name (the bit before the colon).

如果您正在为JSON使用它,那么名称(活动的)必须用引号括起来。没有它,它仍然可以在JavaScript中工作,但是它在技术上是畸形的JSON。请参见:http://json.org/注意,该对象需要名称的字符串(冒号前的位)。

#1


28  

Both are valid. However there are certain keywords you cant use like delete so in order to avoid that you wrap them in quotes so they are not treated literally by the ECMAScript parser and instead are explicitly specified as strings.

两者都是有效的。但是有一些关键字是不能使用的,比如delete,为了避免用引号括起来,所以ECMAScript解析器不会逐字逐句地处理它们,而是显式地指定为字符串。

Additionally, the JSON spec requires that keys have quotes around them:

此外,JSON规范要求键周围要有引号:

A string begins and ends with
quotation marks

字符串以引号开始和结束

So {key:'value'} is not valid JSON but is valid JS, while {"key":"value"} is valid JS and JSON.

所以{key:'value'}不是有效的JSON,而是有效的JS,而{"key":"value"}是有效的JS和JSON。

Examples of keywords and invalid/ambiguous keys:

关键字和无效/不明确的例子:

>>> ({delete:1})
SyntaxError: Unexpected token delete
>>> ({'delete':1})
Object

Another example:

另一个例子:

>>> ({first-name:'john'})
SyntaxError: Unexpected token -
>>> ({'first-name':'john'})
Object
>>> ({'first-name':'john'})['first-name']
"john"

#2


11  

Both are valid JavaScript (although some names can only be used quoted, active isn't among them).

两者都是有效的JavaScript(虽然有些名称只能引用,但活动不在其中)。

The latter is invalid JSON (quoted names are mandatory in JSON).

后者是无效的JSON (JSON中必须使用引号)。

#3


3  

Every valid JSON is also valid JavaScript but not every valid JavaScript is also valid JSON as JSON is a proper subset of JavaScript:

每个有效的JSON都是有效的JavaScript,但不是每个有效的JavaScript都是有效的JSON,因为JSON是JavaScript的一个适当子集:

JSON ⊂ JavaScript

JSON⊂JavaScript

JSON requires the names of name/value pairs to be quoted while JavaScript doesn’t (as long as they are not reserved keywords).

JSON需要名称/值对的名称被引用,而JavaScript不需要(只要它们不是保留的关键字)。

So your first example {"active": "yes"} is both valid JSON and valid JavaScript while the second example {active: "yes"} is only valid JavaScript.

因此,第一个示例{"active": "yes"}是有效的JSON和有效的JavaScript,而第二个示例{active: "yes"}只是有效的JavaScript。

#4


2  

In JavaScript, {"active": "yes"}, {'active': "yes"}, {"active": 'yes'} and {active: 'yes'} are all the same -- if you are using a reserved keyword (as meder points out), you must quote the key -- otherwise, the key does not need to be quoted.

在JavaScript中,{"active": "yes"}, {'active': "yes"}, {"active": 'yes'}和{active: 'yes'}都是一样的——如果你使用一个保留的关键字(如meder所指出的),你必须引用这个关键字——否则,这个关键字不需要被引用。

In JSON, on the other hand all keys and values must be quoted with ".
{"active": "yes"} is valid JSON.
{'active': "yes"}, {"active": 'yes'} and {active: 'yes'} are not.

另一方面,在JSON中,所有键和值都必须用“”进行引号。{"active": "yes"}是有效的JSON。{“活跃”:“是”},{“活跃”:“是的”}和{活跃:'是的' }。

#5


0  

If you are using this for JSON, the name (active) must be enclosed in quotes. It will still work in JavaScript without it, but it's technically malformed JSON.
See: http://json.org/
Note that the object, requires a string for the name (the bit before the colon).

如果您正在为JSON使用它,那么名称(活动的)必须用引号括起来。没有它,它仍然可以在JavaScript中工作,但是它在技术上是畸形的JSON。请参见:http://json.org/注意,该对象需要名称的字符串(冒号前的位)。