Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarified enumeration order of properties #25

Merged
merged 2 commits into from
Jun 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,37 @@ assert(Object.keys(@const { a: 1, b: 2 }) === @const ["a", "b"]);
assert("a" in @const { a: 1, b: 2 });
```

#### Note about ordering

When the properties of a const object are enumerated, its keys are enumerated in sorted order. This differs
from regular objects, where insertion order is preserved when enumerating properties
(except for properties that parse as numerics, where the behavior is undefined).

```js
const obj = { z: 1, a: 1 };
const constObj = @const { z: 1, a: 1 };

Object.keys(obj); // ["z", "a"]
Object.keys(constObj); // ["a", "z"]
```

The properties of const objects and const arrays are enumerated in this sorted order in order to
preserve their equality when consuming them in pure functions.

```js
const constObj1 = { a: 1, z: 1 };
const constObj1 = { z: 1, a: 1 };

const func = (constObj) => {...} // some function with no observable side effects

assert(constObj1 === constObj2);
assert(func(constObj1) === func(constObj2));
```

If enumeration order for const objects and const arrays was instead insertion order, then:
`const func = Object.keys;`
would break the above assertion.

### Const array prototype

The const array prototype is a const object that contains the same methods as Array with a few changes:
Expand Down