使用 [] 和 {} 初始化 javascript 变量有区别吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16921931/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a difference in Initializing javascript variable using [] versus {}
提问by Jubin Jose
Well I have always used
嗯,我一直用
var j= {};
j['field'] = value;
Apparently below works too
显然下面也有效
var j= [];
j['field'] = value;
Any difference?
有什么区别吗?
Edit: Sorry I guess I used the wrong word json here instead of object. My bad. But my questions till stands. Most people are explaining the difference between an array and object to me which I am familiar with.
编辑:抱歉,我想我在这里使用了错误的 json 词而不是 object。我的错。但我的问题直到站。大多数人都在向我解释我熟悉的数组和对象之间的区别。
I always do var x = []; x.push('blah') etc.
我总是做 var x = []; x.push('blah') 等
I also do var x = {}; x.Name = 'Michael'; x.Age = 21 etc.
我也做 var x = {}; x.Name = '迈克尔'; x.Age = 21 等
What I was surprised to see and what prompted me to ask this is I saw code that goes like var x = []; x.name = 'Michael';
我惊讶地看到并促使我提出这个问题的是我看到了类似 var x = []; 的代码。x.name = '迈克尔';
This is something I wasn't used to and hence posted the question.
这是我不习惯的事情,因此发布了问题。
Thanks for all the answers. I have marked accepted answer which addressed my question the best
感谢所有的答案。我已标记已接受的答案,该答案最能解决我的问题
回答by Alnitak
Your objects aren't "JSON", they're (empty) object and array literals. JSON is the serialized formyou get by passing such a value of JSON.stringify
.
您的对象不是“JSON”,它们是(空)对象和数组文字。JSON是序列化形式,你通过传递这样的价值得到JSON.stringify
。
The latter won't work if you do try to convert it to JSON, even though it's legal JavaScript:
如果您尝试将其转换为 JSON,后者将不起作用,即使它是合法的 JavaScript:
var json = []; // ok, it's an array
json["field"] = "value"; // still OK - arrays can have properties too
JSON.stringify(json);
> "[]"
i.e. you just get an empty array, because when you try to serialize an array you only get out the numerically indexedproperties from 0
to myArray.length - 1
, and not named properties.
即你只是得到一个空数组,因为当你尝试序列化一个数组时,你只能从to 中获取数字索引属性,而不是命名属性。0
myArray.length - 1
回答by Jason
[] is an array initializer
[] 是一个数组初始值设定项
{} is an object initializer
{} 是一个对象初始值设定项
回答by recursive
[]
is a zero length array literal.{}
is an empty object literal.
[]
是一个零长度数组文字。{}
是一个空对象字面量。
回答by techfoobar
[]
creates an Array
instance (which is an object instance with certain additional properties/methods), {}
an Object
instance.
[]
创建一个Array
实例(它是具有某些附加属性/方法的对象实例),{}
一个Object
实例。
The one created using []
will have methods from Array
's prototype like push()
, slice()
etc. whereas the second one won't.
一个使用创建的[]
将具有从方法Array
的原型等push()
,slice()
等等,而第二个则不会。
回答by gustavohenke
[]
is array, and {}
is literal object.
[]
是数组,{}
是文字对象。
You can do both things because both are JavaScript primitive objects.
你可以做这两件事,因为两者都是 JavaScript 原始对象。
When you do it in an array, you're setting an property in your array object, and you're not adding an item to it.
When you do it in the literal object... well, as said above, you're setting an property in that object.
当您在数组中执行此操作时,您是在数组对象中设置一个属性,而不是向其中添加项目。
当您在文字对象中执行此操作时……好吧,如上所述,您正在该对象中设置一个属性。
回答by akonsu
There is no difference.
没有区别。
In both cases you are setting a property on an object. It is just that the second object is an array.
在这两种情况下,您都是在对象上设置属性。只是第二个对象是一个数组。
In other words, in your latter example, you effectively use the same technique that you use in your former example, that is, you are setting a property on an object. in this case, an array. But you are not changing the contents of the array.
换句话说,在后一个示例中,您有效地使用了在前一个示例中使用的相同技术,也就是说,您正在为对象设置属性。在这种情况下,一个数组。但是您没有更改数组的内容。
回答by Selvakumar Arumugam
Below is an javascript Object,
下面是一个 javascript 对象,
var json = {};
json['field'] = value; //Valid
Below is an javascript Array,
下面是一个javascript数组,
var json = [];
//Below does not set the value in array instead it sets a property name field.
//json['field'] = value;
json.push(value); //proper way to add an element to an array
Both are Javascript objects and nothing to do with JSON yet.
两者都是 Javascript 对象,与 JSON 无关。
JSON is a notation, a convention followed across different language for easy data communication. Basically the above will be transformed to a string literal following JSON convention which can be read across different language.
JSON 是一种符号,一种跨不同语言遵循的约定,以便于数据通信。基本上,上面的内容将转换为遵循 JSON 约定的字符串文字,可以跨不同语言阅读。
There are two methods,
有两种方法,
JSON.stringify
- Convert the above to string literal following JSON notation.JSON.parse
- Convert the JSON string literal to a valid javascript object.
JSON.stringify
- 按照 JSON 表示法将上述内容转换为字符串文字。JSON.parse
- 将 JSON 字符串文字转换为有效的 javascript 对象。
The same function are available as a library in most languages which allows you to communicate across different language easily.
大多数语言的库都提供相同的功能,使您可以轻松地跨不同语言进行交流。