JavaScript 对象属性顺序的秘密
原文英文,约300词,阅读约需2分钟。发表于: 。Someone asked me recently, are JavaScript object properties necessarily unordered and unpredictable? Developers with early exposure to JavaScript might answer that Object.keys() or for...in...
从ECMAScript 2020开始,JavaScript对象属性的顺序是:数组索引按数值升序,字符串属性按创建顺序,Symbol属性也按创建顺序。Object.keys和for...in遵循此顺序,但不包括Symbol属性,Reflect.ownKeys可以获取所有属性。若需严格插入顺序,建议使用Map。