Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

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.

NotificationsYou must be signed in to change notification settings

tc39/proposal-change-array-by-copy

NoteThis proposal is'finished' and has been merged into the ECMAScript spec.PR

Change Array by copy

Provides additional methods onArray.prototype andTypedArray.prototype to enable changes on the array by returning a new copy of it with the change.

Status

This proposal is currently atStage 4.

Champions

  • Robin Ricard (Bloomberg)
  • Ashley Claymore (Bloomberg)

Reviewers

Overview

This proposal introduces the following function properties toArray.prototype:

  • Array.prototype.toReversed() -> Array
  • Array.prototype.toSorted(compareFn) -> Array
  • Array.prototype.toSpliced(start, deleteCount, ...items) -> Array
  • Array.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() -> TypedArray
  • TypedArray.prototype.toSorted(compareFn) -> TypedArray
  • TypedArray.prototype.with(index, value) -> TypedArray

These methods will then be available on subclasses ofTypedArray. i.e. the following:

  • Int8Array
  • Uint8Array
  • Uint8ClampedArray
  • Int16Array
  • Uint16Array
  • Int32Array
  • Uint32Array
  • Float32Array
  • Float64Array
  • BigInt64Array
  • BigUint64Array

Example

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]

Motivation

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.

Implementations

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

Stars

Watchers

Forks

Contributors11


[8]ページ先頭

©2009-2025 Movatter.jp