@@ -218,6 +218,37 @@ assert(Object.keys(@const { a: 1, b: 2 }) === @const ["a", "b"]);
218
218
assert (" a" in @const { a: 1, b: 2 });
219
219
` ` `
220
220
221
+ #### Note about ordering
222
+
223
+ When the properties of a const object are enumerated, its keys are enumerated in sorted order. This differs
224
+ from regular objects, where insertion order is preserved when enumerating properties
225
+ (except for properties that parse as numerics, where the behavior is undefined).
226
+
227
+ ` ` ` js
228
+ const obj = { z: 1 , a: 1 };
229
+ const constObj = @const { z: 1, a: 1 };
230
+
231
+ Object .keys (obj); // ["z", "a"]
232
+ Object .keys (constObj); // ["a", "z"]
233
+ ` ` `
234
+
235
+ The properties of const objects and const arrays are enumerated in this sorted order in order to
236
+ preserve their equality when consuming them in pure functions.
237
+
238
+ ` ` ` js
239
+ const constObj1 = { a: 1 , z: 1 };
240
+ const constObj1 = { z: 1 , a: 1 };
241
+
242
+ const func = (constObj ) => {... } // some function with no observable side effects
243
+
244
+ assert (constObj1 === constObj2);
245
+ assert (func (constObj1) === func (constObj2));
246
+ ` ` `
247
+
248
+ If enumeration order for const objects and const arrays was instead insertion order, then:
249
+ ` const func = Object .keys ;`
250
+ would break the above assertion.
251
+
221
252
### Const array prototype
222
253
223
254
The const array prototype is a const object that contains the same methods as Array with a few changes:
0 commit comments