[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Array/with [Back]  [Original]

Array.prototype.with() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Array.prototype.with()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2023 ..

with() Array , . .

In this article

js
arrayInstance.with(index, value)

index

, , .

  • : -array.length <= index < 0, index + array.length.
  • , RangeError.
value

.

, index value.

RangeError

, index >= array.length index < -array.length.

with() , . . .

with() at(), .

with() . , undefined.

with() . , this length .

js
const arr = [1, 2, 3, 4, 5];
console.log(arr.with(2, 6)); // [1, 2, 6, 4, 5]
console.log(arr); // [1, 2, 3, 4, 5]

with() , .

js
const arr = [1, 2, 3, 4, 5];
console.log(arr.with(2, 6).map((x) => x ** 2)); // [1, 4, 36, 16, 25]

with()

with() .

js
const arr = [1, , 3, 4, , 6];
console.log(arr.with(0, 2)); // [2, undefined, 3, 4, undefined, 6]

with() ,

with() . length , length. this , , . index value.

js
const arrayLike = {
  length: 3,
  unrelated: "foo",
  0: 5,
  2: 4,
  3: 3, // ,     3
};
console.log(Array.prototype.with.call(arrayLike, 0, 1));
// [ 1, undefined, 4 ]

Specification
ECMAScript 2027 LanguageSpecification
# sec-array.prototype.with


Web Proxy Viewer  |  New URL  |  Original Page