Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
```js
Object.setPrototypeOf(Array.prototype, new Int8Array(0));
const a = [];
a[0] = 4;
console.log(+a[0]);
```
---
`a[0] = 4;`
[[Set]] ( P, V, Receiver )
1. Return ? OrdinarySet(O, P, V, Receiver).
---
OrdinarySet ( O, P, V, Receiver )
...
2. Let ownDesc be ? O.[[GetOwnProperty]](P).
3. Return OrdinarySetWithOwnDescriptor(O, P, V, Receiver, ownDesc).
---
[[GetOwnProperty]] ( P )
1. Return ! OrdinaryGetOwnProperty(O, P).
---
OrdinaryGetOwnProperty ( O, P )
...
2. If O does not have an own property with key P, return undefined.
---
OrdinarySetWithOwnDescriptor ( O, P, V, Receiver, ownDesc )
...
2. If ownDesc is undefined, then
a. Let parent be ? O.[[GetPrototypeOf]]().
b. If parent is not null, then
i. Return ? parent.[[Set]](P, V, Receiver).
---
9.4.5.5 [[Set]] ( P, V, Receiver ) (Integer-indexed exotic objects)
...
2. If Type(P) is String, then
a. Let numericIndex be ! CanonicalNumericIndexString(P).
b. If numericIndex is not undefined, then
i. Return ? IntegerIndexedElementSet(O, numericIndex, V).
---
IntegerIndexedElementSet ( O, index, value )
...
5. If IsInteger(index) is false, return false.
6. If index = -0, return false.
7. Let length be O.[[ArrayLength]].
8. If index < 0 or index ≥ length, return false. // End of the line, `a[0] = 4;` returns false. Without setting anything.
---
`+a[0];`
`a[0]` is `undefined`; `+undefined` is `NaN`;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.