本文介绍了JavaScript是否保证对象属性顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我创建这样的对象:
If I create an object like this:
var obj = {};
obj.prop1 = "Foo";
obj.prop2 = "Bar";
生成的对象总是会是这样吗?
Will the resulting object always look like this?
{ prop1 : "Foo", prop2 : "Bar" }
也就是说,这些属性是否与我添加它们的顺序相同?
That is, will the properties be in the same order that I added them?
推荐答案
不,JavaScript中不保证对象中的属性顺序;你需要使用数组
。
No, properties order in objects is not guaranteed in JavaScript; you need to use an Array
.
:
自ECMAScript 2015 ,使用可能是另一种选择。 Map
与 Object
和:
这篇关于JavaScript是否保证对象属性顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!