23 索引集合

23.1 Array 对象

Array 是异质对象,会对某一类属性名给予特殊处理。关于这种特殊处理的定义,见 10.4.2

23.1.1 Array 构造器

Array 构造器

  • %Array%
  • 全局对象"Array" 属性的初始值。
  • 当作为构造器调用时,创建并初始化一个新的 Array。
  • 当作为函数而不是构造器调用时,也会创建并初始化一个新的 Array。因此,函数调用 Array(…) 等价于带有相同参数的对象创建表达式 new Array(…)
  • 是一个其行为会根据参数数量和类型而不同的函数。
  • 可用作类定义中 extends 子句的值。意图继承异质 Array 行为的子类构造器必须包含对 Array 构造器super 调用,以初始化作为 Array 异质对象的子类实例。然而,大多数 Array.prototype 方法都是泛型方法,并不依赖其 this 值是一个 Array 异质对象

23.1.1.1 Array ( ...values )

此函数在被调用时执行以下步骤:

  1. 如果 NewTarget 是 undefined,令 newTarget活动函数对象;否则令 newTarget 为 NewTarget。
  2. proto 为 ? GetPrototypeFromConstructor(newTarget, "%Array.prototype%")。
  3. numberOfArgsvalues 中元素的数量。
  4. 如果 numberOfArgs = 0,返回 ! ArrayCreate(0, proto)。
  5. 如果 numberOfArgs = 1,则
    1. lenvalues[0]。
    2. array 为 ! ArrayCreate(0, proto)。
    3. 如果 len 不是 Number,则
      1. 执行 ! CreateDataPropertyOrThrow(array, "0", len)。
      2. intLen1𝔽
    4. 否则,
      1. intLen 为 ! ToUint32(len)。
      2. 如果 SameValueZero(intLen, len) 是 false,抛出 RangeError 异常。
    5. 执行 ! Set(array, "length", intLen, true)。
    6. 返回 array
  6. 断言:numberOfArgs ≥ 2。
  7. array 为 ? ArrayCreate(numberOfArgs, proto)。
  8. k 为 0。
  9. 重复,当 k < numberOfArgs 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. itemKvalues[k]。
    3. 执行 ! CreateDataPropertyOrThrow(array, propertyKey, itemK)。
    4. k 设置为 k + 1。
  10. 断言:array"length" 属性的数学值numberOfArgs
  11. 返回 array

23.1.2 Array 构造器的属性

Array 构造器

  • 有一个 [[Prototype]] 内部槽,其值为 %Function.prototype%
  • 有一个值为 1𝔽"length" 属性。
  • 具有以下属性:

23.1.2.1 Array.from ( items [ , mapper [ , thisArg ] ] )

此函数在被调用时执行以下步骤:

  1. constructorthis 值。
  2. 如果 mapperundefined,则
    1. mappingfalse
  3. 否则,
    1. 如果 IsCallable(mapper) 是 false,抛出 TypeError 异常。
    2. mappingtrue
  4. usingIterator 为 ? GetMethod(items, %Symbol.iterator%)。
  5. 如果 usingIterator 不是 undefined,则
    1. 如果 IsConstructor(constructor) 是 true,则
      1. array 为 ? Construct(constructor)。
    2. 否则,
      1. array 为 ! ArrayCreate(0)。
    3. iteratorRecord 为 ? GetIteratorFromMethod(items, usingIterator)。
    4. k 为 0。
    5. 重复,
      1. 如果 k ≥ 253 - 1,则
        1. errorThrowCompletion(一个新创建的 TypeError 对象)。
        2. 返回 ? IteratorClose(iteratorRecord, error)。
      2. propertyKey 为 ! ToString(𝔽(k))。
      3. next 为 ? IteratorStepValue(iteratorRecord)。
      4. 如果 nextdone,则
        1. 执行 ? Set(array, "length", 𝔽(k), true)。
        2. 返回 array
      5. 如果 mappingtrue,则
        1. mappedValueCompletion(Call(mapper, thisArg, « next, 𝔽(k) »))。
        2. IfAbruptCloseIterator(mappedValue, iteratorRecord)。
      6. 否则,
        1. mappedValuenext
      7. defineStatusCompletion(CreateDataPropertyOrThrow(array, propertyKey, mappedValue))。
      8. IfAbruptCloseIterator(defineStatus, iteratorRecord)。
      9. k 设置为 k + 1。
  6. 注:items 不可迭代,因此假定它是一个类数组对象
  7. arrayLike 为 ! ToObject(items)。
  8. len 为 ? LengthOfArrayLike(arrayLike)。
  9. 如果 IsConstructor(constructor) 是 true,则
    1. array 为 ? Construct(constructor, « 𝔽(len) »)。
  10. 否则,
    1. array 为 ? ArrayCreate(len)。
  11. k 为 0。
  12. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ? Get(arrayLike, propertyKey)。
    3. 如果 mappingtrue,则
      1. mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
    4. 否则,
      1. mappedValuekValue
    5. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, mappedValue)。
    6. k 设置为 k + 1。
  13. 执行 ? Set(array, "length", 𝔽(len), true)。
  14. 返回 array
Note

此方法是有意设计的泛型工厂方法;它不要求其 this 值是 Array 构造器。因此,它可以被转移到或由任何其他可用单个数字参数调用的构造器继承。

23.1.2.2 Array.fromAsync ( items [ , mapper [ , thisArg ] ] )

此 async 函数在被调用时执行以下步骤:

  1. constructorthis 值。
  2. mappingfalse
  3. 如果 mapper 不是 undefined,则
    1. 如果 IsCallable(mapper) 是 false,抛出 TypeError 异常。
    2. mapping 设置为 true
  4. iteratorRecordundefined
  5. usingAsyncIterator 为 ? GetMethod(items, %Symbol.asyncIterator%)。
  6. 如果 usingAsyncIteratorundefined,则
    1. usingSyncIterator 为 ? GetMethod(items, %Symbol.iterator%)。
    2. 如果 usingSyncIterator 不是 undefined,则
      1. iteratorRecord 设置为 CreateAsyncFromSyncIterator(? GetIteratorFromMethod(items, usingSyncIterator))。
  7. 否则,
    1. iteratorRecord 设置为 ? GetIteratorFromMethod(items, usingAsyncIterator)。
  8. 如果 iteratorRecord 不是 undefined,则
    1. 如果 IsConstructor(constructor) 是 true,则
      1. array 为 ? Construct(constructor)。
    2. 否则,
      1. array 为 ! ArrayCreate(0)。
    3. k 为 0。
    4. 重复,
      1. 如果 k ≥ 253 - 1,则
        1. errorThrowCompletion(一个新创建的 TypeError 对象)。
        2. 返回 ? AsyncIteratorClose(iteratorRecord, error)。
      2. propertyKey 为 ! ToString(𝔽(k))。
      3. nextResult 为 ? Call(iteratorRecord.[[NextMethod]], iteratorRecord.[[Iterator]])。
      4. nextResult 设置为 ? Await(nextResult)。
      5. 如果 nextResult 不是 Object,抛出 TypeError 异常。
      6. done 为 ? IteratorComplete(nextResult)。
      7. 如果 donetrue,则
        1. 执行 ? Set(array, "length", 𝔽(k), true)。
        2. 返回 array
      8. nextValue 为 ? IteratorValue(nextResult)。
      9. 如果 mappingtrue,则
        1. mappedValueCompletion(Call(mapper, thisArg, « nextValue, 𝔽(k) »))。
        2. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord)。
        3. mappedValue 设置为 Completion(Await(mappedValue))。
        4. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord)。
      10. 否则,
        1. mappedValuenextValue
      11. defineStatusCompletion(CreateDataPropertyOrThrow(array, propertyKey, mappedValue))。
      12. IfAbruptCloseAsyncIterator(defineStatus, iteratorRecord)。
      13. k 设置为 k + 1。
  9. 否则,
    1. 注:items 既不是异步可迭代,也不是可迭代,因此假定它是一个类数组对象
    2. arrayLike 为 ! ToObject(items)。
    3. len 为 ? LengthOfArrayLike(arrayLike)。
    4. 如果 IsConstructor(constructor) 是 true,则
      1. array 为 ? Construct(constructor, « 𝔽(len) »)。
    5. 否则,
      1. array 为 ? ArrayCreate(len)。
    6. k 为 0。
    7. 重复,当 k < len 时,
      1. propertyKey 为 ! ToString(𝔽(k))。
      2. kValue 为 ? Get(arrayLike, propertyKey)。
      3. kValue 设置为 ? Await(kValue)。
      4. 如果 mappingtrue,则
        1. mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
        2. mappedValue 设置为 ? Await(mappedValue)。
      5. 否则,
        1. mappedValuekValue
      6. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, mappedValue)。
      7. k 设置为 k + 1。
    8. 执行 ? Set(array, "length", 𝔽(len), true)。
    9. 返回 array
Note

此方法是有意设计的泛型工厂方法;它不要求其 this 值是 Array 构造器。因此,它可以被转移到或由任何其他可用单个数字参数调用的构造器继承。

23.1.2.3 Array.isArray ( arg )

此函数在被调用时执行以下步骤:

  1. 返回 ? IsArray(arg)。

23.1.2.4 Array.of ( ...items )

此方法在被调用时执行以下步骤:

  1. lenitems 中元素的数量。
  2. lenNumber𝔽(len)。
  3. constructorthis 值。
  4. 如果 IsConstructor(constructor) 是 true,则
    1. array 为 ? Construct(constructor, « lenNumber »)。
  5. 否则,
    1. array 为 ? ArrayCreate(len)。
  6. k 为 0。
  7. 重复,当 k < len 时,
    1. kValueitems[k]。
    2. propertyKey 为 ! ToString(𝔽(k))。
    3. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, kValue)。
    4. k 设置为 k + 1。
  8. 执行 ? Set(array, "length", lenNumber, true)。
  9. 返回 array
Note

此方法是有意设计的泛型工厂方法;它不要求其 this 值是 Array 构造器。因此,它可以被转移到或由其他可用单个数字参数调用的构造器继承。

23.1.2.5 Array.prototype

Array.prototype 的值是 Array 原型对象

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

23.1.2.6 get Array [ %Symbol.species% ]

Array[%Symbol.species%] 是一个访问器属性,其 set 访问器函数为 undefined。它的 get 访问器函数在被调用时执行以下步骤:

  1. 返回 this 值。

此函数的 "name" 属性的值为 "get [Symbol.species]"

Note

Array 原型方法通常使用其 this 值的构造器来创建派生对象。然而,子类构造器可以通过重新定义其 %Symbol.species% 属性来覆写该默认行为。

23.1.3 Array 原型对象的属性

Array 原型对象

  • %Array.prototype%
  • 是一个 Array 异质对象,并具有为此类对象指定的内部方法。
  • 有一个 "length" 属性,其初始值为 +0𝔽,且其特性为 { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }。
  • 有一个 [[Prototype]] 内部槽,其值为 %Object.prototype%
Note

Array 原型对象被指定为 Array 异质对象,是为了确保与 ECMAScript 2015 规范之前创建的 ECMAScript 代码兼容。

23.1.3.1 Array.prototype.at ( index )

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. relativeIndex 为 ? ToIntegerOrInfinity(index)。
  4. 如果 relativeIndex ≥ 0,则
    1. krelativeIndex
  5. 否则,
    1. klen + relativeIndex
  6. 如果 k < 0 或 klen,返回 undefined
  7. 返回 ? Get(obj, ! ToString(𝔽(k)))。

23.1.3.2 Array.prototype.concat ( ...items )

此方法返回一个数组,其中包含该对象的数组元素,后跟每个参数的数组元素。

它在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. array 为 ? ArraySpeciesCreate(obj, 0)。
  3. nextIndex 为 0。
  4. obj 前置到 items
  5. 对于 items 的每个元素 item,执行
    1. spreadable 为 ? IsConcatSpreadable(item)。
    2. 如果 spreadabletrue,则
      1. len 为 ? LengthOfArrayLike(item)。
      2. 如果 nextIndex + len > 253 - 1,抛出 TypeError 异常。
      3. sourceIndex 为 0。
      4. 重复,当 sourceIndex < len 时,
        1. propertyKey 为 ! ToString(𝔽(sourceIndex))。
        2. exists 为 ? HasProperty(item, propertyKey)。
        3. 如果 existstrue,则
          1. subElement 为 ? Get(item, propertyKey)。
          2. 执行 ? CreateDataPropertyOrThrow(array, ! ToString(𝔽(nextIndex)), subElement)。
        4. nextIndex 设置为 nextIndex + 1。
        5. sourceIndex 设置为 sourceIndex + 1。
    3. 否则,
      1. 注:item 作为单个项添加,而不是展开。
      2. 如果 nextIndex ≥ 253 - 1,抛出 TypeError 异常。
      3. 执行 ? CreateDataPropertyOrThrow(array, ! ToString(𝔽(nextIndex)), item)。
      4. nextIndex 设置为 nextIndex + 1。
  6. 执行 ? Set(array, "length", 𝔽(nextIndex), true)。
  7. 返回 array

此方法的 "length" 属性为 1𝔽

Note 1

步骤 6 中对 "length" 属性的显式设置,旨在确保当 items 中最后一个非空元素有尾随空洞,或 array 不是内置 Array 时,长度是正确的。

Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.2.1 IsConcatSpreadable ( obj )

The abstract operation IsConcatSpreadable takes argument obj (一个 ECMAScript 语言值,) and returns 要么是一个包含 Boolean 的正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. 如果 obj 不是 Object,返回 false
  2. spreadable 为 ? Get(obj, %Symbol.isConcatSpreadable%)。
  3. 如果 spreadable 不是 undefined,返回 ToBoolean(spreadable)。
  4. 返回 ? IsArray(obj)。

23.1.3.3 Array.prototype.constructor

Array.prototype.constructor 的初始值是 %Array%

23.1.3.4 Array.prototype.copyWithin ( target, start [ , end ] )

Note 1

end 参数是可选的。如果未提供,则使用 this 值的长度。

Note 2

如果 target 为负,则将其视为 length + target,其中 length 是数组的长度。如果 start 为负,则将其视为 length + start。如果 end 为负,则将其视为 length + end

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. relativeTarget 为 ? ToIntegerOrInfinity(target)。
  4. 如果 relativeTarget = -∞,令 to 为 0。
  5. 否则如果 relativeTarget < 0,令 tomax(len + relativeTarget, 0)。
  6. 否则,令 tomin(relativeTarget, len)。
  7. relativeStart 为 ? ToIntegerOrInfinity(start)。
  8. 如果 relativeStart = -∞,令 from 为 0。
  9. 否则如果 relativeStart < 0,令 frommax(len + relativeStart, 0)。
  10. 否则,令 frommin(relativeStart, len)。
  11. 如果 endundefined,令 relativeEndlen;否则令 relativeEnd 为 ? ToIntegerOrInfinity(end)。
  12. 如果 relativeEnd = -∞,令 final 为 0。
  13. 否则如果 relativeEnd < 0,令 finalmax(len + relativeEnd, 0)。
  14. 否则,令 finalmin(relativeEnd, len)。
  15. countmin(final - from, len - to)。
  16. 如果 from < toto < from + count,则
    1. direction 为 -1。
    2. from 设置为 from + count - 1。
    3. to 设置为 to + count - 1。
  17. 否则,
    1. direction 为 1。
  18. 重复,当 count > 0 时,
    1. fromKey 为 ! ToString(𝔽(from))。
    2. toKey 为 ! ToString(𝔽(to))。
    3. fromPresent 为 ? HasProperty(obj, fromKey)。
    4. 如果 fromPresenttrue,则
      1. fromValue 为 ? Get(obj, fromKey)。
      2. 执行 ? Set(obj, toKey, fromValue, true)。
    5. 否则,
      1. 断言:fromPresentfalse
      2. 执行 ? DeletePropertyOrThrow(obj, toKey)。
    6. from 设置为 from + direction
    7. to 设置为 to + direction
    8. count 设置为 count - 1。
  19. 返回 obj
Note 3

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.5 Array.prototype.entries ( )

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. 返回 CreateArrayIterator(obj, key+value)。

23.1.3.6 Array.prototype.every ( callback [ , thisArg ] )

Note 1

callback 应是一个接受三个参数并返回可强制转换为 Boolean 值的值的函数。every 按升序对数组中存在的每个元素调用一次 callback,直到发现某个元素使 callback 返回 false。如果发现这样的元素,every 立即返回 false。否则,every 返回 truecallback 只会对数组中实际存在的元素调用;不会对数组中缺失的元素调用。

如果提供了 thisArg 参数,则它将用作每次调用 callback 时的 this 值。如果未提供,则使用 undefined

调用 callback 时传入三个参数:元素的值、元素的索引以及正在遍历的对象。

every 不会直接修改调用它的对象,但该对象可能会被对 callback 的调用修改。

every 处理的元素范围在第一次调用 callback 之前确定。在 every 调用开始后追加到数组的元素不会被 callback 访问。如果数组的现有元素被更改,则传给 callback 的值将是 every 访问它们时的值;在 every 调用开始后且被访问之前被删除的元素不会被访问。every 的行为类似于数学中的“全称”量词。特别是,对于空数组,它返回 true

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  4. k 为 0。
  5. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. testResultToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
      3. 如果 testResultfalse,返回 false
    4. k 设置为 k + 1。
  6. 返回 true
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.7 Array.prototype.fill ( value [ , start [ , end ] ] )

Note 1

start 参数是可选的。如果未提供,则使用 +0𝔽

end 参数是可选的。如果未提供,则使用 this 值的长度。

Note 2

如果 start 为负,则将其视为 length + start,其中 length 是数组的长度。如果 end 为负,则将其视为 length + end

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. relativeStart 为 ? ToIntegerOrInfinity(start)。
  4. 如果 relativeStart = -∞,令 k 为 0。
  5. 否则如果 relativeStart < 0,令 kmax(len + relativeStart, 0)。
  6. 否则,令 kmin(relativeStart, len)。
  7. 如果 endundefined,令 relativeEndlen;否则令 relativeEnd 为 ? ToIntegerOrInfinity(end)。
  8. 如果 relativeEnd = -∞,令 final 为 0。
  9. 否则如果 relativeEnd < 0,令 finalmax(len + relativeEnd, 0)。
  10. 否则,令 finalmin(relativeEnd, len)。
  11. 重复,当 k < final 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. 执行 ? Set(obj, propertyKey, value, true)。
    3. k 设置为 k + 1。
  12. 返回 obj
Note 3

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.8 Array.prototype.filter ( callback [ , thisArg ] )

Note 1

callback 应是一个接受三个参数并返回可强制转换为 Boolean 值的值的函数。filter 按升序对数组中的每个元素调用一次 callback,并构造一个新数组,其中包含所有使 callback 返回 true 的值。callback 只会对数组中实际存在的元素调用;不会对数组中缺失的元素调用。

如果提供了 thisArg 参数,则它将用作每次调用 callback 时的 this 值。如果未提供,则使用 undefined

调用 callback 时传入三个参数:元素的值、元素的索引以及正在遍历的对象。

filter 不会直接修改调用它的对象,但该对象可能会被对 callback 的调用修改。

filter 处理的元素范围在第一次调用 callback 之前确定。在 filter 调用开始后追加到数组的元素不会被 callback 访问。如果数组的现有元素被更改,则传给 callback 的值将是 filter 访问它们时的值;在 filter 调用开始后且被访问之前被删除的元素不会被访问。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  4. array 为 ? ArraySpeciesCreate(obj, 0)。
  5. k 为 0。
  6. to 为 0。
  7. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. selectedToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
      3. 如果 selectedtrue,则
        1. 执行 ? CreateDataPropertyOrThrow(array, ! ToString(𝔽(to)), kValue)。
        2. to 设置为 to + 1。
    4. k 设置为 k + 1。
  8. 返回 array
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.9 Array.prototype.find ( predicate [ , thisArg ] )

Note 1

此方法按升序索引顺序对数组的每个元素调用一次 predicate,直到找到一个使 predicate 返回可强制转换为 true 的值的元素。如果找到这样的元素,find 立即返回该元素值。否则,find 返回 undefined

更多信息见 FindViaPredicate

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. findRec 为 ? FindViaPredicate(obj, len, ascending, predicate, thisArg)。
  4. 返回 findRec.[[Value]]
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.10 Array.prototype.findIndex ( predicate [ , thisArg ] )

Note 1

此方法按升序索引顺序对数组的每个元素调用一次 predicate,直到找到一个使 predicate 返回可强制转换为 true 的值的元素。如果找到这样的元素,findIndex 立即返回该元素值的索引。否则,findIndex 返回 -1。

更多信息见 FindViaPredicate

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. findRec 为 ? FindViaPredicate(obj, len, ascending, predicate, thisArg)。
  4. 返回 findRec.[[Index]]
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.11 Array.prototype.findLast ( predicate [ , thisArg ] )

Note 1

此方法按降序索引顺序对数组的每个元素调用一次 predicate,直到找到一个使 predicate 返回可强制转换为 true 的值的元素。如果找到这样的元素,findLast 立即返回该元素值。否则,findLast 返回 undefined

更多信息见 FindViaPredicate

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. findRec 为 ? FindViaPredicate(obj, len, descending, predicate, thisArg)。
  4. 返回 findRec.[[Value]]
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.12 Array.prototype.findLastIndex ( predicate [ , thisArg ] )

Note 1

此方法按降序索引顺序对数组的每个元素调用一次 predicate,直到找到一个使 predicate 返回可强制转换为 true 的值的元素。如果找到这样的元素,findLastIndex 立即返回该元素值的索引。否则,findLastIndex 返回 -1。

更多信息见 FindViaPredicate

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. findRec 为 ? FindViaPredicate(obj, len, descending, predicate, thisArg)。
  4. 返回 findRec.[[Index]]
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array 对象。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.12.1 FindViaPredicate ( obj, len, direction, predicate, thisArg )

The abstract operation FindViaPredicate takes arguments obj (一个 Object,), len (一个非负整数,), direction (ascendingdescending,), predicate (一个 ECMAScript 语言值,), and thisArg (一个 ECMAScript 语言值,) and returns 要么是一个包含具有字段 [[Index]](一个整数 Number)和 [[Value]](一个 ECMAScript 语言值)的 Record正常完成,要么是一个抛出完成.

obj 应是一个类数组对象TypedArray。此操作按升序索引顺序或降序索引顺序(由 direction 指示)对 obj 的每个元素调用一次 predicate,直到找到一个使 predicate 返回可强制转换为 true 的值的元素。此时,此操作返回一个给出所找到元素索引和值的 Record。如果没有找到这样的元素,此操作返回一个 Record,其中索引指定为 -1𝔽,值指定为 undefined

predicate 应是一个函数。当针对数组元素调用时,会向它传入三个参数:元素的值、元素的索引以及正在遍历的对象。它的返回值将被强制转换为 Boolean 值。

thisArg 将用作每次调用 predicate 时的 this 值。

此操作不会直接修改调用它的对象,但该对象可能会被对 predicate 的调用修改。

处理的元素范围在第一次调用 predicate 之前、遍历即将开始时确定。此后追加到数组的元素不会被 predicate 访问。如果数组的现有元素被更改,则传给 predicate 的值将是此操作访问它们时的值。在遍历开始后且被访问之前被删除的元素仍会被访问,并且要么从原型查找,要么为 undefined

It performs the following steps when called:

  1. 如果 IsCallable(predicate) 是 false,抛出 TypeError 异常。
  2. 如果 directionascending,则
    1. indices 为从 0(包含)到 len(不包含)区间内的整数 List,按升序排列。
  3. 否则,
    1. indices 为从 0(包含)到 len(不包含)区间内的整数 List,按降序排列。
  4. 对于 indices 的每个整数 k,执行
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. 注:如果 objTypedArray,下面对 Get 的调用将返回一个正常完成
    3. kValue 为 ? Get(obj, propertyKey)。
    4. testResult 为 ? Call(predicate, thisArg, « kValue, 𝔽(k), obj »)。
    5. 如果 ToBoolean(testResult) 是 true,返回 Record { [[Index]]: 𝔽(k), [[Value]]: kValue }。
  5. 返回 Record { [[Index]]: -1𝔽, [[Value]]: undefined }。

23.1.3.13 Array.prototype.flat ( [ depth ] )

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. sourceLen 为 ? LengthOfArrayLike(obj)。
  3. depthNum 为 1。
  4. 如果 depth 不是 undefined,则
    1. depthNum 设置为 ? ToIntegerOrInfinity(depth)。
    2. 如果 depthNum < 0,将 depthNum 设置为 0。
  5. array 为 ? ArraySpeciesCreate(obj, 0)。
  6. 执行 ? FlattenIntoArray(array, obj, sourceLen, 0, depthNum)。
  7. 返回 array

23.1.3.13.1 FlattenIntoArray ( target, source, sourceLen, start, depth [ , mapperFunction [ , thisArg ] ] )

The abstract operation FlattenIntoArray takes arguments target (一个 Object,), source (一个 Object,), sourceLen (一个非负整数,), start (一个非负整数,), and depth (一个非负整数或 +∞,) and optional arguments mapperFunction (一个函数对象,) and thisArg (一个 ECMAScript 语言值,) and returns 要么是一个包含非负整数正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. 断言:如果 mapperFunction 存在,则 IsCallable(mapperFunction) 是 truethisArg 存在,且 depth 为 1。
  2. targetIndexstart
  3. sourceIndex+0𝔽
  4. 重复,当 (sourceIndex) < sourceLen 时,
    1. propertyKey 为 ! ToString(sourceIndex)。
    2. exists 为 ? HasProperty(source, propertyKey)。
    3. 如果 existstrue,则
      1. element 为 ? Get(source, propertyKey)。
      2. 如果 mapperFunction 存在,则
        1. element 设置为 ? Call(mapperFunction, thisArg, « element, sourceIndex, source »)。
      3. shouldFlattenfalse
      4. 如果 depth > 0,则
        1. shouldFlatten 设置为 ? IsArray(element)。
      5. 如果 shouldFlattentrue,则
        1. 如果 depth = +∞,令 newDepth 为 +∞。
        2. 否则,令 newDepthdepth - 1。
        3. elementLen 为 ? LengthOfArrayLike(element)。
        4. targetIndex 设置为 ? FlattenIntoArray(target, element, elementLen, targetIndex, newDepth)。
      6. 否则,
        1. 如果 targetIndex ≥ 253 - 1,抛出 TypeError 异常。
        2. 执行 ? CreateDataPropertyOrThrow(target, ! ToString(𝔽(targetIndex)), element)。
        3. targetIndex 设置为 targetIndex + 1。
    4. sourceIndex 设置为 sourceIndex + 1𝔽
  5. 返回 targetIndex

23.1.3.14 Array.prototype.flatMap ( mapperFunction [ , thisArg ] )

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. sourceLen 为 ? LengthOfArrayLike(obj)。
  3. 如果 IsCallable(mapperFunction) 是 false,抛出 TypeError 异常。
  4. array 为 ? ArraySpeciesCreate(obj, 0)。
  5. 执行 ? FlattenIntoArray(array, obj, sourceLen, 0, 1, mapperFunction, thisArg)。
  6. 返回 array

23.1.3.15 Array.prototype.forEach ( callback [ , thisArg ] )

Note 1

callback 应是一个接受三个参数的函数。forEach 按升序对数组中存在的每个元素调用一次 callbackcallback 只会对数组中实际存在的元素调用;不会对数组中缺失的元素调用。

如果提供了 thisArg 参数,则它将用作每次调用 callback 时的 this 值。如果未提供,则使用 undefined

调用 callback 时传入三个参数:元素的值、元素的索引以及正在遍历的对象。

forEach 不会直接修改调用它的对象,但该对象可能会被对 callback 的调用修改。

forEach 处理的元素范围在第一次调用 callback 之前确定。在 forEach 调用开始后追加到数组的元素不会被 callback 访问。如果数组的现有元素被更改,则传给 callback 的值将是 forEach 访问它们时的值;在 forEach 调用开始后且被访问之前被删除的元素不会被访问。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  4. k 为 0。
  5. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. 执行 ? Call(callback, thisArg, « kValue, 𝔽(k), obj »)。
    4. k 设置为 k + 1。
  6. 返回 undefined
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.16 Array.prototype.includes ( searchElement [ , fromIndex ] )

Note 1

此方法按升序使用 SameValueZero 算法将 searchElement 与数组元素进行比较,如果在任何位置找到,则返回 true;否则返回 false

可选的第二参数 fromIndex 默认为 +0𝔽(即搜索整个数组)。如果它大于或等于数组的长度,则返回 false,即不会搜索该数组。如果它小于 -0𝔽,则它被用作从数组末尾开始的偏移量来计算 fromIndex。如果计算出的索引小于或等于 +0𝔽,则会搜索整个数组。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 len = 0,返回 false
  4. startIndex 为 ? ToIntegerOrInfinity(fromIndex)。
  5. 断言:如果 fromIndexundefined,则 startIndex 为 0。
  6. 如果 startIndex = +∞,返回 false
  7. 如果 startIndex = -∞,将 startIndex 设置为 0。
  8. 如果 startIndex ≥ 0,则
    1. kstartIndex
  9. 否则,
    1. klen + startIndex
    2. 如果 k < 0,将 k 设置为 0。
  10. 重复,当 k < len 时,
    1. elementK 为 ? Get(obj, ! ToString(𝔽(k)))。
    2. 如果 SameValueZero(searchElement, elementK) 是 true,返回 true
    3. k 设置为 k + 1。
  11. 返回 false
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

Note 3

此方法有意在两个方面不同于类似的 indexOf 方法。第一,它使用 SameValueZero 算法,而不是 IsStrictlyEqual,从而允许它检测 NaN 数组元素。第二,它不会跳过缺失的数组元素,而是将它们视为 undefined

23.1.3.17 Array.prototype.indexOf ( searchElement [ , fromIndex ] )

此方法按升序使用 IsStrictlyEqual 算法将 searchElement 与数组元素进行比较,如果在一个或多个索引处找到,则返回此类索引中最小的一个;否则返回 -1𝔽

Note 1

可选的第二参数 fromIndex 默认为 +0𝔽(即搜索整个数组)。如果它大于或等于数组的长度,则返回 -1𝔽,即不会搜索该数组。如果它小于 -0𝔽,则用它来计算从数组末尾开始的偏移量。如果计算出的索引小于或等于 +0𝔽,则会搜索整个数组。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 len = 0,返回 -1𝔽
  4. startIndex 为 ? ToIntegerOrInfinity(fromIndex)。
  5. 断言:如果 fromIndexundefined,则 startIndex 为 0。
  6. 如果 startIndex = +∞,返回 -1𝔽
  7. 如果 startIndex = -∞,将 startIndex 设置为 0。
  8. 如果 startIndex ≥ 0,则
    1. kstartIndex
  9. 否则,
    1. klen + startIndex
    2. 如果 k < 0,将 k 设置为 0。
  10. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. elementK 为 ? Get(obj, propertyKey)。
      2. 如果 IsStrictlyEqual(searchElement, elementK) 是 true,返回 𝔽(k)。
    4. k 设置为 k + 1。
  11. 返回 -1𝔽
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.18 Array.prototype.join ( separator )

此方法将数组的元素转换为 String,然后将这些 String 拼接起来,并用 separator 的出现分隔。如果未提供分隔符,则使用单个逗号作为分隔符。

它在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 separatorundefined,令 sep","
  4. 否则,令 sep 为 ? ToString(separator)。
  5. result 为空 String。
  6. k 为 0。
  7. 重复,当 k < len 时,
    1. 如果 k > 0,将 result 设置为 resultsep 的字符串拼接。
    2. element 为 ? Get(obj, ! ToString(𝔽(k)))。
    3. 如果 element 既不是 undefined 也不是 null,则
      1. elementStr 为 ? ToString(element)。
      2. result 设置为 resultelementStr 的字符串拼接。
    4. k 设置为 k + 1。
  8. 返回 result
Note

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.19 Array.prototype.keys ( )

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. 返回 CreateArrayIterator(obj, key)。

23.1.3.20 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )

Note 1

此方法按降序使用 IsStrictlyEqual 算法将 searchElement 与数组元素进行比较,如果在一个或多个索引处找到,则返回此类索引中最大的一个;否则返回 -1𝔽

可选的第二参数 fromIndex 默认为数组长度减一(即搜索整个数组)。如果它大于或等于数组的长度,则会搜索整个数组。如果它小于 -0𝔽,则用它来计算从数组末尾开始的偏移量。如果计算出的索引小于 -0𝔽,则返回 -1𝔽

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 len = 0,返回 -1𝔽
  4. 如果 fromIndex 存在,令 startIndex 为 ? ToIntegerOrInfinity(fromIndex);否则令 startIndexlen - 1。
  5. 如果 startIndex = -∞,返回 -1𝔽
  6. 如果 startIndex ≥ 0,则
    1. kmin(startIndex, len - 1)。
  7. 否则,
    1. klen + startIndex
  8. 重复,当 k ≥ 0 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. elementK 为 ? Get(obj, propertyKey)。
      2. 如果 IsStrictlyEqual(searchElement, elementK) 是 true,返回 𝔽(k)。
    4. k 设置为 k - 1。
  9. 返回 -1𝔽
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.21 Array.prototype.map ( callback [ , thisArg ] )

Note 1

callback 应是一个接受三个参数的函数。map 按升序对数组中的每个元素调用一次 callback,并根据结果构造一个新的 Array。callback 只会对数组中实际存在的元素调用;不会对数组中缺失的元素调用。

如果提供了 thisArg 参数,则它将用作每次调用 callback 时的 this 值。如果未提供,则使用 undefined

调用 callback 时传入三个参数:元素的值、元素的索引以及正在遍历的对象。

map 不会直接修改调用它的对象,但该对象可能会被对 callback 的调用修改。

map 处理的元素范围在第一次调用 callback 之前确定。在 map 调用开始后追加到数组的元素不会被 callback 访问。如果数组的现有元素被更改,则传给 callback 的值将是 map 访问它们时的值;在 map 调用开始后且被访问之前被删除的元素不会被访问。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  4. array 为 ? ArraySpeciesCreate(obj, len)。
  5. k 为 0。
  6. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. mappedValue 为 ? Call(callback, thisArg, « kValue, 𝔽(k), obj »)。
      3. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, mappedValue)。
    4. k 设置为 k + 1。
  7. 返回 array
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.22 Array.prototype.pop ( )

Note 1

此方法移除数组的最后一个元素并返回它。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 len = 0,则
    1. 执行 ? Set(obj, "length", +0𝔽, true)。
    2. 返回 undefined
  4. 断言:len > 0。
  5. newLen𝔽(len - 1)。
  6. index 为 ! ToString(newLen)。
  7. element 为 ? Get(obj, index)。
  8. 执行 ? DeletePropertyOrThrow(obj, index)。
  9. 执行 ? Set(obj, "length", newLen, true)。
  10. 返回 element
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.23 Array.prototype.push ( ...items )

Note 1

此方法按参数出现的顺序将参数追加到数组末尾。它返回数组的新长度。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. argCountitems 中元素的数量。
  4. 如果 len + argCount > 253 - 1,抛出 TypeError 异常。
  5. 对于 items 的每个元素 item,执行
    1. 执行 ? Set(obj, ! ToString(𝔽(len)), item, true)。
    2. len 设置为 len + 1。
  6. 执行 ? Set(obj, "length", 𝔽(len), true)。
  7. 返回 𝔽(len)。

此方法的 "length" 属性为 1𝔽

Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.24 Array.prototype.reduce ( callback [ , initialValue ] )

Note 1

callback 应是一个接受四个参数的函数。reduce 会按升序对数组中存在的每个元素调用一次 callback,除非提供了 initialValue,否则会跳过第一个元素。

调用 callback 时传入四个参数:previousValue(前一次调用 callback 得到的值)、currentValue(当前元素的值)、currentIndex 以及正在遍历的对象。第一次调用 callback 时,previousValuecurrentValue 可以是两种值之一。如果调用 reduce 时提供了 initialValue,则 previousValue 将是 initialValuecurrentValue 将是数组中的第一个值。如果未提供 initialValue,则 previousValue 将是数组中的第一个值,currentValue 将是第二个值。如果数组不包含任何元素且未提供 initialValue,则为 TypeError

reduce 不会直接修改调用它的对象,但该对象可能会被对 callback 的调用修改。

reduce 处理的元素范围在第一次调用 callback 之前确定。在 reduce 调用开始后追加到数组的元素不会被 callback 访问。如果数组的现有元素被更改,则传给 callback 的值将是 reduce 访问它们时的值;在 reduce 调用开始后且被访问之前被删除的元素不会被访问。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  4. 如果 len = 0 且 initialValue 不存在,抛出 TypeError 异常。
  5. k 为 0。
  6. accumulatorundefined
  7. 如果 initialValue 存在,则
    1. accumulator 设置为 initialValue
  8. 否则,
    1. kPresentfalse
    2. 重复,当 kPresentfalsek < len 时,
      1. propertyKey 为 ! ToString(𝔽(k))。
      2. kPresent 设置为 ? HasProperty(obj, propertyKey)。
      3. 如果 kPresenttrue,则
        1. accumulator 设置为 ? Get(obj, propertyKey)。
      4. k 设置为 k + 1。
    3. 如果 kPresentfalse,抛出 TypeError 异常。
  9. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. accumulator 设置为 ? Call(callback, undefined, « accumulator, kValue, 𝔽(k), obj »)。
    4. k 设置为 k + 1。
  10. 返回 accumulator
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.25 Array.prototype.reduceRight ( callback [ , initialValue ] )

Note 1

callback 应是一个接受四个参数的函数。reduceRight 会按降序对数组中存在的每个元素调用一次 callback,除非提供了 initialValue,否则会跳过第一次调用。

调用 callback 时传入四个参数:previousValue(前一次调用 callback 得到的值)、currentValue(当前元素的值)、currentIndex 以及正在遍历的对象。第一次调用该函数时,previousValuecurrentValue 可以是两种值之一。如果调用 reduceRight 时提供了 initialValue,则 previousValue 将是 initialValuecurrentValue 将是数组中的最后一个值。如果未提供 initialValue,则 previousValue 将是数组中的最后一个值,currentValue 将是倒数第二个值。如果数组不包含任何元素且未提供 initialValue,则为 TypeError

reduceRight 不会直接修改调用它的对象,但该对象可能会被对 callback 的调用修改。

reduceRight 处理的元素范围在第一次调用 callback 之前确定。在 reduceRight 调用开始后追加到数组的元素不会被 callback 访问。如果数组的现有元素被 callback 更改,则传给 callback 的值将是 reduceRight 访问它们时的值;在 reduceRight 调用开始后且被访问之前被删除的元素不会被访问。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  4. 如果 len = 0 且 initialValue 不存在,抛出 TypeError 异常。
  5. klen - 1。
  6. accumulatorundefined
  7. 如果 initialValue 存在,则
    1. accumulator 设置为 initialValue
  8. 否则,
    1. kPresentfalse
    2. 重复,当 kPresentfalsek ≥ 0 时,
      1. propertyKey 为 ! ToString(𝔽(k))。
      2. kPresent 设置为 ? HasProperty(obj, propertyKey)。
      3. 如果 kPresenttrue,则
        1. accumulator 设置为 ? Get(obj, propertyKey)。
      4. k 设置为 k - 1。
    3. 如果 kPresentfalse,抛出 TypeError 异常。
  9. 重复,当 k ≥ 0 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. accumulator 设置为 ? Call(callback, undefined, « accumulator, kValue, 𝔽(k), obj »)。
    4. k 设置为 k - 1。
  10. 返回 accumulator
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.26 Array.prototype.reverse ( )

Note 1

此方法重新排列数组的元素,以反转其顺序。它返回反转后的数组。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. middlefloor(len / 2)。
  4. lower 为 0。
  5. 重复,当 lowermiddle 时,
    1. upperlen - lower - 1。
    2. upperP 为 ! ToString(𝔽(upper))。
    3. lowerP 为 ! ToString(𝔽(lower))。
    4. lowerExists 为 ? HasProperty(obj, lowerP)。
    5. 如果 lowerExiststrue,则
      1. lowerValue 为 ? Get(obj, lowerP)。
    6. upperExists 为 ? HasProperty(obj, upperP)。
    7. 如果 upperExiststrue,则
      1. upperValue 为 ? Get(obj, upperP)。
    8. 如果 lowerExiststrueupperExiststrue,则
      1. 执行 ? Set(obj, lowerP, upperValue, true)。
      2. 执行 ? Set(obj, upperP, lowerValue, true)。
    9. 否则如果 lowerExistsfalseupperExiststrue,则
      1. 执行 ? Set(obj, lowerP, upperValue, true)。
      2. 执行 ? DeletePropertyOrThrow(obj, upperP)。
    10. 否则如果 lowerExiststrueupperExistsfalse,则
      1. 执行 ? DeletePropertyOrThrow(obj, lowerP)。
      2. 执行 ? Set(obj, upperP, lowerValue, true)。
    11. 否则,
      1. 断言:lowerExistsupperExists 都是 false
      2. 注:不需要任何动作。
    12. lower 设置为 lower + 1。
  6. 返回 obj
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.27 Array.prototype.shift ( )

此方法移除数组的第一个元素并返回它。

它在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 len = 0,则
    1. 执行 ? Set(obj, "length", +0𝔽, true)。
    2. 返回 undefined
  4. first 为 ? Get(obj, "0")。
  5. k 为 1。
  6. 重复,当 k < len 时,
    1. from 为 ! ToString(𝔽(k))。
    2. to 为 ! ToString(𝔽(k - 1))。
    3. fromPresent 为 ? HasProperty(obj, from)。
    4. 如果 fromPresenttrue,则
      1. fromValue 为 ? Get(obj, from)。
      2. 执行 ? Set(obj, to, fromValue, true)。
    5. 否则,
      1. 断言:fromPresentfalse
      2. 执行 ? DeletePropertyOrThrow(obj, to)。
    6. k 设置为 k + 1。
  7. 执行 ? DeletePropertyOrThrow(obj, ! ToString(𝔽(len - 1)))。
  8. 执行 ? Set(obj, "length", 𝔽(len - 1), true)。
  9. 返回 first
Note

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.28 Array.prototype.slice ( start, end )

此方法返回一个数组,其中包含从元素 start 到元素 end(不包含)的数组元素(如果 endundefined,则一直到数组末尾)。如果 start 为负,则将其视为 length + start,其中 length 是数组的长度。如果 end 为负,则将其视为 length + end,其中 length 是数组的长度。

它在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. relativeStart 为 ? ToIntegerOrInfinity(start)。
  4. 如果 relativeStart = -∞,令 k 为 0。
  5. 否则如果 relativeStart < 0,令 kmax(len + relativeStart, 0)。
  6. 否则,令 kmin(relativeStart, len)。
  7. 如果 endundefined,令 relativeEndlen;否则令 relativeEnd 为 ? ToIntegerOrInfinity(end)。
  8. 如果 relativeEnd = -∞,令 final 为 0。
  9. 否则如果 relativeEnd < 0,令 finalmax(len + relativeEnd, 0)。
  10. 否则,令 finalmin(relativeEnd, len)。
  11. countmax(final - k, 0)。
  12. array 为 ? ArraySpeciesCreate(obj, count)。
  13. resultIndex 为 0。
  14. 重复,当 k < final 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. 执行 ? CreateDataPropertyOrThrow(array, ! ToString(𝔽(resultIndex)), kValue)。
    4. k 设置为 k + 1。
    5. resultIndex 设置为 resultIndex + 1。
  15. 执行 ? Set(array, "length", 𝔽(resultIndex), true)。
  16. 返回 array
Note 1

步骤 15 中对 "length" 属性的显式设置,旨在确保即使 array 不是内置 Array,长度也是正确的。

Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.29 Array.prototype.some ( callback [ , thisArg ] )

Note 1

callback 应是一个接受三个参数并返回可强制转换为 Boolean 值的值的函数。some 按升序对数组中存在的每个元素调用一次 callback,直到发现某个元素使 callback 返回 true。如果发现这样的元素,some 立即返回 true。否则,some 返回 falsecallback 只会对数组中实际存在的元素调用;不会对数组中缺失的元素调用。

如果提供了 thisArg 参数,则它将用作每次调用 callback 时的 this 值。如果未提供,则使用 undefined

调用 callback 时传入三个参数:元素的值、元素的索引以及正在遍历的对象。

some 不会直接修改调用它的对象,但该对象可能会被对 callback 的调用修改。

some 处理的元素范围在第一次调用 callback 之前确定。在 some 调用开始后追加到数组的元素不会被 callback 访问。如果数组的现有元素被更改,则传给 callback 的值将是 some 访问它们时的值;在 some 调用开始后且被访问之前被删除的元素不会被访问。some 的行为类似于数学中的“存在”量词。特别是,对于空数组,它返回 false

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  4. k 为 0。
  5. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. testResultToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
      3. 如果 testResulttrue,返回 true
    4. k 设置为 k + 1。
  6. 返回 false
Note 2

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.30 Array.prototype.sort ( comparator )

此方法对该数组的元素进行排序。如果 comparator 不是 undefined,则它应是一个接受两个参数 xy 的函数,并在 x < y 时返回一个负 Number,在 x > y 时返回一个正 Number,其他情况下返回零。

它在被调用时执行以下步骤:

  1. 如果 comparator 不是 undefinedIsCallable(comparator) 是 false,抛出 TypeError 异常。
  2. obj 为 ? ToObject(this value)。
  3. len 为 ? LengthOfArrayLike(obj)。
  4. sortCompare 为一个新的 Abstract Closure,其参数为 (x, y),捕获 comparator,并在被调用时执行以下步骤:
    1. 返回 ? CompareArrayElements(x, y, comparator)。
  5. sortedList 为 ? SortIndexedProperties(obj, len, sortCompare, skip-holes)。
  6. itemCountsortedList 中元素的数量。
  7. j 为 0。
  8. 重复,当 j < itemCount 时,
    1. 执行 ? Set(obj, ! ToString(𝔽(j)), sortedList[j], true)。
    2. j 设置为 j + 1。
  9. 注:步骤 5 中对 SortIndexedProperties 的调用使用 skip-holes。剩余索引会被删除,以保留检测到并从排序中排除的空洞数量。
  10. 重复,当 j < len 时,
    1. 执行 ? DeletePropertyOrThrow(obj, ! ToString(𝔽(j)))。
    2. j 设置为 j + 1。
  11. 返回 obj
Note 1

由于不存在的属性值总是比较为大于 undefined 属性值,而 undefined 总是比较为大于任何其他值(见 CompareArrayElements),因此 undefined 属性值总是排序到结果末尾,其后是不存的属性值。

Note 2

步骤 56 中由 ToString 抽象操作执行的方法调用,可能导致 sortCompare 不再表现为一致比较器

Note 3

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.30.1 SortIndexedProperties ( obj, len, sortCompare, holes )

The abstract operation SortIndexedProperties takes arguments obj (一个 Object,), len (一个非负整数,), sortCompare (一个带有两个参数的 Abstract Closure,), and holes (skip-holesread-through-holes,) and returns 要么是一个包含 ECMAScript 语言值 List正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. items 为一个新的空 List
  2. k 为 0。
  3. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. 如果 holesskip-holes,则
      1. kRead 为 ? HasProperty(obj, propertyKey)。
    3. 否则,
      1. 断言:holesread-through-holes
      2. kReadtrue
    4. 如果 kReadtrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. kValue 追加到 items
    5. k 设置为 k + 1。
  4. 使用实现定义sortCompare 的调用序列对 items 排序。如果任何此类调用返回 abrupt completion,则在执行任何进一步的 sortCompare 调用之前停止,并返回该 Completion Record
  5. 返回 items

排序顺序是在上述算法步骤 4 完成后 items 的顺序。如果 sortCompare 不是 items 元素的一致比较器,则排序顺序实现定义的。当 SortIndexedProperties 被 Array.prototype.sortArray.prototype.toSorted 调用时,如果 comparatorundefined,并且对传递给 sortCompare 的任何特定值应用 ToString 并不都产生相同结果,则排序顺序也是实现定义的。

除非排序顺序被指定为实现定义,否则它必须满足以下所有条件:

  • 必须存在某个小于 itemCount 的非负整数的数学排列 π,使得对于每个小于 itemCount 的非负整数 j,元素 old[j]new[π(j)] 完全相同。
  • 然后对于所有小于 itemCount 的非负整数 jk,如果 (sortCompare(old[j], old[k])) < 0,则 π(j) < π(k)
  • 并且对于所有满足 j < k < itemCount 的非负整数 jk,如果 (sortCompare(old[j], old[k])) = 0,则 π(j) < π(k);即,排序是稳定的。

这里使用记法 old[j] 指代执行步骤 4 之前的 items[j],使用记法 new[j] 指代执行步骤 4 之后的 items[j]

如果对于集合 values 中的所有值 abc(可能是同一个值)都满足下列要求,则 Abstract Closure 或函数 comparator 是值集合 values一致比较器:记法 a <C b 表示 (comparator(a, b)) < 0a =C b 表示 (comparator(a, b)) = 0;而 a >C b 表示 (comparator(a, b)) > 0

  • 对给定的一对值 ab 作为两个参数调用 comparator(a, b) 时,总是返回相同的值 v。此外,v 是一个 Number,并且 v 不是 NaN。注意,这意味着对于给定的一对 aba <C ba =C ba >C b 中恰好有一个为 true。
  • 调用 comparator(a, b) 不会修改 objobj 原型链上的任何对象。
  • a =C a(自反性)
  • 如果 a =C b,则 b =C a(对称性)
  • 如果 a =C bb =C c,则 a =C c(=C 的传递性)
  • 如果 a <C bb <C c,则 a <C c(<C 的传递性)
  • 如果 a >C bb >C c,则 a >C c(>C 的传递性)
Note

上述条件是确保 comparator 将集合 values 划分为等价类,并且这些等价类被全序排列的必要且充分条件。

23.1.3.30.2 CompareArrayElements ( x, y, comparator )

The abstract operation CompareArrayElements takes arguments x (一个 ECMAScript 语言值,), y (一个 ECMAScript 语言值,), and comparator (一个函数对象undefined,) and returns 要么是一个包含 Number 的正常完成,要么是一个 abrupt completion. It performs the following steps when called:

  1. 如果 xundefinedyundefined,返回 +0𝔽
  2. 如果 xundefined,返回 1𝔽
  3. 如果 yundefined,返回 -1𝔽
  4. 如果 comparator 不是 undefined,则
    1. result 为 ? ToNumber(? Call(comparator, undefined, « x, y »))。
    2. 如果 resultNaN,返回 +0𝔽
    3. 返回 result
  5. xString 为 ? ToString(x)。
  6. yString 为 ? ToString(y)。
  7. xSmaller 为 ! IsLessThan(xString, yString, true)。
  8. 如果 xSmallertrue,返回 -1𝔽
  9. ySmaller 为 ! IsLessThan(yString, xString, true)。
  10. 如果 ySmallertrue,返回 1𝔽
  11. 返回 +0𝔽

23.1.3.31 Array.prototype.splice ( start, deleteCount, ...items )

Note 1

此方法删除数组中从整数索引 start 开始的 deleteCount 个元素,并用 items 的元素替换它们。它返回一个包含被删除元素(如果有)的 Array。

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. relativeStart 为 ? ToIntegerOrInfinity(start)。
  4. 如果 relativeStart = -∞,令 actualStart 为 0。
  5. 否则如果 relativeStart < 0,令 actualStartmax(len + relativeStart, 0)。
  6. 否则,令 actualStartmin(relativeStart, len)。
  7. itemCountitems 中元素的数量。
  8. 如果 start 不存在,则
    1. actualDeleteCount 为 0。
  9. 否则如果 deleteCount 不存在,则
    1. actualDeleteCountlen - actualStart
  10. 否则,
    1. dc 为 ? ToIntegerOrInfinity(deleteCount)。
    2. actualDeleteCount 为将 dc 夹在 0 和 len - actualStart 之间的结果。
  11. 如果 len + itemCount - actualDeleteCount > 253 - 1,抛出 TypeError 异常。
  12. deletedArray 为 ? ArraySpeciesCreate(obj, actualDeleteCount)。
  13. k 为 0。
  14. 重复,当 k < actualDeleteCount 时,
    1. from 为 ! ToString(𝔽(actualStart + k))。
    2. 如果 ? HasProperty(obj, from) 是 true,则
      1. fromValue 为 ? Get(obj, from)。
      2. 执行 ? CreateDataPropertyOrThrow(deletedArray, ! ToString(𝔽(k)), fromValue)。
    3. k 设置为 k + 1。
  15. 执行 ? Set(deletedArray, "length", 𝔽(actualDeleteCount), true)。
  16. 如果 itemCount < actualDeleteCount,则
    1. k 设置为 actualStart
    2. 重复,当 k < (len - actualDeleteCount) 时,
      1. from 为 ! ToString(𝔽(k + actualDeleteCount))。
      2. to 为 ! ToString(𝔽(k + itemCount))。
      3. 如果 ? HasProperty(obj, from) 是 true,则
        1. fromValue 为 ? Get(obj, from)。
        2. 执行 ? Set(obj, to, fromValue, true)。
      4. 否则,
        1. 执行 ? DeletePropertyOrThrow(obj, to)。
      5. k 设置为 k + 1。
    3. k 设置为 len
    4. 重复,当 k > (len - actualDeleteCount + itemCount) 时,
      1. 执行 ? DeletePropertyOrThrow(obj, ! ToString(𝔽(k - 1)))。
      2. k 设置为 k - 1。
  17. 否则如果 itemCount > actualDeleteCount,则
    1. k 设置为 (len - actualDeleteCount)。
    2. 重复,当 k > actualStart 时,
      1. from 为 ! ToString(𝔽(k + actualDeleteCount - 1))。
      2. to 为 ! ToString(𝔽(k + itemCount - 1))。
      3. 如果 ? HasProperty(obj, from) 是 true,则
        1. fromValue 为 ? Get(obj, from)。
        2. 执行 ? Set(obj, to, fromValue, true)。
      4. 否则,
        1. 执行 ? DeletePropertyOrThrow(obj, to)。
      5. k 设置为 k - 1。
  18. k 设置为 actualStart
  19. 对于 items 的每个元素 item,执行
    1. 执行 ? Set(obj, ! ToString(𝔽(k)), item, true)。
    2. k 设置为 k + 1。
  20. 执行 ? Set(obj, "length", 𝔽(len - actualDeleteCount + itemCount), true)。
  21. 返回 deletedArray
Note 2

步骤 1520 中对 "length" 属性的显式设置,旨在确保即使对象不是内置 Array,长度也是正确的。

Note 3

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.32 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )

包含 ECMA-402 国际化 API 的 ECMAScript 实现必须按照 ECMA-402 规范中的规定实现此方法。如果 ECMAScript 实现不包含 ECMA-402 API,则使用以下对此方法的规范。

Note 1

ECMA-402 第一版未包含此方法的替代规范。

此方法的可选参数的含义在 ECMA-402 规范中定义;不包含 ECMA-402 支持的实现不得将这些参数位置用于任何其他用途。

此方法在被调用时执行以下步骤:

  1. array 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(array)。
  3. separator 为适合宿主环境当前区域设置的实现定义的列表分隔符 String 值(例如 ", ")。
  4. result 为空 String。
  5. k 为 0。
  6. 重复,当 k < len 时,
    1. 如果 k > 0,将 result 设置为 resultseparator 的字符串拼接。
    2. element 为 ? Get(array, ! ToString(𝔽(k)))。
    3. 如果 element 既不是 undefined 也不是 null,则
      1. elementStr 为 ? ToString(? Invoke(element, "toLocaleString")).
      2. result 设置为 resultelementStr 的字符串拼接。
    4. k 设置为 k + 1。
  7. 返回 result
Note 2

此方法使用数组元素的 toLocaleString 方法将其转换为 String,然后拼接这些 String,并用实现定义的区域设置敏感分隔符 String 的出现分隔。此方法类似于 toString,但它旨在产生与宿主环境当前区域设置的约定相对应的区域设置敏感结果。

Note 3

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.33 Array.prototype.toReversed ( )

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. array 为 ? ArrayCreate(len)。
  4. k 为 0。
  5. 重复,当 k < len 时,
    1. from 为 ! ToString(𝔽(len - k - 1))。
    2. propertyKey 为 ! ToString(𝔽(k))。
    3. fromValue 为 ? Get(obj, from)。
    4. 执行 ! CreateDataPropertyOrThrow(array, propertyKey, fromValue)。
    5. k 设置为 k + 1。
  6. 返回 array

23.1.3.34 Array.prototype.toSorted ( comparator )

此方法在被调用时执行以下步骤:

  1. 如果 comparator 不是 undefinedIsCallable(comparator) 是 false,抛出 TypeError 异常。
  2. obj 为 ? ToObject(this value)。
  3. len 为 ? LengthOfArrayLike(obj)。
  4. array 为 ? ArrayCreate(len)。
  5. sortCompare 为一个新的 Abstract Closure,其参数为 (x, y),捕获 comparator,并在被调用时执行以下步骤:
    1. 返回 ? CompareArrayElements(x, y, comparator)。
  6. sortedList 为 ? SortIndexedProperties(obj, len, sortCompare, read-through-holes)。
  7. j 为 0。
  8. 重复,当 j < len 时,
    1. 执行 ! CreateDataPropertyOrThrow(array, ! ToString(𝔽(j)), sortedList[j])。
    2. j 设置为 j + 1。
  9. 返回 array

23.1.3.35 Array.prototype.toSpliced ( start, skipCount, ...items )

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. relativeStart 为 ? ToIntegerOrInfinity(start)。
  4. 如果 relativeStart = -∞,令 actualStart 为 0。
  5. 否则如果 relativeStart < 0,令 actualStartmax(len + relativeStart, 0)。
  6. 否则,令 actualStartmin(relativeStart, len)。
  7. insertCountitems 中元素的数量。
  8. 如果 start 不存在,则
    1. actualSkipCount 为 0。
  9. 否则如果 skipCount 不存在,则
    1. actualSkipCountlen - actualStart
  10. 否则,
    1. sc 为 ? ToIntegerOrInfinity(skipCount)。
    2. actualSkipCount 为将 sc 夹在 0 和 len - actualStart 之间的结果。
  11. newLenlen + insertCount - actualSkipCount
  12. 如果 newLen > 253 - 1,抛出 TypeError 异常。
  13. newArray 为 ? ArrayCreate(newLen)。
  14. writeIndex 为 0。
  15. readIndexactualStart + actualSkipCount
  16. 重复,当 writeIndex < actualStart 时,
    1. propertyKey 为 ! ToString(𝔽(writeIndex))。
    2. iValue 为 ? Get(obj, propertyKey)。
    3. 执行 ! CreateDataPropertyOrThrow(newArray, propertyKey, iValue)。
    4. writeIndex 设置为 writeIndex + 1。
  17. 对于 items 的每个元素 item,执行
    1. propertyKey 为 ! ToString(𝔽(writeIndex))。
    2. 执行 ! CreateDataPropertyOrThrow(newArray, propertyKey, item)。
    3. writeIndex 设置为 writeIndex + 1。
  18. 重复,当 writeIndex < newLen 时,
    1. propertyKey 为 ! ToString(𝔽(writeIndex))。
    2. from 为 ! ToString(𝔽(readIndex))。
    3. fromValue 为 ? Get(obj, from)。
    4. 执行 ! CreateDataPropertyOrThrow(newArray, propertyKey, fromValue)。
    5. writeIndex 设置为 writeIndex + 1。
    6. readIndex 设置为 readIndex + 1。
  19. 返回 newArray

23.1.3.36 Array.prototype.toString ( )

此方法在被调用时执行以下步骤:

  1. array 为 ? ToObject(this value)。
  2. func 为 ? Get(array, "join")。
  3. 如果 IsCallable(func) 是 false,将 func 设置为内在函数 %Object.prototype.toString%。
  4. 返回 ? Call(func, array)。
Note

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.37 Array.prototype.unshift ( ...items )

此方法将参数前置到数组开头,使它们在数组中的顺序与其在参数列表中出现的顺序相同。

它在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. argCountitems 中元素的数量。
  4. 如果 argCount > 0,则
    1. 如果 len + argCount > 253 - 1,抛出 TypeError 异常。
    2. klen
    3. 重复,当 k > 0 时,
      1. from 为 ! ToString(𝔽(k - 1))。
      2. to 为 ! ToString(𝔽(k + argCount - 1))。
      3. fromPresent 为 ? HasProperty(obj, from)。
      4. 如果 fromPresenttrue,则
        1. fromValue 为 ? Get(obj, from)。
        2. 执行 ? Set(obj, to, fromValue, true)。
      5. 否则,
        1. 断言:fromPresentfalse
        2. 执行 ? DeletePropertyOrThrow(obj, to)。
      6. k 设置为 k - 1。
    4. j+0𝔽
    5. 对于 items 的每个元素 item,执行
      1. 执行 ? Set(obj, ! ToString(j), item, true)。
      2. j 设置为 j + 1𝔽
  5. 执行 ? Set(obj, "length", 𝔽(len + argCount), true)。
  6. 返回 𝔽(len + argCount)。

此方法的 "length" 属性为 1𝔽

Note

此方法有意设计为泛型;它不要求其 this 值是 Array。因此,它可以被转移到其他种类的对象上作为方法使用。

23.1.3.38 Array.prototype.values ( )

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. 返回 CreateArrayIterator(obj, value)。

23.1.3.39 Array.prototype.with ( index, value )

此方法在被调用时执行以下步骤:

  1. obj 为 ? ToObject(this value)。
  2. len 为 ? LengthOfArrayLike(obj)。
  3. relativeIndex 为 ? ToIntegerOrInfinity(index)。
  4. 如果 relativeIndex ≥ 0,令 actualIndexrelativeIndex
  5. 否则,令 actualIndexlen + relativeIndex
  6. 如果 actualIndexlenactualIndex < 0,抛出 RangeError 异常。
  7. array 为 ? ArrayCreate(len)。
  8. k 为 0。
  9. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. 如果 k = actualIndex,令 fromValuevalue
    3. 否则,令 fromValue 为 ? Get(obj, propertyKey)。
    4. 执行 ! CreateDataPropertyOrThrow(array, propertyKey, fromValue)。
    5. k 设置为 k + 1。
  10. 返回 array

23.1.3.40 Array.prototype [ %Symbol.iterator% ] ( )

%Symbol.iterator% 属性的初始值为 %Array.prototype.values%,定义见 23.1.3.38

23.1.3.41 Array.prototype [ %Symbol.unscopables% ]

%Symbol.unscopables% 数据属性的初始值是由以下步骤创建的对象:

  1. unscopableListOrdinaryObjectCreate(null)。
  2. 执行 ! CreateDataPropertyOrThrow(unscopableList, "at", true)。
  3. 执行 ! CreateDataPropertyOrThrow(unscopableList, "copyWithin", true)。
  4. 执行 ! CreateDataPropertyOrThrow(unscopableList, "entries", true)。
  5. 执行 ! CreateDataPropertyOrThrow(unscopableList, "fill", true)。
  6. 执行 ! CreateDataPropertyOrThrow(unscopableList, "find", true)。
  7. 执行 ! CreateDataPropertyOrThrow(unscopableList, "findIndex", true)。
  8. 执行 ! CreateDataPropertyOrThrow(unscopableList, "findLast", true)。
  9. 执行 ! CreateDataPropertyOrThrow(unscopableList, "findLastIndex", true)。
  10. 执行 ! CreateDataPropertyOrThrow(unscopableList, "flat", true)。
  11. 执行 ! CreateDataPropertyOrThrow(unscopableList, "flatMap", true)。
  12. 执行 ! CreateDataPropertyOrThrow(unscopableList, "includes", true)。
  13. 执行 ! CreateDataPropertyOrThrow(unscopableList, "keys", true)。
  14. 执行 ! CreateDataPropertyOrThrow(unscopableList, "toReversed", true)。
  15. 执行 ! CreateDataPropertyOrThrow(unscopableList, "toSorted", true)。
  16. 执行 ! CreateDataPropertyOrThrow(unscopableList, "toSpliced", true)。
  17. 执行 ! CreateDataPropertyOrThrow(unscopableList, "values", true)。
  18. 返回 unscopableList

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }。

Note

此对象的自身属性名是在 ECMAScript 2015 规范之前未作为 Array.prototype 标准属性包含的属性名。为了保留现有代码的行为,这些名称会在 with 语句绑定目的下被忽略;现有代码可能在外层作用域中将其中一个名称用作绑定,而该绑定会被以 Array 作为绑定对象的 with 语句遮蔽。

"with" 未包含在 unscopableList 中的原因是它已经是一个 保留字

23.1.4 Array 实例的属性

Array 实例是 Array 异质对象,并具有为此类对象指定的内部方法。Array 实例从 Array 原型对象继承属性。

Array 实例有一个 "length" 属性,以及一组具有数组索引名称的可枚举属性。

23.1.4.1 length

Array 实例的 "length" 属性是一个数据属性,其值总是在数值上大于每个名称为数组索引的可配置自身属性的名称。

"length" 属性初始具有特性 { [[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false }。

Note

减小 "length" 属性的值具有删除自身数组元素的副作用,这些数组元素的数组索引位于旧长度值和新长度值之间。然而,不可配置属性不能被删除。尝试将 Array 的 "length" 属性设置为一个在数值上小于或等于该数组现有不可配置 数组索引 属性的最大数值自身属性名的值,将导致长度被设置为比该不可配置数值自身属性名大一的数值。见 10.4.2.1

23.1.5 Array Iterator 对象

Array Iterator 是一个表示对某个特定 Array 实例对象进行特定迭代的对象。Array Iterator 对象没有具名构造器。相反,Array Iterator 对象是通过调用 Array 实例对象的某些方法创建的。

23.1.5.1 CreateArrayIterator ( array, kind )

The abstract operation CreateArrayIterator takes arguments array (一个 Object,) and kind (key+valuekeyvalue,) and returns 一个 Object. 它用于为返回此类迭代器的 Array 方法创建迭代器对象。 It performs the following steps when called:

  1. iteratorOrdinaryObjectCreate(%ArrayIteratorPrototype%, « [[IteratedArrayLike]], [[ArrayLikeNextIndex]], [[ArrayLikeIterationKind]] »)。
  2. iterator.[[IteratedArrayLike]] 设置为 array
  3. iterator.[[ArrayLikeNextIndex]] 设置为 0。
  4. iterator.[[ArrayLikeIterationKind]] 设置为 kind
  5. 返回 iterator

23.1.5.2 %ArrayIteratorPrototype% 对象

%ArrayIteratorPrototype% 对象:

23.1.5.2.1 %ArrayIteratorPrototype%.next ( )

  1. iteratorObjthis 值。
  2. 如果 iteratorObj 不是 Object,抛出 TypeError 异常。
  3. 如果 iteratorObj 不具有 Array Iterator 实例的所有内部槽(23.1.5.3),抛出 TypeError 异常。
  4. arrayiteratorObj.[[IteratedArrayLike]]
  5. 如果 arrayundefined,返回 CreateIteratorResultObject(undefined, true)。
  6. indexiteratorObj.[[ArrayLikeNextIndex]]
  7. kinditeratorObj.[[ArrayLikeIterationKind]]
  8. 如果 array 具有 [[TypedArrayName]] 内部槽,则
    1. taRecordMakeTypedArrayWithBufferWitnessRecord(array, seq-cst)。
    2. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
    3. lenTypedArrayLength(taRecord)。
  9. 否则,
    1. len 为 ? LengthOfArrayLike(array)。
  10. 如果 indexlen,则
    1. iteratorObj.[[IteratedArrayLike]] 设置为 undefined
    2. 返回 CreateIteratorResultObject(undefined, true)。
  11. iteratorObj.[[ArrayLikeNextIndex]] 设置为 index + 1。
  12. indexNumber𝔽(index)。
  13. 如果 kindkey,则
    1. resultindexNumber
  14. 否则,
    1. elementKey 为 ! ToString(indexNumber)。
    2. elementValue 为 ? Get(array, elementKey)。
    3. 如果 kindvalue,则
      1. resultelementValue
    4. 否则,
      1. 断言:kindkey+value
      2. resultCreateArrayFromListindexNumber, elementValue »)。
  15. 返回 CreateIteratorResultObject(result, false)。

23.1.5.2.2 %ArrayIteratorPrototype% [ %Symbol.toStringTag% ]

%Symbol.toStringTag% 属性的初始值是 String 值 "Array Iterator"

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }。

23.1.5.3 Array Iterator 实例的属性

Array Iterator 实例是普通对象,从 %ArrayIteratorPrototype% 内在对象继承属性。Array Iterator 实例最初会创建为具有 Table 69 中列出的内部槽。

Table 69: Array Iterator 实例的内部槽
内部槽 类型 描述
[[IteratedArrayLike]] 一个 Object 或 undefined 正在被迭代的类数组对象
[[ArrayLikeNextIndex]] 一个非负整数 此迭代器要检查的下一个元素的整数索引。
[[ArrayLikeIterationKind]] key+valuekeyvalue 一个标识每次迭代的元素返回内容的值。

23.2 TypedArray 对象

TypedArray 表示底层二进制数据缓冲区(25.1)的类数组视图。TypedArray 元素类型TypedArray 实例的所有元素所具有的底层二进制标量数据类型。对于每种受支持的元素类型,都有一个不同的 TypedArray 构造器,列于 Table 70Table 70 中的每个构造器都有一个对应的不同原型对象。

Table 70: TypedArray 构造器
构造器名称和内在对象 元素类型 元素大小 转换操作 描述
Int8Array
%Int8Array%
int8 1 ToInt8 8 位二进制补码有符号整数
Uint8Array
%Uint8Array%
uint8 1 ToUint8 8 位无符号整数
Uint8ClampedArray
%Uint8ClampedArray%
uint8clamped 1 ToUint8Clamp 8 位无符号整数(夹紧转换)
Int16Array
%Int16Array%
int16 2 ToInt16 16 位二进制补码有符号整数
Uint16Array
%Uint16Array%
uint16 2 ToUint16 16 位无符号整数
Int32Array
%Int32Array%
int32 4 ToInt32 32 位二进制补码有符号整数
Uint32Array
%Uint32Array%
uint32 4 ToUint32 32 位无符号整数
BigInt64Array
%BigInt64Array%
bigint64 8 ToBigInt64 64 位二进制补码有符号整数
BigUint64Array
%BigUint64Array%
biguint64 8 ToBigUint64 64 位无符号整数
Float16Array
%Float16Array%
float16 2 16 位 IEEE 浮点数
Float32Array
%Float32Array%
float32 4 32 位 IEEE 浮点数
Float64Array
%Float64Array%
float64 8 64 位 IEEE 浮点数

在下面的定义中,对 TypedArray 的引用应替换为上表中适当的构造器名称。

23.2.1 %TypedArray% 内在对象

%TypedArray% 内在对象:

  • 是一个构造器函数对象,所有 TypedArray 构造器对象都继承自它。
  • 连同其对应的原型对象,提供由所有 TypedArray 构造器及其实例继承的公共属性。
  • 没有全局名称,也不作为全局对象的属性出现。
  • 充当各种 TypedArray 构造器的抽象超类。
  • 被调用时会抛出错误,因为它是一个抽象类构造器TypedArray 构造器不会对它执行 super 调用。

23.2.1.1 %TypedArray% ( )

此函数在被调用时执行以下步骤:

  1. 抛出 TypeError 异常。

此函数的 "length" 属性为 +0𝔽

23.2.2 %TypedArray% 内在对象的属性

%TypedArray% 内在对象:

  • 有一个 [[Prototype]] 内部槽,其值为 %Function.prototype%
  • 有一个值为 "TypedArray""name" 属性。
  • 具有以下属性:

23.2.2.1 %TypedArray%.from ( source [ , mapper [ , thisArg ] ] )

此方法在被调用时执行以下步骤:

  1. constructorthis 值。
  2. 如果 IsConstructor(constructor) 是 false,抛出 TypeError 异常。
  3. 如果 mapperundefined,则
    1. mappingfalse
  4. 否则,
    1. 如果 IsCallable(mapper) 是 false,抛出 TypeError 异常。
    2. mappingtrue
  5. usingIterator 为 ? GetMethod(source, %Symbol.iterator%)。
  6. 如果 usingIterator 不是 undefined,则
    1. values 为 ? IteratorToList(? GetIteratorFromMethod(source, usingIterator))。
    2. lenvalues 中元素的数量。
    3. targetObj 为 ? TypedArrayCreateFromConstructor(constructor, « 𝔽(len) »)。
    4. k 为 0。
    5. 重复,当 k < len 时,
      1. propertyKey 为 ! ToString(𝔽(k))。
      2. kValuevalues 的第一个元素。
      3. values 中移除第一个元素。
      4. 如果 mappingtrue,则
        1. mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
      5. 否则,
        1. mappedValuekValue
      6. 执行 ? Set(targetObj, propertyKey, mappedValue, true)。
      7. k 设置为 k + 1。
    6. 断言:values 现在是一个空 List
    7. 返回 targetObj
  7. 注:source 不是可迭代对象,因此假定它已经是一个类数组对象
  8. arrayLike 为 ! ToObject(source)。
  9. len 为 ? LengthOfArrayLike(arrayLike)。
  10. targetObj 为 ? TypedArrayCreateFromConstructor(constructor, « 𝔽(len) »)。
  11. k 为 0。
  12. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ? Get(arrayLike, propertyKey)。
    3. 如果 mappingtrue,则
      1. mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
    4. 否则,
      1. mappedValuekValue
    5. 执行 ? Set(targetObj, propertyKey, mappedValue, true)。
    6. k 设置为 k + 1。
  13. 返回 targetObj

23.2.2.2 %TypedArray%.of ( ...items )

此方法在被调用时执行以下步骤:

  1. lenitems 中元素的数量。
  2. constructorthis 值。
  3. 如果 IsConstructor(constructor) 是 false,抛出 TypeError 异常。
  4. newObj 为 ? TypedArrayCreateFromConstructor(constructor, « 𝔽(len) »)。
  5. k 为 0。
  6. 重复,当 k < len 时,
    1. kValueitems[k]。
    2. propertyKey 为 ! ToString(𝔽(k))。
    3. 执行 ? Set(newObj, propertyKey, kValue, true)。
    4. k 设置为 k + 1。
  7. 返回 newObj

23.2.2.3 %TypedArray%.prototype

%TypedArray%.prototype 的初始值是 %TypedArray% 原型对象

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

23.2.2.4 get %TypedArray% [ %Symbol.species% ]

%TypedArray%[%Symbol.species%] 是一个访问器属性,其 set 访问器函数为 undefined。它的 get 访问器函数在被调用时执行以下步骤:

  1. 返回 this 值。

此函数的 "name" 属性的值为 "get [Symbol.species]"

Note

%TypedArray.prototype% 方法通常使用其 this 值的构造器来创建派生对象。然而,子类构造器可以通过重新定义其 %Symbol.species% 属性来覆写该默认行为。

23.2.3 %TypedArray% 原型对象的属性

%TypedArray% 原型对象

  • 有一个 [[Prototype]] 内部槽,其值为 %Object.prototype%
  • %TypedArray.prototype%
  • 是一个普通对象
  • 没有 [[ViewedArrayBuffer]] 或任何其他特定于 TypedArray 实例对象的内部槽。

23.2.3.1 %TypedArray%.prototype.at ( index )

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. relativeIndex 为 ? ToIntegerOrInfinity(index)。
  5. 如果 relativeIndex ≥ 0,则
    1. krelativeIndex
  6. 否则,
    1. klen + relativeIndex
  7. 如果 k < 0 或 klen,返回 undefined
  8. 返回 ! Get(obj, ! ToString(𝔽(k)))。

23.2.3.2 get %TypedArray%.prototype.buffer

%TypedArray%.prototype.buffer 是一个访问器属性,其 set 访问器函数为 undefined。它的 get 访问器函数在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj[[ViewedArrayBuffer]] 内部槽。
  4. bufferobj.[[ViewedArrayBuffer]]
  5. 返回 buffer

23.2.3.3 get %TypedArray%.prototype.byteLength

%TypedArray%.prototype.byteLength 是一个访问器属性,其 set 访问器函数为 undefined。它的 get 访问器函数在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj[[ViewedArrayBuffer]] 内部槽。
  4. taRecordMakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  5. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,返回 +0𝔽
  6. sizeTypedArrayByteLength(taRecord)。
  7. 返回 𝔽(size)。

23.2.3.4 get %TypedArray%.prototype.byteOffset

%TypedArray%.prototype.byteOffset 是一个访问器属性,其 set 访问器函数为 undefined。它的 get 访问器函数在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj[[ViewedArrayBuffer]] 内部槽。
  4. taRecordMakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  5. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,返回 +0𝔽
  6. offsetobj.[[ByteOffset]]
  7. 返回 𝔽(offset)。

23.2.3.5 %TypedArray%.prototype.constructor

%TypedArray%.prototype.constructor 的初始值是 %TypedArray%

23.2.3.6 %TypedArray%.prototype.copyWithin ( target, start [ , end ] )

此方法的参数解释和使用方式与 23.1.3.4 中定义的 Array.prototype.copyWithin 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. relativeTarget 为 ? ToIntegerOrInfinity(target)。
  5. 如果 relativeTarget = -∞,令 targetIndex 为 0。
  6. 否则如果 relativeTarget < 0,令 targetIndexmax(len + relativeTarget, 0)。
  7. 否则,令 targetIndexmin(relativeTarget, len)。
  8. relativeStart 为 ? ToIntegerOrInfinity(start)。
  9. 如果 relativeStart = -∞,令 startIndex 为 0。
  10. 否则如果 relativeStart < 0,令 startIndexmax(len + relativeStart, 0)。
  11. 否则,令 startIndexmin(relativeStart, len)。
  12. 如果 endundefined,令 relativeEndlen;否则令 relativeEnd 为 ? ToIntegerOrInfinity(end)。
  13. 如果 relativeEnd = -∞,令 endIndex 为 0。
  14. 否则如果 relativeEnd < 0,令 endIndexmax(len + relativeEnd, 0)。
  15. 否则,令 endIndexmin(relativeEnd, len)。
  16. countmin(endIndex - startIndex, len - targetIndex)。
  17. 如果 count > 0,则
    1. 注:复制必须以保留源数据位级编码的方式执行。
    2. bufferobj.[[ViewedArrayBuffer]]
    3. taRecord 设置为 MakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
    4. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
    5. len 设置为 TypedArrayLength(taRecord)。
    6. 注:上述步骤的副作用可能减小了 obj 的大小,在这种情况下复制应按仍适用的最长前缀继续进行。
    7. count 设置为 min(count, len - startIndex, len - targetIndex)。
    8. elementSizeTypedArrayElementSize(obj)。
    9. byteOffsetobj.[[ByteOffset]]
    10. toByteIndex 为 (targetIndex × elementSize) + byteOffset
    11. fromByteIndex 为 (startIndex × elementSize) + byteOffset
    12. countBytescount × elementSize
    13. 如果 fromByteIndex < toByteIndextoByteIndex < fromByteIndex + countBytes,则
      1. direction 为 -1。
      2. fromByteIndex 设置为 fromByteIndex + countBytes - 1。
      3. toByteIndex 设置为 toByteIndex + countBytes - 1。
    14. 否则,
      1. direction 为 1。
    15. 重复,当 countBytes > 0 时,
      1. valueGetValueFromBuffer(buffer, fromByteIndex, uint8, true, unordered)。
      2. 执行 SetValueInBuffer(buffer, toByteIndex, uint8, value, true, unordered)。
      3. fromByteIndex 设置为 fromByteIndex + direction
      4. toByteIndex 设置为 toByteIndex + direction
      5. countBytes 设置为 countBytes - 1。
  18. 返回 obj

23.2.3.7 %TypedArray%.prototype.entries ( )

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? ValidateTypedArray(obj, seq-cst)。
  3. 返回 CreateArrayIterator(obj, key+value)。

23.2.3.8 %TypedArray%.prototype.every ( callback [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.6 中定义的 Array.prototype.every 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  5. k 为 0。
  6. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ! Get(obj, propertyKey)。
    3. testResultToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
    4. 如果 testResultfalse,返回 false
    5. k 设置为 k + 1。
  7. 返回 true

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.9 %TypedArray%.prototype.fill ( value [ , start [ , end ] ] )

此方法的参数解释和使用方式与 23.1.3.7 中定义的 Array.prototype.fill 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 obj.[[ContentType]]bigint,将 value 设置为 ? ToBigInt(value)。
  5. 否则,将 value 设置为 ? ToNumber(value)。
  6. relativeStart 为 ? ToIntegerOrInfinity(start)。
  7. 如果 relativeStart = -∞,令 startIndex 为 0。
  8. 否则如果 relativeStart < 0,令 startIndexmax(len + relativeStart, 0)。
  9. 否则,令 startIndexmin(relativeStart, len)。
  10. 如果 endundefined,令 relativeEndlen;否则令 relativeEnd 为 ? ToIntegerOrInfinity(end)。
  11. 如果 relativeEnd = -∞,令 endIndex 为 0。
  12. 否则如果 relativeEnd < 0,令 endIndexmax(len + relativeEnd, 0)。
  13. 否则,令 endIndexmin(relativeEnd, len)。
  14. taRecord 设置为 MakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  15. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
  16. len 设置为 TypedArrayLength(taRecord)。
  17. endIndex 设置为 min(endIndex, len)。
  18. kstartIndex
  19. 重复,当 k < endIndex 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. 执行 ! Set(obj, propertyKey, value, true)。
    3. k 设置为 k + 1。
  20. 返回 obj

23.2.3.10 %TypedArray%.prototype.filter ( callback [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.8 中定义的 Array.prototype.filter 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  5. kept 为一个新的空 List
  6. captured 为 0。
  7. k 为 0。
  8. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ! Get(obj, propertyKey)。
    3. selectedToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
    4. 如果 selectedtrue,则
      1. kValue 追加到 kept
      2. captured 设置为 captured + 1。
    5. k 设置为 k + 1。
  9. resultTypedArray 为 ? TypedArraySpeciesCreate(obj, « 𝔽(captured) »)。
  10. n 为 0。
  11. 对于 kept 的每个元素 e,执行
    1. 执行 ! Set(resultTypedArray, ! ToString(𝔽(n)), e, true)。
    2. n 设置为 n + 1。
  12. 返回 resultTypedArray

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.11 %TypedArray%.prototype.find ( predicate [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.9 中定义的 Array.prototype.find 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. findRec 为 ? FindViaPredicate(obj, len, ascending, predicate, thisArg)。
  5. 返回 findRec.[[Value]]

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.12 %TypedArray%.prototype.findIndex ( predicate [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.10 中定义的 Array.prototype.findIndex 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. findRec 为 ? FindViaPredicate(obj, len, ascending, predicate, thisArg)。
  5. 返回 findRec.[[Index]]

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.13 %TypedArray%.prototype.findLast ( predicate [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.11 中定义的 Array.prototype.findLast 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. findRec 为 ? FindViaPredicate(obj, len, descending, predicate, thisArg)。
  5. 返回 findRec.[[Value]]

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.14 %TypedArray%.prototype.findLastIndex ( predicate [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.12 中定义的 Array.prototype.findLastIndex 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. findRec 为 ? FindViaPredicate(obj, len, descending, predicate, thisArg)。
  5. 返回 findRec.[[Index]]

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.15 %TypedArray%.prototype.forEach ( callback [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.15 中定义的 Array.prototype.forEach 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  5. k 为 0。
  6. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ! Get(obj, propertyKey)。
    3. 执行 ? Call(callback, thisArg, « kValue, 𝔽(k), obj »)。
    4. k 设置为 k + 1。
  7. 返回 undefined

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.16 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] )

此方法的参数解释和使用方式与 23.1.3.16 中定义的 Array.prototype.includes 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 len = 0,返回 false
  5. n 为 ? ToIntegerOrInfinity(fromIndex)。
  6. 断言:如果 fromIndexundefined,则 n 为 0。
  7. 如果 n = +∞,返回 false
  8. 如果 n = -∞,将 n 设置为 0。
  9. 如果 n ≥ 0,则
    1. kn
  10. 否则,
    1. klen + n
    2. 如果 k < 0,将 k 设置为 0。
  11. 重复,当 k < len 时,
    1. elementK 为 ! Get(obj, ! ToString(𝔽(k)))。
    2. 如果 SameValueZero(searchElement, elementK) 是 true,返回 true
    3. k 设置为 k + 1。
  12. 返回 false

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.17 %TypedArray%.prototype.indexOf ( searchElement [ , fromIndex ] )

此方法的参数解释和使用方式与 23.1.3.17 中定义的 Array.prototype.indexOf 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 len = 0,返回 -1𝔽
  5. n 为 ? ToIntegerOrInfinity(fromIndex)。
  6. 断言:如果 fromIndexundefined,则 n 为 0。
  7. 如果 n = +∞,返回 -1𝔽
  8. 如果 n = -∞,将 n 设置为 0。
  9. 如果 n ≥ 0,则
    1. kn
  10. 否则,
    1. klen + n
    2. 如果 k < 0,将 k 设置为 0。
  11. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ! HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. elementK 为 ! Get(obj, propertyKey)。
      2. 如果 IsStrictlyEqual(searchElement, elementK) 是 true,返回 𝔽(k)。
    4. k 设置为 k + 1。
  12. 返回 -1𝔽

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.18 %TypedArray%.prototype.join ( separator )

此方法的参数解释和使用方式与 23.1.3.18 中定义的 Array.prototype.join 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 separatorundefined,令 sep","
  5. 否则,令 sep 为 ? ToString(separator)。
  6. result 为空 String。
  7. k 为 0。
  8. 重复,当 k < len 时,
    1. 如果 k > 0,将 result 设置为 resultsep 的字符串拼接。
    2. element 为 ! Get(obj, ! ToString(𝔽(k)))。
    3. 如果 element 不是 undefined,则
      1. elementStr 为 ! ToString(element)。
      2. result 设置为 resultelementStr 的字符串拼接。
    4. k 设置为 k + 1。
  9. 返回 result

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.19 %TypedArray%.prototype.keys ( )

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? ValidateTypedArray(obj, seq-cst)。
  3. 返回 CreateArrayIterator(obj, key)。

23.2.3.20 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] )

此方法的参数解释和使用方式与 23.1.3.20 中定义的 Array.prototype.lastIndexOf 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 len = 0,返回 -1𝔽
  5. 如果 fromIndex 存在,令 n 为 ? ToIntegerOrInfinity(fromIndex);否则令 nlen - 1。
  6. 如果 n = -∞,返回 -1𝔽
  7. 如果 n ≥ 0,则
    1. kmin(n, len - 1)。
  8. 否则,
    1. klen + n
  9. 重复,当 k ≥ 0 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kPresent 为 ! HasProperty(obj, propertyKey)。
    3. 如果 kPresenttrue,则
      1. elementK 为 ! Get(obj, propertyKey)。
      2. 如果 IsStrictlyEqual(searchElement, elementK) 是 true,返回 𝔽(k)。
    4. k 设置为 k - 1。
  10. 返回 -1𝔽

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.21 get %TypedArray%.prototype.length

%TypedArray%.prototype.length 是一个访问器属性,其 set 访问器函数为 undefined。它的 get 访问器函数在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj[[ViewedArrayBuffer]][[ArrayLength]] 内部槽。
  4. taRecordMakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  5. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,返回 +0𝔽
  6. lengthTypedArrayLength(taRecord)。
  7. 返回 𝔽(length)。

此函数不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.22 %TypedArray%.prototype.map ( callback [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.21 中定义的 Array.prototype.map 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  5. resultTypedArray 为 ? TypedArraySpeciesCreate(obj, « 𝔽(len) »)。
  6. k 为 0。
  7. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ! Get(obj, propertyKey)。
    3. mappedValue 为 ? Call(callback, thisArg, « kValue, 𝔽(k), obj »)。
    4. 执行 ? Set(resultTypedArray, propertyKey, mappedValue, true)。
    5. k 设置为 k + 1。
  8. 返回 resultTypedArray

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.23 %TypedArray%.prototype.reduce ( callback [ , initialValue ] )

此方法的参数解释和使用方式与 23.1.3.24 中定义的 Array.prototype.reduce 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  5. 如果 len = 0 且 initialValue 不存在,抛出 TypeError 异常。
  6. k 为 0。
  7. accumulatorundefined
  8. 如果 initialValue 存在,则
    1. accumulator 设置为 initialValue
  9. 否则,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. accumulator 设置为 ! Get(obj, propertyKey)。
    3. k 设置为 k + 1。
  10. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ! Get(obj, propertyKey)。
    3. accumulator 设置为 ? Call(callback, undefined, « accumulator, kValue, 𝔽(k), obj »)。
    4. k 设置为 k + 1。
  11. 返回 accumulator

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.24 %TypedArray%.prototype.reduceRight ( callback [ , initialValue ] )

此方法的参数解释和使用方式与 23.1.3.25 中定义的 Array.prototype.reduceRight 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  5. 如果 len = 0 且 initialValue 不存在,抛出 TypeError 异常。
  6. klen - 1。
  7. accumulatorundefined
  8. 如果 initialValue 存在,则
    1. accumulator 设置为 initialValue
  9. 否则,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. accumulator 设置为 ! Get(obj, propertyKey)。
    3. k 设置为 k - 1。
  10. 重复,当 k ≥ 0 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ! Get(obj, propertyKey)。
    3. accumulator 设置为 ? Call(callback, undefined, « accumulator, kValue, 𝔽(k), obj »)。
    4. k 设置为 k - 1。
  11. 返回 accumulator

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.25 %TypedArray%.prototype.reverse ( )

此方法的参数解释和使用方式与 23.1.3.26 中定义的 Array.prototype.reverse 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. middlefloor(len / 2)。
  5. lower 为 0。
  6. 重复,当 lowermiddle 时,
    1. upperlen - lower - 1。
    2. upperP 为 ! ToString(𝔽(upper))。
    3. lowerP 为 ! ToString(𝔽(lower))。
    4. lowerValue 为 ! Get(obj, lowerP)。
    5. upperValue 为 ! Get(obj, upperP)。
    6. 执行 ! Set(obj, lowerP, upperValue, true)。
    7. 执行 ! Set(obj, upperP, lowerValue, true)。
    8. lower 设置为 lower + 1。
  7. 返回 obj

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.26 %TypedArray%.prototype.set ( source [ , offset ] )

此方法在此 TypedArray 中设置多个值,从 source 读取值。细节取决于 source 的类型。可选的 offset 值指示在此 TypedArray 中写入值的第一个元素索引。如果省略,则假定为 0。

它在被调用时执行以下步骤:

  1. targetthis 值。
  2. 执行 ? RequireInternalSlot(target, [[TypedArrayName]])。
  3. 断言:target[[ViewedArrayBuffer]] 内部槽。
  4. targetOffset 为 ? ToIntegerOrInfinity(offset)。
  5. 如果 targetOffset < 0,抛出 RangeError 异常。
  6. 如果 source 是一个具有 [[TypedArrayName]] 内部槽的 Object,则
    1. 执行 ? SetTypedArrayFromTypedArray(target, targetOffset, source)。
  7. 否则,
    1. 执行 ? SetTypedArrayFromArrayLike(target, targetOffset, source)。
  8. 返回 undefined

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.26.1 SetTypedArrayFromArrayLike ( target, targetOffset, source )

The abstract operation SetTypedArrayFromArrayLike takes arguments target (一个 TypedArray,), targetOffset (一个非负整数或 +∞,), and source (一个 ECMAScript 语言值,但不是 TypedArray,) and returns 要么是一个包含 unused正常完成,要么是一个抛出完成. 它从 source 读取值,从索引 targetOffset 开始在 target 中设置多个值。 It performs the following steps when called:

  1. targetRecordMakeTypedArrayWithBufferWitnessRecord(target, seq-cst)。
  2. 如果 IsTypedArrayOutOfBounds(targetRecord) 是 true,抛出 TypeError 异常。
  3. targetLengthTypedArrayLength(targetRecord)。
  4. src 为 ? ToObject(source)。
  5. srcLength 为 ? LengthOfArrayLike(src)。
  6. 如果 targetOffset = +∞,抛出 RangeError 异常。
  7. 如果 srcLength + targetOffset > targetLength,抛出 RangeError 异常。
  8. k 为 0。
  9. 重复,当 k < srcLength 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. value 为 ? Get(src, propertyKey)。
    3. targetIndex𝔽(targetOffset + k)。
    4. 执行 ? TypedArraySetElement(target, targetIndex, value)。
    5. k 设置为 k + 1。
  10. 返回 unused

23.2.3.26.2 SetTypedArrayFromTypedArray ( target, targetOffset, source )

The abstract operation SetTypedArrayFromTypedArray takes arguments target (一个 TypedArray,), targetOffset (一个非负整数或 +∞,), and source (一个 TypedArray,) and returns 要么是一个包含 unused正常完成,要么是一个抛出完成. 它从 source 读取值,从索引 targetOffset 开始在 target 中设置多个值。 It performs the following steps when called:

  1. targetBuffertarget.[[ViewedArrayBuffer]]
  2. targetRecordMakeTypedArrayWithBufferWitnessRecord(target, seq-cst)。
  3. 如果 IsTypedArrayOutOfBounds(targetRecord) 是 true,抛出 TypeError 异常。
  4. targetLengthTypedArrayLength(targetRecord)。
  5. srcBuffersource.[[ViewedArrayBuffer]]
  6. srcRecordMakeTypedArrayWithBufferWitnessRecord(source, seq-cst)。
  7. 如果 IsTypedArrayOutOfBounds(srcRecord) 是 true,抛出 TypeError 异常。
  8. srcLengthTypedArrayLength(srcRecord)。
  9. targetTypeTypedArrayElementType(target)。
  10. targetElementSizeTypedArrayElementSize(target)。
  11. targetByteOffsettarget.[[ByteOffset]]
  12. srcTypeTypedArrayElementType(source)。
  13. srcElementSizeTypedArrayElementSize(source)。
  14. srcByteOffsetsource.[[ByteOffset]]
  15. 如果 targetOffset = +∞,抛出 RangeError 异常。
  16. 如果 srcLength + targetOffset > targetLength,抛出 RangeError 异常。
  17. 如果 target.[[ContentType]] 不是 source.[[ContentType]],抛出 TypeError 异常。
  18. 如果 IsSharedArrayBuffer(srcBuffer) 是 trueIsSharedArrayBuffer(targetBuffer) 是 true,并且 srcBuffer.[[ArrayBufferData]]targetBuffer.[[ArrayBufferData]],令 sameSharedArrayBuffertrue;否则令 sameSharedArrayBufferfalse
  19. 如果 SameValue(srcBuffer, targetBuffer) 是 truesameSharedArrayBuffertrue,则
    1. srcByteLengthTypedArrayByteLength(srcRecord)。
    2. srcBuffer 设置为 ? CloneArrayBuffer(srcBuffer, srcByteOffset, srcByteLength)。
    3. srcByteIndex 为 0。
  20. 否则,
    1. srcByteIndexsrcByteOffset
  21. targetByteIndex 为 (targetOffset × targetElementSize) + targetByteOffset
  22. limittargetByteIndex + (targetElementSize × srcLength)。
  23. 如果 srcTypetargetType,则
    1. 注:传输必须以保留源数据位级编码的方式执行。
    2. 重复,当 targetByteIndex < limit 时,
      1. valueGetValueFromBuffer(srcBuffer, srcByteIndex, uint8, true, unordered)。
      2. 执行 SetValueInBuffer(targetBuffer, targetByteIndex, uint8, value, true, unordered)。
      3. srcByteIndex 设置为 srcByteIndex + 1。
      4. targetByteIndex 设置为 targetByteIndex + 1。
  24. 否则,
    1. 重复,当 targetByteIndex < limit 时,
      1. valueGetValueFromBuffer(srcBuffer, srcByteIndex, srcType, true, unordered)。
      2. 执行 SetValueInBuffer(targetBuffer, targetByteIndex, targetType, value, true, unordered)。
      3. srcByteIndex 设置为 srcByteIndex + srcElementSize
      4. targetByteIndex 设置为 targetByteIndex + targetElementSize
  25. 返回 unused

23.2.3.27 %TypedArray%.prototype.slice ( start, end )

此方法的参数解释和使用方式与 23.1.3.28 中定义的 Array.prototype.slice 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. srcArrayLengthTypedArrayLength(taRecord)。
  4. relativeStart 为 ? ToIntegerOrInfinity(start)。
  5. 如果 relativeStart = -∞,令 startIndex 为 0。
  6. 否则如果 relativeStart < 0,令 startIndexmax(srcArrayLength + relativeStart, 0)。
  7. 否则,令 startIndexmin(relativeStart, srcArrayLength)。
  8. 如果 endundefined,令 relativeEndsrcArrayLength;否则令 relativeEnd 为 ? ToIntegerOrInfinity(end)。
  9. 如果 relativeEnd = -∞,令 endIndex 为 0。
  10. 否则如果 relativeEnd < 0,令 endIndexmax(srcArrayLength + relativeEnd, 0)。
  11. 否则,令 endIndexmin(relativeEnd, srcArrayLength)。
  12. countBytesmax(endIndex - startIndex, 0)。
  13. resultArray 为 ? TypedArraySpeciesCreate(obj, « 𝔽(countBytes) »)。
  14. 如果 countBytes > 0,则
    1. taRecord 设置为 MakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
    2. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
    3. endIndex 设置为 min(endIndex, TypedArrayLength(taRecord))。
    4. countBytes 设置为 max(endIndex - startIndex, 0)。
    5. srcTypeTypedArrayElementType(obj)。
    6. targetTypeTypedArrayElementType(resultArray)。
    7. 如果 srcTypetargetType,则
      1. 注:传输必须以保留源数据位级编码的方式执行。
      2. srcBufferobj.[[ViewedArrayBuffer]]
      3. targetBufferresultArray.[[ViewedArrayBuffer]]
      4. elementSizeTypedArrayElementSize(obj)。
      5. srcByteOffsetobj.[[ByteOffset]]
      6. srcByteIndex 为 (startIndex × elementSize) + srcByteOffset
      7. targetByteIndexresultArray.[[ByteOffset]]
      8. endByteIndextargetByteIndex + (countBytes × elementSize)。
      9. 重复,当 targetByteIndex < endByteIndex 时,
        1. valueGetValueFromBuffer(srcBuffer, srcByteIndex, uint8, true, unordered)。
        2. 执行 SetValueInBuffer(targetBuffer, targetByteIndex, uint8, value, true, unordered)。
        3. srcByteIndex 设置为 srcByteIndex + 1。
        4. targetByteIndex 设置为 targetByteIndex + 1。
    8. 否则,
      1. n 为 0。
      2. kstartIndex
      3. 重复,当 k < endIndex 时,
        1. propertyKey 为 ! ToString(𝔽(k))。
        2. kValue 为 ! Get(obj, propertyKey)。
        3. 执行 ! Set(resultArray, ! ToString(𝔽(n)), kValue, true)。
        4. k 设置为 k + 1。
        5. n 设置为 n + 1。
  15. 返回 resultArray

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.28 %TypedArray%.prototype.some ( callback [ , thisArg ] )

此方法的参数解释和使用方式与 23.1.3.29 中定义的 Array.prototype.some 相同。

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,抛出 TypeError 异常。
  5. k 为 0。
  6. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ! Get(obj, propertyKey)。
    3. testResultToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
    4. 如果 testResulttrue,返回 true
    5. k 设置为 k + 1。
  7. 返回 false

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.29 %TypedArray%.prototype.sort ( comparator )

这是一个不同的方法,除下文所述外,它实现与 23.1.3.30 中定义的 Array.prototype.sort 相同的要求。此方法的实现可以基于以下事实进行优化:this 值是一个具有固定长度且其整数索引属性不稀疏的对象。

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

它在被调用时执行以下步骤:

  1. 如果 comparator 不是 undefinedIsCallable(comparator) 是 false,抛出 TypeError 异常。
  2. objthis 值。
  3. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  4. lenTypedArrayLength(taRecord)。
  5. 注:下面的闭包执行数值比较,而不是 23.1.3.30 中使用的字符串比较。
  6. sortCompare 为一个新的 Abstract Closure,其参数为 (x, y),捕获 comparator,并在被调用时执行以下步骤:
    1. 返回 ? CompareTypedArrayElements(x, y, comparator)。
  7. sortedList 为 ? SortIndexedProperties(obj, len, sortCompare, read-through-holes)。
  8. j 为 0。
  9. 重复,当 j < len 时,
    1. 执行 ! Set(obj, ! ToString(𝔽(j)), sortedList[j], true)。
    2. j 设置为 j + 1。
  10. 返回 obj
Note

因为 NaN 总是比较为大于任何其他值(见 CompareTypedArrayElements),所以当未提供 comparator 时,NaN 属性值总是排序到结果末尾。

23.2.3.30 %TypedArray%.prototype.subarray ( start, end )

此方法返回一个新的 TypedArray,其元素类型是此 TypedArray 的元素类型,其 ArrayBuffer 是此 TypedArray 的 ArrayBuffer,并引用从 start(包含)到 end(不包含)区间内的元素。如果 startend 为负,则它引用的是从数组末尾开始的索引,而不是从开头开始。

它在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj[[ViewedArrayBuffer]] 内部槽。
  4. bufferobj.[[ViewedArrayBuffer]]
  5. srcRecordMakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  6. 如果 IsTypedArrayOutOfBounds(srcRecord) 是 true,则
    1. srcLength 为 0。
  7. 否则,
    1. srcLengthTypedArrayLength(srcRecord)。
  8. relativeStart 为 ? ToIntegerOrInfinity(start)。
  9. 如果 relativeStart = -∞,令 startIndex 为 0。
  10. 否则如果 relativeStart < 0,令 startIndexmax(srcLength + relativeStart, 0)。
  11. 否则,令 startIndexmin(relativeStart, srcLength)。
  12. elementSizeTypedArrayElementSize(obj)。
  13. srcByteOffsetobj.[[ByteOffset]]
  14. beginByteOffsetsrcByteOffset + (startIndex × elementSize)。
  15. 如果 obj.[[ArrayLength]]autoendundefined,则
    1. argumentsList 为 « buffer, 𝔽(beginByteOffset) »。
  16. 否则,
    1. 如果 endundefined,令 relativeEndsrcLength;否则令 relativeEnd 为 ? ToIntegerOrInfinity(end)。
    2. 如果 relativeEnd = -∞,令 endIndex 为 0。
    3. 否则如果 relativeEnd < 0,令 endIndexmax(srcLength + relativeEnd, 0)。
    4. 否则,令 endIndexmin(relativeEnd, srcLength)。
    5. newLengthmax(endIndex - startIndex, 0)。
    6. argumentsList 为 « buffer, 𝔽(beginByteOffset), 𝔽(newLength) »。
  17. 返回 ? TypedArraySpeciesCreate(obj, argumentsList)。

此方法不是泛型的。this 值必须是一个具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.31 %TypedArray%.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )

这是一个不同的方法,它实现与 23.1.3.32 中定义的 Array.prototype.toLocaleString 相同的算法,不同之处在于调用 TypedArrayLength 代替执行对 "length"[[Get]]。在底层缓冲区不可调整大小时,可以基于以下事实优化该算法的实现:this 值具有固定长度且其整数索引属性不稀疏。然而,此类优化不得在算法的指定行为中引入任何可观察变化。

此方法不是泛型的。在求值该算法之前,会以 this 值和 seq-cst 作为参数调用 ValidateTypedArray。如果其结果是一个 abrupt completion,则抛出该异常,而不是求值该算法。

Note

如果 ECMAScript 实现包含 ECMA-402 国际化 API,则此方法基于 ECMA-402 规范中的 Array.prototype.toLocaleString 算法。

23.2.3.32 %TypedArray%.prototype.toReversed ( )

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. resultArray 为 ? TypedArrayCreateSameType(obj, len)。
  5. k 为 0。
  6. 重复,当 k < len 时,
    1. from 为 ! ToString(𝔽(len - k - 1))。
    2. propertyKey 为 ! ToString(𝔽(k))。
    3. fromValue 为 ! Get(obj, from)。
    4. 执行 ! Set(resultArray, propertyKey, fromValue, true)。
    5. k 设置为 k + 1。
  7. 返回 resultArray

23.2.3.33 %TypedArray%.prototype.toSorted ( comparator )

此方法在被调用时执行以下步骤:

  1. 如果 comparator 不是 undefinedIsCallable(comparator) 是 false,抛出 TypeError 异常。
  2. objthis 值。
  3. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  4. lenTypedArrayLength(taRecord)。
  5. resultArray 为 ? TypedArrayCreateSameType(obj, len)。
  6. 注:下面的闭包执行数值比较,而不是 23.1.3.34 中使用的字符串比较。
  7. sortCompare 为一个新的 Abstract Closure,其参数为 (x, y),捕获 comparator,并在被调用时执行以下步骤:
    1. 返回 ? CompareTypedArrayElements(x, y, comparator)。
  8. sortedList 为 ? SortIndexedProperties(obj, len, sortCompare, read-through-holes)。
  9. j 为 0。
  10. 重复,当 j < len 时,
    1. 执行 ! Set(resultArray, ! ToString(𝔽(j)), sortedList[j], true)。
    2. j 设置为 j + 1。
  11. 返回 resultArray

23.2.3.34 %TypedArray%.prototype.toString ( )

"toString" 属性的初始值为 %Array.prototype.toString%,定义见 23.1.3.36

23.2.3.35 %TypedArray%.prototype.values ( )

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. 执行 ? ValidateTypedArray(obj, seq-cst)。
  3. 返回 CreateArrayIterator(obj, value)。

23.2.3.36 %TypedArray%.prototype.with ( index, value )

此方法在被调用时执行以下步骤:

  1. objthis 值。
  2. taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. lenTypedArrayLength(taRecord)。
  4. relativeIndex 为 ? ToIntegerOrInfinity(index)。
  5. 如果 relativeIndex ≥ 0,令 actualIndexrelativeIndex
  6. 否则,令 actualIndexlen + relativeIndex
  7. 如果 obj.[[ContentType]]bigint,令 numericValue 为 ? ToBigInt(value)。
  8. 否则,令 numericValue 为 ? ToNumber(value)。
  9. 如果 IsValidIntegerIndex(obj, 𝔽(actualIndex)) 是 false,抛出 RangeError 异常。
  10. resultArray 为 ? TypedArrayCreateSameType(obj, len)。
  11. k 为 0。
  12. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. 如果 k = actualIndex,令 fromValuenumericValue
    3. 否则,令 fromValue 为 ! Get(obj, propertyKey)。
    4. 执行 ! Set(resultArray, propertyKey, fromValue, true)。
    5. k 设置为 k + 1。
  13. 返回 resultArray

23.2.3.37 %TypedArray%.prototype [ %Symbol.iterator% ] ( )

%Symbol.iterator% 属性的初始值为 %TypedArray.prototype.values%,定义见 23.2.3.35

23.2.3.38 get %TypedArray%.prototype [ %Symbol.toStringTag% ]

%TypedArray%.prototype[%Symbol.toStringTag%] 是一个访问器属性,其 set 访问器函数为 undefined。它的 get 访问器函数在被调用时执行以下步骤:

  1. objthis 值。
  2. 如果 obj 不是 Object,返回 undefined
  3. 如果 obj 没有 [[TypedArrayName]] 内部槽,返回 undefined
  4. nameobj.[[TypedArrayName]]
  5. 断言:name 是一个 String。
  6. 返回 name

此属性具有特性 { [[Enumerable]]: false, [[Configurable]]: true }。

此函数的 "name" 属性的初始值为 "get [Symbol.toStringTag]"

23.2.4 TypedArray 对象的抽象操作

23.2.4.1 TypedArrayCreateFromConstructor ( constructor, argumentList )

The abstract operation TypedArrayCreateFromConstructor takes arguments constructor (一个构造器,) and argumentList (一个 ECMAScript 语言值 List,) and returns 要么是一个包含 TypedArray正常完成,要么是一个抛出完成. 它用于指定使用构造器函数创建一个新的 TypedArray。 It performs the following steps when called:

  1. newTypedArray 为 ? Construct(constructor, argumentList)。
  2. taRecord 为 ? ValidateTypedArray(newTypedArray, seq-cst)。
  3. 断言:newTypedArray 具有 TypedArray 实例的属性 中提到的所有内部槽。
  4. 如果 argumentList 中的元素数量为 1,且 argumentList[0] 是 Number,则
    1. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
    2. lengthTypedArrayLength(taRecord)。
    3. 如果 length < (argumentList[0]),抛出 TypeError 异常。
  5. 返回 newTypedArray

23.2.4.2 TypedArrayCreateSameType ( exemplar, length )

The abstract operation TypedArrayCreateSameType takes arguments exemplar (一个 TypedArray,) and length (一个非负整数,) and returns 要么是一个包含 TypedArray正常完成,要么是一个抛出完成. 它用于指定使用从 exemplar 派生的构造器函数创建一个新的 TypedArray。与可以通过使用 %Symbol.species% 构造自定义 TypedArray 子类的 TypedArraySpeciesCreate 不同,此操作始终使用内置 TypedArray 构造器之一。 It performs the following steps when called:

  1. constructorTable 70 中与构造器名称 exemplar.[[TypedArrayName]] 关联的内在对象。
  2. result 为 ? TypedArrayCreateFromConstructor(constructor, « 𝔽(length) »)。
  3. 断言:result 具有 [[TypedArrayName]][[ContentType]] 内部槽。
  4. 断言:result.[[ContentType]]exemplar.[[ContentType]]
  5. 返回 result

23.2.4.3 TypedArraySpeciesCreate ( exemplar, argumentList )

The abstract operation TypedArraySpeciesCreate takes arguments exemplar (一个 TypedArray,) and argumentList (一个 ECMAScript 语言值 List,) and returns 要么是一个包含 TypedArray正常完成,要么是一个抛出完成. 它用于指定使用从 exemplar 派生的构造器函数创建一个新的 TypedArray。与可以通过使用 %Symbol.species% 创建非 Array 对象的 ArraySpeciesCreate 不同,此操作强制要求该构造器函数创建一个真正的 TypedArray。 It performs the following steps when called:

  1. defaultConstructorTable 70 中与构造器名称 exemplar.[[TypedArrayName]] 关联的内在对象。
  2. constructor 为 ? SpeciesConstructor(exemplar, defaultConstructor)。
  3. result 为 ? TypedArrayCreateFromConstructor(constructor, argumentList)。
  4. 如果 result.[[ContentType]] 不是 exemplar.[[ContentType]],抛出 TypeError 异常。
  5. 返回 result

23.2.4.4 ValidateTypedArray ( obj, order )

The abstract operation ValidateTypedArray takes arguments obj (一个 ECMAScript 语言值,) and order (seq-cstunordered,) and returns 要么是一个包含 TypedArray With Buffer Witness Record正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  2. 断言:obj[[ViewedArrayBuffer]] 内部槽。
  3. taRecordMakeTypedArrayWithBufferWitnessRecord(obj, order)。
  4. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
  5. 返回 taRecord

23.2.4.5 TypedArrayElementSize ( obj )

The abstract operation TypedArrayElementSize takes argument obj (一个 TypedArray,) and returns 一个非负整数. It performs the following steps when called:

  1. 返回 Table 70 中为 obj.[[TypedArrayName]] 指定的 Element Size 值。

23.2.4.6 TypedArrayElementType ( obj )

The abstract operation TypedArrayElementType takes argument obj (一个 TypedArray,) and returns 一个 TypedArray 元素类型. It performs the following steps when called:

  1. 返回 Table 70 中为 obj.[[TypedArrayName]] 指定的 Element Type 值。

23.2.4.7 CompareTypedArrayElements ( x, y, comparator )

The abstract operation CompareTypedArrayElements takes arguments x (一个 Number 或 BigInt,), y (一个 Number 或 BigInt,), and comparator (一个函数对象undefined,) and returns 要么是一个包含 Number 的正常完成,要么是一个 abrupt completion. It performs the following steps when called:

  1. 断言:x 是 Number 且 y 是 Number,或者 x 是 BigInt 且 y 是 BigInt。
  2. 如果 comparator 不是 undefined,则
    1. result 为 ? ToNumber(? Call(comparator, undefined, « x, y »))。
    2. 如果 resultNaN,返回 +0𝔽
    3. 返回 result
  3. 如果 xNaNyNaN,返回 +0𝔽
  4. 如果 xNaN,返回 1𝔽
  5. 如果 yNaN,返回 -1𝔽
  6. 如果 x < y,返回 -1𝔽
  7. 如果 x > y,返回 1𝔽
  8. 如果 x-0𝔽y+0𝔽,返回 -1𝔽
  9. 如果 x+0𝔽y-0𝔽,返回 1𝔽
  10. 返回 +0𝔽
Note
此操作执行数值比较,而不是 23.1.3.30.2 中使用的字符串比较。

23.2.5 TypedArray 构造器

每个 TypedArray 构造器

  • 是一个内在对象,具有下文所述的结构,除非另有说明,否则仅在 Table 70 中使用的构造器名称替代 TypedArray 这一点上有所不同。
  • 是一个其行为会根据参数数量和类型而不同的函数。TypedArray 调用的实际行为取决于传递给它的参数数量和种类。
  • 不意图作为函数调用,并且以这种方式调用时会抛出异常。
  • 可用作类定义中 extends 子句的值。意图继承指定 TypedArray 行为的子类构造器必须包含对 TypedArray 构造器super 调用,以创建并初始化带有支持 %TypedArray%.prototype 内置方法所需内部状态的子类实例。

23.2.5.1 TypedArray ( ...args )

每个 TypedArray 构造器在被调用时执行以下步骤:

  1. 如果 NewTarget 是 undefined,抛出 TypeError 异常。
  2. constructorNameTable 70 中为此 TypedArray 构造器指定的 Constructor Name 值的 String 值。
  3. proto"%TypedArray.prototype%"
  4. numberOfArgsargs 中元素的数量。
  5. 如果 numberOfArgs = 0,返回 ? AllocateTypedArray(constructorName, NewTarget, proto, 0)。
  6. firstArgumentargs[0]。
  7. 如果 firstArgument 是 Object,则
    1. obj 为 ? AllocateTypedArray(constructorName, NewTarget, proto)。
    2. 如果 firstArgument[[TypedArrayName]] 内部槽,则
      1. 执行 ? InitializeTypedArrayFromTypedArray(obj, firstArgument)。
    3. 否则如果 firstArgument[[ArrayBufferData]] 内部槽,则
      1. 如果 numberOfArgs > 1,令 byteOffsetargs[1];否则令 byteOffsetundefined
      2. 如果 numberOfArgs > 2,令 lengthargs[2];否则令 lengthundefined
      3. 执行 ? InitializeTypedArrayFromArrayBuffer(obj, firstArgument, byteOffset, length)。
    4. 否则,
      1. 断言:firstArgument 是 Object,并且 firstArgument 既没有 [[TypedArrayName]] 内部槽,也没有 [[ArrayBufferData]] 内部槽。
      2. usingIterator 为 ? GetMethod(firstArgument, %Symbol.iterator%)。
      3. 如果 usingIterator 不是 undefined,则
        1. values 为 ? IteratorToList(? GetIteratorFromMethod(firstArgument, usingIterator))。
        2. 执行 ? InitializeTypedArrayFromList(obj, values)。
      4. 否则,
        1. 注:firstArgument 不是可迭代对象,因此假定它已经是一个类数组对象
        2. 执行 ? InitializeTypedArrayFromArrayLike(obj, firstArgument)。
    5. 返回 obj
  8. 断言:firstArgument 不是 Object。
  9. elementLength 为 ? ToIndex(firstArgument)。
  10. 返回 ? AllocateTypedArray(constructorName, NewTarget, proto, elementLength)。

23.2.5.1.1 AllocateTypedArray ( constructorName, newTarget, defaultProto [ , length ] )

The abstract operation AllocateTypedArray takes arguments constructorName (一个 String,它是 Table 70 中的 TypedArray 构造器名称,), newTarget (一个构造器,), and defaultProto (一个 String,) and optional argument length (一个非负整数,) and returns 要么是一个包含 TypedArray正常完成,要么是一个抛出完成. 它用于验证并创建 TypedArray 构造器的实例。如果传递了 length 参数,还会分配该长度的 ArrayBuffer 并与新的 TypedArray 实例关联。AllocateTypedArray 提供由 TypedArray 使用的公共语义。 It performs the following steps when called:

  1. proto 为 ? GetPrototypeFromConstructor(newTarget, defaultProto)。
  2. objTypedArrayCreate(proto)。
  3. 断言:obj.[[ViewedArrayBuffer]]undefined
  4. obj.[[TypedArrayName]] 设置为 constructorName
  5. 如果 constructorName"BigInt64Array""BigUint64Array",将 obj.[[ContentType]] 设置为 bigint
  6. 否则,将 obj.[[ContentType]] 设置为 number
  7. 如果 length 不存在,则
    1. obj.[[ByteLength]] 设置为 0。
    2. obj.[[ByteOffset]] 设置为 0。
    3. obj.[[ArrayLength]] 设置为 0。
  8. 否则,
    1. 执行 ? AllocateTypedArrayBuffer(obj, length)。
  9. 返回 obj

23.2.5.1.2 InitializeTypedArrayFromTypedArray ( obj, srcArray )

The abstract operation InitializeTypedArrayFromTypedArray takes arguments obj (一个 TypedArray,) and srcArray (一个 TypedArray,) and returns 要么是一个包含 unused正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. srcDatasrcArray.[[ViewedArrayBuffer]]
  2. elementTypeTypedArrayElementType(obj)。
  3. elementSizeTypedArrayElementSize(obj)。
  4. srcTypeTypedArrayElementType(srcArray)。
  5. srcElementSizeTypedArrayElementSize(srcArray)。
  6. srcByteOffsetsrcArray.[[ByteOffset]]
  7. srcRecordMakeTypedArrayWithBufferWitnessRecord(srcArray, seq-cst)。
  8. 如果 IsTypedArrayOutOfBounds(srcRecord) 是 true,抛出 TypeError 异常。
  9. elementLengthTypedArrayLength(srcRecord)。
  10. byteLengthelementSize × elementLength
  11. 如果 elementTypesrcType,则
    1. data 为 ? CloneArrayBuffer(srcData, srcByteOffset, byteLength)。
  12. 否则,
    1. data 为 ? AllocateArrayBuffer(%ArrayBuffer%, byteLength)。
    2. 如果 srcArray.[[ContentType]] 不是 obj.[[ContentType]],抛出 TypeError 异常。
    3. srcByteIndexsrcByteOffset
    4. targetByteIndex 为 0。
    5. countelementLength
    6. 重复,当 count > 0 时,
      1. valueGetValueFromBuffer(srcData, srcByteIndex, srcType, true, unordered)。
      2. 执行 SetValueInBuffer(data, targetByteIndex, elementType, value, true, unordered)。
      3. srcByteIndex 设置为 srcByteIndex + srcElementSize
      4. targetByteIndex 设置为 targetByteIndex + elementSize
      5. count 设置为 count - 1。
  13. obj.[[ViewedArrayBuffer]] 设置为 data
  14. obj.[[ByteLength]] 设置为 byteLength
  15. obj.[[ByteOffset]] 设置为 0。
  16. obj.[[ArrayLength]] 设置为 elementLength
  17. 返回 unused

23.2.5.1.3 InitializeTypedArrayFromArrayBuffer ( obj, buffer, byteOffset, length )

The abstract operation InitializeTypedArrayFromArrayBuffer takes arguments obj (一个 TypedArray,), buffer (一个 ArrayBuffer 或 SharedArrayBuffer,), byteOffset (一个 ECMAScript 语言值,), and length (一个 ECMAScript 语言值,) and returns 要么是一个包含 unused正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. elementSizeTypedArrayElementSize(obj)。
  2. offset 为 ? ToIndex(byteOffset)。
  3. 如果 offset modulo elementSize ≠ 0,抛出 RangeError 异常。
  4. bufferIsFixedLengthIsFixedLengthArrayBuffer(buffer)。
  5. 如果 length 不是 undefined,则
    1. newLength 为 ? ToIndex(length)。
  6. 如果 IsDetachedBuffer(buffer) 是 true,抛出 TypeError 异常。
  7. bufferByteLengthArrayBufferByteLength(buffer, seq-cst)。
  8. 如果 lengthundefinedbufferIsFixedLengthfalse,则
    1. 如果 offset > bufferByteLength,抛出 RangeError 异常。
    2. obj.[[ByteLength]] 设置为 auto
    3. obj.[[ArrayLength]] 设置为 auto
  9. 否则,
    1. 如果 lengthundefined,则
      1. 如果 bufferByteLength modulo elementSize ≠ 0,抛出 RangeError 异常。
      2. newByteLengthbufferByteLength - offset
      3. 如果 newByteLength < 0,抛出 RangeError 异常。
    2. 否则,
      1. newByteLengthnewLength × elementSize
      2. 如果 offset + newByteLength > bufferByteLength,抛出 RangeError 异常。
    3. obj.[[ByteLength]] 设置为 newByteLength
    4. obj.[[ArrayLength]] 设置为 newByteLength / elementSize
  10. obj.[[ViewedArrayBuffer]] 设置为 buffer
  11. obj.[[ByteOffset]] 设置为 offset
  12. 返回 unused

23.2.5.1.4 InitializeTypedArrayFromList ( obj, values )

The abstract operation InitializeTypedArrayFromList takes arguments obj (一个 TypedArray,) and values (一个 ECMAScript 语言值 List,) and returns 要么是一个包含 unused正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. lenvalues 中元素的数量。
  2. 执行 ? AllocateTypedArrayBuffer(obj, len)。
  3. k 为 0。
  4. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValuevalues 的第一个元素。
    3. values 中移除第一个元素。
    4. 执行 ? Set(obj, propertyKey, kValue, true)。
    5. k 设置为 k + 1。
  5. 断言:values 现在是一个空 List
  6. 返回 unused

23.2.5.1.5 InitializeTypedArrayFromArrayLike ( obj, arrayLike )

The abstract operation InitializeTypedArrayFromArrayLike takes arguments obj (一个 TypedArray,) and arrayLike (一个 Object,但不是 TypedArray 或 ArrayBuffer,) and returns 要么是一个包含 unused正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. len 为 ? LengthOfArrayLike(arrayLike)。
  2. 执行 ? AllocateTypedArrayBuffer(obj, len)。
  3. k 为 0。
  4. 重复,当 k < len 时,
    1. propertyKey 为 ! ToString(𝔽(k))。
    2. kValue 为 ? Get(arrayLike, propertyKey)。
    3. 执行 ? Set(obj, propertyKey, kValue, true)。
    4. k 设置为 k + 1。
  5. 返回 unused

23.2.5.1.6 AllocateTypedArrayBuffer ( obj, length )

The abstract operation AllocateTypedArrayBuffer takes arguments obj (一个 TypedArray,) and length (一个非负整数,) and returns 要么是一个包含 unused正常完成,要么是一个抛出完成. 它分配一个 ArrayBuffer 并将其与 obj 关联。 It performs the following steps when called:

  1. 断言:obj.[[ViewedArrayBuffer]]undefined
  2. elementSizeTypedArrayElementSize(obj)。
  3. byteLengthelementSize × length
  4. data 为 ? AllocateArrayBuffer(%ArrayBuffer%, byteLength)。
  5. obj.[[ViewedArrayBuffer]] 设置为 data
  6. obj.[[ByteLength]] 设置为 byteLength
  7. obj.[[ByteOffset]] 设置为 0。
  8. obj.[[ArrayLength]] 设置为 length
  9. 返回 unused

23.2.6 TypedArray 构造器的属性

每个 TypedArray 构造器

  • 有一个 [[Prototype]] 内部槽,其值为 %TypedArray%
  • 有一个值为 3𝔽"length" 属性。
  • 有一个 "name" 属性,其值是在 Table 70 中为它指定的构造器名称的 String 值。
  • 具有以下属性:

23.2.6.1 TypedArray.BYTES_PER_ELEMENT

TypedArray.BYTES_PER_ELEMENT 的值是 Table 70 中为 TypedArray 指定的 Element Size 值。

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

23.2.6.2 TypedArray.prototype

TypedArray.prototype 的初始值是对应的 TypedArray 原型内在对象(23.2.7)。

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

23.2.7 TypedArray 原型对象的属性

每个 TypedArray 原型对象:

  • 有一个 [[Prototype]] 内部槽,其值为 %TypedArray.prototype%
  • 是一个普通对象
  • 没有 [[ViewedArrayBuffer]] 或任何其他特定于 TypedArray 实例对象的内部槽。

23.2.7.1 TypedArray.prototype.BYTES_PER_ELEMENT

TypedArray.prototype.BYTES_PER_ELEMENT 的值是 Table 70 中为 TypedArray 指定的 Element Size 值。

此属性具有特性 { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }。

23.2.7.2 TypedArray.prototype.constructor

给定 TypedArray 构造器的原型的 "constructor" 属性的初始值是该构造器自身。

23.2.8 TypedArray 实例的属性

TypedArray 实例是 TypedArray。每个 TypedArray 实例从对应的 TypedArray 原型对象继承属性。每个 TypedArray 实例都有以下内部槽:[[ViewedArrayBuffer]][[TypedArrayName]][[ContentType]][[ByteLength]][[ByteOffset]][[ArrayLength]]

23.3 Uint8Array 对象

Uint8Array 是上文所述的一种特定 TypedArray。此外,Uint8Array 构造器23.3.1)和 Uint8Array 原型对象(23.3.2)上还有附加方法。

23.3.1 Uint8Array 构造器的附加属性

23.3.1.1 Uint8Array.fromBase64 ( string [ , options ] )

  1. 如果 string 不是 String,抛出 TypeError 异常。
  2. opts 为 ? GetOptionsObject(options)。
  3. alphabet 为 ? Get(opts, "alphabet")。
  4. 如果 alphabetundefined,将 alphabet 设置为 "base64"
  5. 如果 alphabet 既不是 "base64" 也不是 "base64url",抛出 TypeError 异常。
  6. lastChunkHandling 为 ? Get(opts, "lastChunkHandling")。
  7. 如果 lastChunkHandlingundefined,将 lastChunkHandling 设置为 "loose"
  8. 如果 lastChunkHandling 不是 "loose""strict""stop-before-partial" 之一,抛出 TypeError 异常。
  9. resultFromBase64(string, alphabet, lastChunkHandling)。
  10. 如果 result.[[Error]] 不是 none,则
    1. 抛出 result.[[Error]]
  11. resultLengthresult.[[Bytes]] 中元素的数量。
  12. ta 为 ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength)。
  13. 断言:ta.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]]result.[[Bytes]] 中元素的数量。
  14. ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] 每个索引处的值设置为 result.[[Bytes]] 中对应索引处的值。
  15. 返回 ta

23.3.1.2 Uint8Array.fromHex ( string )

  1. 如果 string 不是 String,抛出 TypeError 异常。
  2. resultFromHex(string)。
  3. 如果 result.[[Error]] 不是 none,则
    1. 抛出 result.[[Error]]
  4. resultLengthresult.[[Bytes]] 中元素的数量。
  5. ta 为 ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength)。
  6. 断言:ta.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]]result.[[Bytes]] 中元素的数量。
  7. ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] 每个索引处的值设置为 result.[[Bytes]] 中对应索引处的值。
  8. 返回 ta

23.3.2 Uint8Array 原型对象的附加属性

23.3.2.1 Uint8Array.prototype.setFromBase64 ( string [ , options ] )

  1. intothis 值。
  2. 执行 ? ValidateUint8Array(into)。
  3. 如果 string 不是 String,抛出 TypeError 异常。
  4. opts 为 ? GetOptionsObject(options)。
  5. alphabet 为 ? Get(opts, "alphabet")。
  6. 如果 alphabetundefined,将 alphabet 设置为 "base64"
  7. 如果 alphabet 既不是 "base64" 也不是 "base64url",抛出 TypeError 异常。
  8. lastChunkHandling 为 ? Get(opts, "lastChunkHandling")。
  9. 如果 lastChunkHandlingundefined,将 lastChunkHandling 设置为 "loose"
  10. 如果 lastChunkHandling 不是 "loose""strict""stop-before-partial" 之一,抛出 TypeError 异常。
  11. taRecordMakeTypedArrayWithBufferWitnessRecord(into, seq-cst)。
  12. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
  13. byteLengthTypedArrayLength(taRecord)。
  14. resultFromBase64(string, alphabet, lastChunkHandling, byteLength)。
  15. bytesresult.[[Bytes]]
  16. writtenbytes 中元素的数量。
  17. 注:FromBase64 不调用任何用户代码,因此支持 into 的 ArrayBuffer 不可能已经被分离或缩小。
  18. 断言:writtenbyteLength
  19. 执行 SetUint8ArrayBytes(into, bytes)。
  20. 如果 result.[[Error]] 不是 none,则
    1. 抛出 result.[[Error]]
  21. resultObjectOrdinaryObjectCreate(%Object.prototype%)。
  22. 执行 ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]]))。
  23. 执行 ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written))。
  24. 返回 resultObject

23.3.2.2 Uint8Array.prototype.setFromHex ( string )

  1. intothis 值。
  2. 执行 ? ValidateUint8Array(into)。
  3. 如果 string 不是 String,抛出 TypeError 异常。
  4. taRecordMakeTypedArrayWithBufferWitnessRecord(into, seq-cst)。
  5. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
  6. byteLengthTypedArrayLength(taRecord)。
  7. resultFromHex(string, byteLength)。
  8. bytesresult.[[Bytes]]
  9. writtenbytes 中元素的数量。
  10. 注:FromHex 不调用任何用户代码,因此支持 into 的 ArrayBuffer 不可能已经被分离或缩小。
  11. 断言:writtenbyteLength
  12. 执行 SetUint8ArrayBytes(into, bytes)。
  13. 如果 result.[[Error]] 不是 none,则
    1. 抛出 result.[[Error]]
  14. resultObjectOrdinaryObjectCreate(%Object.prototype%)。
  15. 执行 ! CreateDataPropertyOrThrow(resultObject, "read", 𝔽(result.[[Read]]))。
  16. 执行 ! CreateDataPropertyOrThrow(resultObject, "written", 𝔽(written))。
  17. 返回 resultObject

23.3.2.3 Uint8Array.prototype.toBase64 ( [ options ] )

  1. objthis 值。
  2. 执行 ? ValidateUint8Array(obj)。
  3. opts 为 ? GetOptionsObject(options)。
  4. alphabet 为 ? Get(opts, "alphabet")。
  5. 如果 alphabetundefined,将 alphabet 设置为 "base64"
  6. 如果 alphabet 既不是 "base64" 也不是 "base64url",抛出 TypeError 异常。
  7. omitPaddingToBoolean(? Get(opts, "omitPadding")).
  8. toEncode 为 ? GetUint8ArrayBytes(obj)。
  9. 如果 alphabet"base64",则
    1. outAscii 为按照 RFC 4648 第 4 节中指定的 base64 编码对 toEncode 编码所得的码点序列。当且仅当 omitPaddingfalse 时包含填充。
  10. 否则,
    1. 断言:alphabet"base64url"
    2. outAscii 为按照 RFC 4648 第 5 节中指定的 base64url 编码对 toEncode 编码所得的码点序列。当且仅当 omitPaddingfalse 时包含填充。
  11. 返回 CodePointsToString(outAscii)。

23.3.2.4 Uint8Array.prototype.toHex ( )

  1. objthis 值。
  2. 执行 ? ValidateUint8Array(obj)。
  3. toEncode 为 ? GetUint8ArrayBytes(obj)。
  4. out 为空 String。
  5. 对于 toEncode 的每个字节 byte,执行
    1. hexNumber::toString(𝔽(byte), 16)。
    2. hex 设置为 StringPad(hex, 2, "0", start)。
    3. out 设置为 outhex 的字符串拼接。
  6. 返回 out

23.3.3 Uint8Array 对象的抽象操作

23.3.3.1 ValidateUint8Array ( ta )

The abstract operation ValidateUint8Array takes argument ta (一个 ECMAScript 语言值,) and returns 要么是一个包含 unused正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. 执行 ? RequireInternalSlot(ta, [[TypedArrayName]])。
  2. 如果 ta.[[TypedArrayName]] 不是 "Uint8Array",抛出 TypeError 异常。
  3. 返回 unused

23.3.3.2 GetUint8ArrayBytes ( ta )

The abstract operation GetUint8ArrayBytes takes argument ta (一个 Uint8Array,) and returns 要么是一个包含字节值 List正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. bufferta.[[ViewedArrayBuffer]]
  2. taRecordMakeTypedArrayWithBufferWitnessRecord(ta, seq-cst)。
  3. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,抛出 TypeError 异常。
  4. lenTypedArrayLength(taRecord)。
  5. byteOffsetta.[[ByteOffset]]
  6. bytes 为一个新的空 List
  7. index 为 0。
  8. 重复,当 index < len 时,
    1. byteIndexbyteOffset + index
    2. byte(GetValueFromBuffer(buffer, byteIndex, uint8, true, unordered))。
    3. byte 追加到 bytes
    4. index 设置为 index + 1。
  9. 返回 bytes

23.3.3.3 SetUint8ArrayBytes ( into, bytes )

The abstract operation SetUint8ArrayBytes takes arguments into (一个 Uint8Array,) and bytes (一个字节值 List,) and returns unused. It performs the following steps when called:

  1. offsetinto.[[ByteOffset]]
  2. lenbytes 中元素的数量。
  3. index 为 0。
  4. 重复,当 index < len 时,
    1. bytebytes[index]。
    2. byteIndexInBufferindex + offset
    3. 执行 SetValueInBuffer(into.[[ViewedArrayBuffer]], byteIndexInBuffer, uint8, 𝔽(byte), true, unordered)。
    4. index 设置为 index + 1。
  5. 返回 unused

23.3.3.4 SkipAsciiWhitespace ( string, index )

The abstract operation SkipAsciiWhitespace takes arguments string (一个 String,) and index (一个非负整数,) and returns 一个非负整数. It performs the following steps when called:

  1. lengthstring 的长度。
  2. 重复,当 index < length 时,
    1. charstring 中索引 index 处的码元。
    2. 如果 char 不是 0x0009 (TAB)、0x000A (LF)、0x000C (FF)、0x000D (CR) 或 0x0020 (SPACE) 之一,则
      1. 返回 index
    3. index 设置为 index + 1。
  3. 返回 index

23.3.3.5 DecodeFinalBase64Chunk ( chunk, throwOnExtraBits )

The abstract operation DecodeFinalBase64Chunk takes arguments chunk (一个长度为 2 或 3 的 String,) and throwOnExtraBits (一个 Boolean,) and returns 要么是一个包含字节值 List正常完成,要么是一个抛出完成. It performs the following steps when called:

  1. chunkLengthchunk 的长度。
  2. 如果 chunkLength = 2,则
    1. chunk 设置为 chunk"AA" 的字符串拼接。
  3. 否则,
    1. 断言:chunkLength 为 3。
    2. chunk 设置为 chunk"A" 的字符串拼接。
  4. bytesDecodeFullLengthBase64Chunk(chunk)。
  5. 如果 chunkLength = 2,则
    1. 如果 throwOnExtraBitstruebytes[1] ≠ 0,抛出 SyntaxError 异常。
    2. 返回 « bytes[0] »。
  6. 如果 throwOnExtraBitstruebytes[2] ≠ 0,抛出 SyntaxError 异常。
  7. 返回 « bytes[0], bytes[1] »。

23.3.3.6 DecodeFullLengthBase64Chunk ( chunk )

The abstract operation DecodeFullLengthBase64Chunk takes argument chunk (一个长度为 4 的 String,) and returns 一个长度为 3 的字节值 List.

标准 base64 字母表"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",即其元素为对应 Unicode Basic Latin 块中每个字母和数字以及 "+""/" 的码元的 String。

  1. byteSequence 为将 chunk 作为 base64 解码所得的唯一 3 字节序列(即对 byteSequence 应用 RFC 4648 第 4 节中指定的 base64 编码会产生 chunk 的那个序列)。
  2. 返回一个 List,其元素按顺序为 byteSequence 的元素。

23.3.3.7 FromBase64 ( string, alphabet, lastChunkHandling [ , maxLength ] )

The abstract operation FromBase64 takes arguments string (一个 String,), alphabet ("base64""base64url",), and lastChunkHandling ("loose""strict""stop-before-partial",) and optional argument maxLength (一个非负整数,) and returns 一个具有字段 [[Read]](一个整数)、[[Bytes]](一个字节值 List)和 [[Error]](一个 SyntaxError 对象或 none)的 Record. It performs the following steps when called:

  1. 如果 maxLength 不存在,则
    1. maxLength 设置为 253 - 1。
    2. 注:因为输入是 String,String 的长度限制为 253 - 1 个字符,并且输出所需字节数不超过输入字符数,所以永远不会达到此限制。然而,使用有限值在编辑上更方便。
  2. 注:下面算法中的验证和解码顺序是不可观察的。鼓励实现以最高效的任何顺序执行它们,也可以将验证与解码交错进行。
  3. 如果 maxLength = 0,则
    1. 返回 Record { [[Read]]: 0, [[Bytes]]: « », [[Error]]: none }。
  4. read 为 0。
  5. bytes 为一个新的空 List
  6. chunk 为空 String。
  7. chunkLength 为 0。
  8. index 为 0。
  9. lengthstring 的长度。
  10. 重复,
    1. index 设置为 SkipAsciiWhitespace(string, index)。
    2. 如果 index = length,则
      1. 如果 chunkLength > 0,则
        1. 如果 lastChunkHandling"stop-before-partial",则
          1. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }。
        2. 如果 lastChunkHandling"strict",则
          1. error 为一个新创建的 SyntaxError 对象。
          2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
        3. 断言:lastChunkHandling"loose"
        4. 如果 chunkLength = 1,则
          1. error 为一个新创建的 SyntaxError 对象。
          2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
        5. bytes 设置为 bytes 与 ! DecodeFinalBase64Chunk(chunk, false) 的列表拼接。
      2. 返回 Record { [[Read]]: length, [[Bytes]]: bytes, [[Error]]: none }。
    3. charstring 中从 indexindex + 1 的子字符串
    4. index 设置为 index + 1。
    5. 如果 char"=",则
      1. 如果 chunkLength < 2,则
        1. error 为一个新创建的 SyntaxError 对象。
        2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
      2. index 设置为 SkipAsciiWhitespace(string, index)。
      3. 如果 chunkLength = 2,则
        1. 如果 index = length,则
          1. 如果 lastChunkHandling"stop-before-partial",则
            1. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }。
          2. error 为一个新创建的 SyntaxError 对象。
          3. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
        2. char 设置为 string 中从 indexindex + 1 的子字符串
        3. 如果 char"=",则
          1. index 设置为 SkipAsciiWhitespace(string, index + 1)。
      4. 如果 index < length,则
        1. error 为一个新创建的 SyntaxError 对象。
        2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
      5. 如果 lastChunkHandling"strict",令 throwOnExtraBitstrue;否则令 throwOnExtraBitsfalse
      6. decodeResultCompletion(DecodeFinalBase64Chunk(chunk, throwOnExtraBits))。
      7. 如果 decodeResult 是一个 abrupt completion,则
        1. errordecodeResult.[[Value]]
        2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
      8. bytes 设置为 bytes 与 ! decodeResult 的列表拼接。
      9. 返回 Record { [[Read]]: length, [[Bytes]]: bytes, [[Error]]: none }。
    6. 如果 alphabet"base64url",则
      1. 如果 char"+""/",则
        1. error 为一个新创建的 SyntaxError 对象。
        2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
      2. 否则如果 char"-",则
        1. char 设置为 "+"
      3. 否则如果 char"_",则
        1. char 设置为 "/"
    7. 如果 char 的唯一码元不是标准 base64 字母表的元素,则
      1. error 为一个新创建的 SyntaxError 对象。
      2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
    8. remainingmaxLength - bytes 中元素的数量。
    9. 如果 remaining = 1 且 chunkLength = 2,或如果 remaining = 2 且 chunkLength = 3,则
      1. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }。
    10. chunk 设置为 chunkchar 的字符串拼接。
    11. chunkLength 设置为 chunk 的长度。
    12. 如果 chunkLength = 4,则
      1. bytes 设置为 bytesDecodeFullLengthBase64Chunk(chunk) 的列表拼接。
      2. chunk 设置为空 String。
      3. chunkLength 设置为 0。
      4. read 设置为 index
      5. 如果 bytes 中元素的数量 = maxLength,则
        1. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }。

23.3.3.8 FromHex ( string [ , maxLength ] )

The abstract operation FromHex takes argument string (一个 String,) and optional argument maxLength (一个非负整数,) and returns 一个具有字段 [[Read]](一个整数)、[[Bytes]](一个字节值 List)和 [[Error]](一个 SyntaxError 对象或 none)的 Record. It performs the following steps when called:

  1. 如果 maxLength 不存在,将 maxLength 设置为 253 - 1。
  2. lengthstring 的长度。
  3. bytes 为一个新的空 List
  4. read 为 0。
  5. 如果 length modulo 2 ≠ 0,则
    1. error 为一个新创建的 SyntaxError 对象。
    2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
  6. 重复,当 read < lengthbytes 中元素的数量 < maxLength 时,
    1. hexitsstring 中从 readread + 2 的子字符串
    2. 如果 hexits 包含任何不在 "0123456789abcdefABCDEF" 中的码元,则
      1. error 为一个新创建的 SyntaxError 对象。
      2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
    3. read 设置为 read + 2。
    4. bytehexits 以 16 进制表示法表示的整数值,其中使用字母 AFaf 表示值为 10 到 15 的数字。
    5. byte 追加到 bytes
  7. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }。