开发者

In javascript, what is the difference between `{foo : "bar"}` and `{"foo":"bar"}`? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

What is the difference between object keys with quotes and without quotes?

What is the difference between a and b as below?

var a = {foo : "开发者_C百科bar"};
var b = {"foo" : "bar"};


Both are valid JavaScript object literals, and evaluate to different objects with properties called foo, with a.foo == b.foo being true.

Since you tagged this json, the first statement is invalid JSON because keys need to be strings (aside from the var a declaration).


There is no difference.

A key in an object literal can be an identifier or a string literal. You can use characters in a string that you can't use in an identifier, but foo doesn't contain any of those.

(As an aside, if you were writing JSON rather than JS, then the key would have to be a string)


They are the same.

The quoted syntax allows to set keys that are not valid identifiers (like foo bar) or that are reserved keywords (like for).

JSON only allows the quoted syntax.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜