【发布时间】:2018-08-24 13:56:35
【问题描述】:
我正在学习JavaScript,我一直在寻找这方面的信息,但我没有得到任何答案。我的问题是是否有任何规则可以在 JavaScript 中定义 JSON 键。
例如python中有一个规则定义dict,是All the keys must be of an immutable data type such as strings, numbers, or tuples.
var json = {};
json[""] = "White space";
json[" "] = "Two white space";
var emptyJSON = {};
var emptyArray = [];
function a (){}
json[a] = "Function";
json[emptyJSON] = "Json";
json[emptyArray]= "Array";
//I don't know why this property whit an empty array does not appear when I console the json
console.log("Print the entire object =>", json);
//But it appears when I console the specific property
console.log("Print empty array property =>", json[emptyArray]);
【问题讨论】:
-
请阅读“What is the difference between JSON and Object Literal Notation?”,因为您的问题中根本没有 JSON。
-
JavaScript 对象的属性由字符串命名。任何字符串都可以是属性名。
-
谢谢你 Luis Saul @str lol
标签: javascript arrays object key