- Notifications
You must be signed in to change notification settings - Fork19
Provides additional methods on Array.prototype and TypedArray.prototype to enable changes on the array by returning a new copy of it with the change.
tc39/proposal-change-array-by-copy
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
NoteThis proposal is'finished' and has been merged into the ECMAScript spec.PR
Provides additional methods onArray.prototype andTypedArray.prototype to enable changes on the array by returning a new copy of it with the change.
This proposal is currently atStage 4.
- Robin Ricard (Bloomberg)
- Ashley Claymore (Bloomberg)
This proposal introduces the following function properties toArray.prototype:
Array.prototype.toReversed() -> ArrayArray.prototype.toSorted(compareFn) -> ArrayArray.prototype.toSpliced(start, deleteCount, ...items) -> ArrayArray.prototype.with(index, value) -> Array
All of those methods keep the target Array untouched and returns a copy of it with the change performed instead.
toReversed,toSorted, andwith will also be added to TypedArrays:
TypedArray.prototype.toReversed() -> TypedArrayTypedArray.prototype.toSorted(compareFn) -> TypedArrayTypedArray.prototype.with(index, value) -> TypedArray
These methods will then be available on subclasses ofTypedArray. i.e. the following:
Int8ArrayUint8ArrayUint8ClampedArrayInt16ArrayUint16ArrayInt32ArrayUint32ArrayFloat32ArrayFloat64ArrayBigInt64ArrayBigUint64Array
constsequence=[1,2,3];sequence.toReversed();// => [3, 2, 1]sequence;// => [1, 2, 3]constoutOfOrder=newUint8Array([3,1,2]);outOfOrder.toSorted();// => Uint8Array [1, 2, 3]outOfOrder;// => Uint8Array [3, 1, 2]constcorrectionNeeded=[1,1,3];correctionNeeded.with(1,2);// => [1, 2, 3]correctionNeeded;// => [1, 1, 3]
TheTuple.prototype introduces these functions as a way to deal with the immutable aspect of the Tuples inRecord & Tuple. While Arrays are not immutable by nature, this style of programming can be beneficial to users dealing with frozen arrays for instance.
This proposal notably makes it easier to write code able to deal with Arrays and Tuples interchangeably.
Relationship withRecord & Tuple
While this proposal is derived fromRecord & Tuple, it should progress independently.
If web compatibility prescribes it, property names defined in this proposal are going to be changed. Those changes should be reflected onTuple.prototype.
Firefox/SpiderMonkey, shipping unflagged sinceFirefox 115
Safari/JavaScriptCore, shipping unflagged sinceSafari Tech Preview 146
Chrome/V8, shipping unflagged since Chrome 110
Ladybird/LibJS, shipping unflagged
./polyfill.js (minimalist reference implementation)
About
Provides additional methods on Array.prototype and TypedArray.prototype to enable changes on the array by returning a new copy of it with the change.
Topics
Resources
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors11
Uh oh!
There was an error while loading.Please reload this page.