页面模式偏好

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 value 是 Array 异质对象。

23.1.1.1 Array ( ...values )

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

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

23.1.2 Array 构造函数的属性

Array 构造函数:

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

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

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

  1. 令 ctor 为 this value。
  2. 如果 mapper 是 undefined,则
    1. 令 mapping 为 false。
  3. 否则,
    1. 如果 IsCallable(mapper) 是 false,则抛出 TypeError 异常。
    2. 令 mapping 为 true。
  4. 令 usingIterator 为 ? GetMethod(items, %Symbol.iterator%)。
  5. 如果 usingIterator 不是 undefined,则
    1. 如果 IsConstructor(ctor) 是 true,则
      1. 令 array 为 ? Construct(ctor)。
    2. 否则,
      1. 令 array 为 ! ArrayCreate(0)。
    3. 令 iteratorRecord 为 ? GetIteratorFromMethod(items, usingIterator)。
    4. 令 k 为 0。
    5. 重复执行,
      1. 如果 k ≥ 253 - 1,则
        1. 令 error 为 ThrowCompletion(a newly created TypeError object)。
        2. 返回 ? IteratorClose(iteratorRecord, error)。
      2. 令 propertyKey 为 ! ToString(𝔽(k))。
      3. 令 next 为 ? IteratorStepValue(iteratorRecord)。
      4. 如果 next 是 done,则
        1. 执行 ? Set(array, "length", 𝔽(k), true)。
        2. 返回 array。
      5. 如果 mapping 是 true,则
        1. 令 mappedValue 为 Completion(Call(mapper, thisArg, « next, 𝔽(k) »))。
        2. IfAbruptCloseIterator(mappedValue, iteratorRecord)。
      6. 否则,
        1. 令 mappedValue 为 next。
      7. 令 defineStatus 为 Completion(CreateDataPropertyOrThrow(array, propertyKey, mappedValue))。
      8. IfAbruptCloseIterator(defineStatus, iteratorRecord)。
      9. 将 k 设置为 k + 1。
  6. 注:items 不可迭代,因此假定它是类数组对象。
  7. 令 arrayLike 为 ! ToObject(items)。
  8. 令 length 为 ? LengthOfArrayLike(arrayLike)。
  9. 如果 IsConstructor(ctor) 是 true,则
    1. 令 array 为 ? Construct(ctor, « 𝔽(length) »)。
  10. 否则,
    1. 令 array 为 ? ArrayCreate(length)。
  11. 令 k 为 0。
  12. 当 k < length 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 令 kValue 为 ? Get(arrayLike, propertyKey)。
    3. 如果 mapping 是 true,则
      1. 令 mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
    4. 否则,
      1. 令 mappedValue 为 kValue。
    5. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, mappedValue)。
    6. 将 k 设置为 k + 1。
  13. 执行 ? Set(array, "length", 𝔽(length), true)。
  14. 返回 array。
注

此方法有意设计为通用工厂方法;它不要求其 this value 是 Array 构造函数。因此,它可以被转移给或由任何其他可使用单个数值参数调用的构造函数继承。

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

调用时,此异步函数执行以下步骤:

  1. 令 ctor 为 this value。
  2. 令 mapping 为 false。
  3. 如果 mapper 不是 undefined,则
    1. 如果 IsCallable(mapper) 是 false,则抛出 TypeError 异常。
    2. 将 mapping 设置为 true。
  4. 令 iteratorRecord 为 undefined。
  5. 令 usingAsyncIterator 为 ? GetMethod(items, %Symbol.asyncIterator%)。
  6. 如果 usingAsyncIterator 是 undefined,则
    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(ctor) 是 true,则
      1. 令 array 为 ? Construct(ctor)。
    2. 否则,
      1. 令 array 为 ! ArrayCreate(0)。
    3. 令 k 为 0。
    4. 重复执行,
      1. 如果 k ≥ 253 - 1,则
        1. 令 error 为 ThrowCompletion(a newly created TypeError object)。
        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. 如果 done 是 true,则
        1. 执行 ? Set(array, "length", 𝔽(k), true)。
        2. 返回 array。
      8. 令 nextValue 为 ? IteratorValue(nextResult)。
      9. 如果 mapping 是 true,则
        1. 令 mappedValue 为 Completion(Call(mapper, thisArg, « nextValue, 𝔽(k) »))。
        2. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord)。
        3. 将 mappedValue 设置为 Completion(Await(mappedValue))。
        4. IfAbruptCloseAsyncIterator(mappedValue, iteratorRecord)。
      10. 否则,
        1. 令 mappedValue 为 nextValue。
      11. 令 defineStatus 为 Completion(CreateDataPropertyOrThrow(array, propertyKey, mappedValue))。
      12. IfAbruptCloseAsyncIterator(defineStatus, iteratorRecord)。
      13. 将 k 设置为 k + 1。
  9. 否则,
    1. 注:items 既不是异步可迭代对象,也不是可迭代对象,因此假定它是类数组对象。
    2. 令 arrayLike 为 ! ToObject(items)。
    3. 令 length 为 ? LengthOfArrayLike(arrayLike)。
    4. 如果 IsConstructor(ctor) 是 true,则
      1. 令 array 为 ? Construct(ctor, « 𝔽(length) »)。
    5. 否则,
      1. 令 array 为 ? ArrayCreate(length)。
    6. 令 k 为 0。
    7. 当 k < length 时,重复执行,
      1. 令 propertyKey 为 ! ToString(𝔽(k))。
      2. 令 kValue 为 ? Get(arrayLike, propertyKey)。
      3. 将 kValue 设置为 ? Await(kValue)。
      4. 如果 mapping 是 true,则
        1. 令 mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
        2. 将 mappedValue 设置为 ? Await(mappedValue)。
      5. 否则,
        1. 令 mappedValue 为 kValue。
      6. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, mappedValue)。
      7. 将 k 设置为 k + 1。
    8. 执行 ? Set(array, "length", 𝔽(length), true)。
    9. 返回 array。
注

此方法有意设计为通用工厂方法;它不要求其 this value 是 Array 构造函数。因此,它可以被转移给或由任何其他可使用单个数值参数调用的构造函数继承。

23.1.2.3 Array.isArray ( arg )

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

  1. 返回 ? IsArray(arg)。

23.1.2.4 Array.of ( ...items )

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

  1. 令 length 为 items 中的元素数量。
  2. 令 lengthNumber 为 𝔽(length)。
  3. 令 ctor 为 this value。
  4. 如果 IsConstructor(ctor) 是 true,则
    1. 令 array 为 ? Construct(ctor, « lengthNumber »)。
  5. 否则,
    1. 令 array 为 ? ArrayCreate(length)。
  6. 令 k 为 0。
  7. 当 k < length 时,重复执行,
    1. 令 kValue 为 items[k]。
    2. 令 propertyKey 为 ! ToString(𝔽(k))。
    3. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, kValue)。
    4. 将 k 设置为 k + 1。
  8. 执行 ? Set(array, "length", lengthNumber, true)。
  9. 返回 array。
注

此方法有意设计为通用工厂方法;它不要求其 this value 是 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 value。

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

注

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

23.1.3 Array 原型对象的属性

Array 原型对象:

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

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

23.1.3.1 Array.prototype.at ( index )

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 k 为 ? ToAbsoluteIndex(index, length)。
  4. 如果 k < 0 或 k ≥ length,则返回 undefined。
  5. 返回 ? 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. 如果 spreadable 是 true,则
      1. 令 length 为 ? LengthOfArrayLike(item)。
      2. 如果 nextIndex + length > 253 - 1,则抛出 TypeError 异常。
      3. 令 sourceIndex 为 0。
      4. 当 sourceIndex < length 时,重复执行,
        1. 令 propertyKey 为 ! ToString(𝔽(sourceIndex))。
        2. 令 exists 为 ? HasProperty(item, propertyKey)。
        3. 如果 exists 是 true,则
          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𝔽。

注 1

在步骤 6 中显式设置 "length" 属性,是为了确保当 items 中最后一个非空元素尾部存在空位,或者 array 不是内置 Array 时,长度仍然正确。

注 2

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

23.1.3.2.1 IsConcatSpreadable ( obj )

抽象操作 IsConcatSpreadable,接收参数 obj (ECMAScript 语言值);返回值:(正常完成 (值:Boolean) 或 抛出完成)。 调用时执行以下步骤:

  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 ] )

注 1

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

注 2

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

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 to 为 ? ToClampedIndex(target, length)。
  4. 令 from 为 ? ToClampedIndex(start, length)。
  5. 如果 end 是 undefined,则令 final 为 length;否则令 final 为 ? ToClampedIndex(end, length)。
  6. 令 count 为 min(final - from, length - to)。
  7. 如果 from < to 且 to < from + count,则
    1. 令 direction 为 -1。
    2. 将 from 设置为 from + count - 1。
    3. 将 to 设置为 to + count - 1。
  8. 否则,
    1. 令 direction 为 1。
  9. 当 count > 0 时,重复执行,
    1. 令 fromKey 为 ! ToString(𝔽(from))。
    2. 令 toKey 为 ! ToString(𝔽(to))。
    3. 令 fromPresent 为 ? HasProperty(obj, fromKey)。
    4. 如果 fromPresent 是 true,则
      1. 令 fromValue 为 ? Get(obj, fromKey)。
      2. 执行 ? Set(obj, toKey, fromValue, true)。
    5. 否则,
      1. 断言:fromPresent 是 false。
      2. 执行 ? DeletePropertyOrThrow(obj, toKey)。
    6. 将 from 设置为 from + direction。
    7. 将 to 设置为 to + direction。
    8. 将 count 设置为 count - 1。
  10. 返回 obj。
注 3

此方法有意设计为通用方法;它不要求其 this value 是 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 ] )

注 1

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

如果提供了 thisArg 参数,则每次调用 callback 时都会将其用作 this value。如果未提供,则改用 undefined。

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

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

every 处理的元素范围在第一次调用 callback 之前确定。调用 every 开始后追加到数组中的元素不会被 callback 访问。如果数组中已有元素发生变化,则传递给 callback 的值是在 every 访问该元素时的值;在调用 every 开始后且被访问之前删除的元素不会被访问。every 的作用类似于数学中的“对所有”量词。特别地,对于空数组,它返回 true。

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

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

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

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

注 1

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

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

注 2

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

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 k 为 ? ToClampedIndex(start, length)。
  4. 如果 end 是 undefined,则令 final 为 length;否则令 final 为 ? ToClampedIndex(end, length)。
  5. 当 k < final 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 执行 ? Set(obj, propertyKey, value, true)。
    3. 将 k 设置为 k + 1。
  6. 返回 obj。
注 3

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

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

注 1

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

如果提供了 thisArg 参数,则每次调用 callback 时都会将其用作 this value。如果未提供,则改用 undefined。

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

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

filter 处理的元素范围在第一次调用 callback 之前确定。调用 filter 开始后追加到数组中的元素不会被 callback 访问。如果数组中已有元素发生变化,则传递给 callback 的值是在 filter 访问该元素时的值;在调用 filter 开始后且被访问之前删除的元素不会被访问。

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

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

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

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

注 1

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

有关更多信息,请参见 FindViaPredicate。

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 findRecord 为 ? FindViaPredicate(obj, length, ascending, predicate, thisArg)。
  4. 返回 findRecord.[[Value]]。
注 2

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

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

注 1

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

有关更多信息,请参见 FindViaPredicate。

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 findRecord 为 ? FindViaPredicate(obj, length, ascending, predicate, thisArg)。
  4. 返回 findRecord.[[Index]]。
注 2

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

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

注 1

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

有关更多信息,请参见 FindViaPredicate。

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 findRecord 为 ? FindViaPredicate(obj, length, descending, predicate, thisArg)。
  4. 返回 findRecord.[[Value]]。
注 2

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

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

注 1

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

有关更多信息,请参见 FindViaPredicate。

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 findRecord 为 ? FindViaPredicate(obj, length, descending, predicate, thisArg)。
  4. 返回 findRecord.[[Index]]。
注 2

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

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

抽象操作 FindViaPredicate,接收参数 obj (Object)、length (非负整数)、direction ((ascending 或 descending))、predicate (ECMAScript 语言值) 和 thisArg (ECMAScript 语言值);返回值:(正常完成 (值:记录 (字段:[[Index]] (整数 Number), [[Value]] (ECMAScript 语言值))) 或 抛出完成)。

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

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

每次调用 predicate 时,thisArg 都将用作 this value。

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

所处理元素的范围在第一次调用 predicate 之前、遍历开始前确定。此后追加到数组中的元素不会被 predicate 访问。如果数组中已有元素发生变化,则传递给 predicate 的值是在此操作访问该元素时的值。在遍历开始后且被访问之前删除的元素仍然会被访问,并且要么从原型中查找,要么为 undefined。

调用时执行以下步骤:

  1. 如果 IsCallable(predicate) 是 false,则抛出 TypeError 异常。
  2. 如果 direction 是 ascending,则
    1. 令 indices 为从 0(包含)到 length(不包含)区间内按升序排列的整数 List。
  3. 否则,
    1. 令 indices 为从 0(包含)到 length(不包含)区间内按降序排列的整数 List。
  4. 对 indices 的每个整数 k,执行
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 注:如果 obj 是 TypedArray,则以下 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. 令 sourceLength 为 ? LengthOfArrayLike(obj)。
  3. 令 depthNumber 为 1。
  4. 如果 depth 不是 undefined,则
    1. 将 depthNumber 设置为 ? ToIntegerOrInfinity(depth)。
    2. 如果 depthNumber < 0,则将 depthNumber 设置为 0。
  5. 令 array 为 ? ArraySpeciesCreate(obj, 0)。
  6. 执行 ? FlattenIntoArray(array, obj, sourceLength, 0, depthNumber)。
  7. 返回 array。

23.1.3.13.1 FlattenIntoArray ( target, source, sourceLength, start, depth [ , mapperFunc [ , thisArg ] ] )

抽象操作 FlattenIntoArray,接收参数 target (Object)、source (Object)、sourceLength (非负整数)、start (非负整数) 和 depth ((非负整数 或 +∞))和可选参数 mapperFunc (函数对象) 和 thisArg (ECMAScript 语言值);返回值:(正常完成 (值:非负整数) 或 抛出完成)。 调用时执行以下步骤:

  1. 断言:如果存在 mapperFunc,则 IsCallable(mapperFunc) 是 true,存在 thisArg,并且 depth 是 1。
  2. 令 targetIndex 为 start。
  3. 令 sourceIndex 为 0。
  4. 当 sourceIndex < sourceLength 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(sourceIndex))。
    2. 令 exists 为 ? HasProperty(source, propertyKey)。
    3. 如果 exists 是 true,则
      1. 令 element 为 ? Get(source, propertyKey)。
      2. 如果存在 mapperFunc,则
        1. 将 element 设置为 ? Call(mapperFunc, thisArg, « element, 𝔽(sourceIndex), source »)。
      3. 令 shouldFlatten 为 false。
      4. 如果 depth > 0,则
        1. 将 shouldFlatten 设置为 ? IsArray(element)。
      5. 如果 shouldFlatten 是 true,则
        1. 如果 depth = +∞,则令 newDepth 为 +∞。
        2. 否则,令 newDepth 为 depth - 1。
        3. 令 elementLength 为 ? LengthOfArrayLike(element)。
        4. 将 targetIndex 设置为 ? FlattenIntoArray(target, element, elementLength, 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 ( mapperFunc [ , thisArg ] )

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

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

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

注 1

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

如果提供了 thisArg 参数,则每次调用 callback 时都会将其用作 this value。如果未提供,则改用 undefined。

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

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

forEach 处理的元素范围在第一次调用 callback 之前确定。调用 forEach 开始后追加到数组中的元素不会被 callback 访问。如果数组中已有元素发生变化,则传递给 callback 的值是在 forEach 访问该元素时的值;在调用 forEach 开始后且被访问之前删除的元素不会被访问。

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

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

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

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

注 1

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

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

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 如果 length = 0,则返回 false。
  4. 令 k 为 ? ToClampedIndex(fromIndex, length)。
  5. 当 k < length 时,重复执行,
    1. 令 elementK 为 ? Get(obj, ! ToString(𝔽(k)))。
    2. 如果 SameValueZero(searchElement, elementK) 是 true,则返回 true。
    3. 将 k 设置为 k + 1。
  6. 返回 false。
注 2

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

注 3

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

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

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

注 1

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

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 如果 length = 0,则返回 -1𝔽。
  4. 令 k 为 ? ToClampedIndex(fromIndex, length)。
  5. 当 k < length 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 令 kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresent 是 true,则
      1. 令 elementK 为 ? Get(obj, propertyKey)。
      2. 如果 IsStrictlyEqual(searchElement, elementK) 是 true,则返回 𝔽(k)。
    4. 将 k 设置为 k + 1。
  6. 返回 -1𝔽。
注 2

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

23.1.3.18 Array.prototype.join ( separator )

此方法将数组元素转换为 Strings,然后连接这些 Strings,并在它们之间插入 separator。如果未提供分隔符,则使用单个逗号作为分隔符。

调用时,它执行以下步骤:

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 如果 separator 是 undefined,则令 separatorString 为 ","。
  4. 否则,令 separatorString 为 ? ToString(separator)。
  5. 令 result 为空 String。
  6. 令 k 为 0。
  7. 当 k < length 时,重复执行,
    1. 如果 k > 0,则将 result 设置为 result 和 separatorString 的字符串连接。
    2. 令 element 为 ? Get(obj, ! ToString(𝔽(k)))。
    3. 如果 element 既不是 undefined 也不是 null,则
      1. 令 elementString 为 ? ToString(element)。
      2. 将 result 设置为 result 和 elementString 的字符串连接。
    4. 将 k 设置为 k + 1。
  8. 返回 result。
注

此方法有意设计为通用方法;它不要求其 this value 是 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 ] )

注 1

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

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

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 如果 length = 0,则返回 -1𝔽。
  4. 如果不存在 fromIndex,则令 k 为 length - 1;否则令 k 为 min(? ToAbsoluteIndex(fromIndex, length), length - 1)。
  5. 当 k ≥ 0 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 令 kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresent 是 true,则
      1. 令 elementK 为 ? Get(obj, propertyKey)。
      2. 如果 IsStrictlyEqual(searchElement, elementK) 是 true,则返回 𝔽(k)。
    4. 将 k 设置为 k - 1。
  6. 返回 -1𝔽。
注 2

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

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

注 1

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

如果提供了 thisArg 参数,则每次调用 callback 时都会将其用作 this value。如果未提供,则改用 undefined。

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

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

map 处理的元素范围在第一次调用 callback 之前确定。调用 map 开始后追加到数组中的元素不会被 callback 访问。如果数组中已有元素发生变化,则传递给 callback 的值是在 map 访问该元素时的值;在调用 map 开始后且被访问之前删除的元素不会被访问。

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

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

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

23.1.3.22 Array.prototype.pop ( )

注 1

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

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

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

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

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

注 1

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

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

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

此方法的 "length" 属性是 1𝔽。

注 2

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

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

注 1

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

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

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

reduce 处理的元素范围在第一次调用 callback 之前确定。调用 reduce 开始后追加到数组中的元素不会被 callback 访问。如果数组中已有元素发生变化,则传递给 callback 的值是在 reduce 访问该元素时的值;在调用 reduce 开始后且被访问之前删除的元素不会被访问。

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

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

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

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

注 1

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

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

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

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

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

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

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

23.1.3.26 Array.prototype.reverse ( )

注 1

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

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 middle 为 floor(length / 2)。
  4. 令 lower 为 0。
  5. 当 lower ≠ middle 时,重复执行,
    1. 令 upper 为 length - lower - 1。
    2. 令 upperP 为 ! ToString(𝔽(upper))。
    3. 令 lowerP 为 ! ToString(𝔽(lower))。
    4. 令 lowerExists 为 ? HasProperty(obj, lowerP)。
    5. 如果 lowerExists 是 true,则
      1. 令 lowerValue 为 ? Get(obj, lowerP)。
    6. 令 upperExists 为 ? HasProperty(obj, upperP)。
    7. 如果 upperExists 是 true,则
      1. 令 upperValue 为 ? Get(obj, upperP)。
    8. 如果 lowerExists 是 true 且 upperExists 是 true,则
      1. 执行 ? Set(obj, lowerP, upperValue, true)。
      2. 执行 ? Set(obj, upperP, lowerValue, true)。
    9. 否则如果 lowerExists 是 false 且 upperExists 是 true,则
      1. 执行 ? Set(obj, lowerP, upperValue, true)。
      2. 执行 ? DeletePropertyOrThrow(obj, upperP)。
    10. 否则如果 lowerExists 是 true 且 upperExists 是 false,则
      1. 执行 ? DeletePropertyOrThrow(obj, lowerP)。
      2. 执行 ? Set(obj, upperP, lowerValue, true)。
    11. 否则,
      1. 断言:lowerExists 和 upperExists 都是 false。
      2. 注:无需执行任何操作。
    12. 将 lower 设置为 lower + 1。
  6. 返回 obj。
注 2

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

23.1.3.27 Array.prototype.shift ( )

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

调用时,它执行以下步骤:

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

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

23.1.3.28 Array.prototype.slice ( start, end )

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

调用时,它执行以下步骤:

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 k 为 ? ToClampedIndex(start, length)。
  4. 如果 end 是 undefined,则令 final 为 length;否则令 final 为 ? ToClampedIndex(end, length)。
  5. 令 count 为 max(final - k, 0)。
  6. 令 array 为 ? ArraySpeciesCreate(obj, count)。
  7. 令 resultIndex 为 0。
  8. 当 k < final 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 令 kPresent 为 ? HasProperty(obj, propertyKey)。
    3. 如果 kPresent 是 true,则
      1. 令 kValue 为 ? Get(obj, propertyKey)。
      2. 执行 ? CreateDataPropertyOrThrow(array, ! ToString(𝔽(resultIndex)), kValue)。
    4. 将 k 设置为 k + 1。
    5. 将 resultIndex 设置为 resultIndex + 1。
  9. 执行 ? Set(array, "length", 𝔽(resultIndex), true)。
  10. 返回 array。
注 1

在步骤 9 中显式设置 "length" 属性,是为了确保即使 array 不是内置 Array,长度仍然正确。

注 2

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

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

注 1

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

如果提供了 thisArg 参数,则每次调用 callback 时都会将其用作 this value。如果未提供,则改用 undefined。

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

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

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

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

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

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

23.1.3.30 Array.prototype.sort ( comparator )

此方法对该数组的元素进行排序。如果 comparator 不是 undefined,它应当是一个接受两个参数 x 和 y 的函数;如果 x < y,返回负 Number;如果 x > y,返回正 Number;否则返回零。

调用时,它执行以下步骤:

  1. 如果 comparator 不是 undefined 且 IsCallable(comparator) 是 false,则抛出 TypeError 异常。
  2. 令 obj 为 ? ToObject(this value)。
  3. 令 length 为 ? LengthOfArrayLike(obj)。
  4. 令 sortCompare 为参数为 (x, y) 的新 Abstract Closure,它捕获 comparator,并在被调用时执行以下步骤:
    1. 返回 ? CompareArrayElements(x, y, comparator)。
  5. 令 sortedList 为 ? SortIndexedProperties(obj, length, sortCompare, skip-holes)。
  6. 令 itemCount 为 sortedList 中的元素数量。
  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 < length 时,重复执行,
    1. 执行 ? DeletePropertyOrThrow(obj, ! ToString(𝔽(j)))。
    2. 将 j 设置为 j + 1。
  11. 返回 obj。
注 1

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

注 2

步骤 5 和 6 中 ToString 抽象操作执行的方法调用可能导致 sortCompare 无法表现为一致比较器。

注 3

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

23.1.3.30.1 SortIndexedProperties ( obj, length, sortCompare, holes )

抽象操作 SortIndexedProperties,接收参数 obj (Object)、length (非负整数)、sortCompare (有两个参数的抽象闭包) 和 holes ((skip-holes 或 read-through-holes));返回值:(正常完成 (值:列表 (元素:ECMAScript 语言值)) 或 抛出完成)。 调用时执行以下步骤:

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

排序顺序是上述算法的步骤 4 完成后 items 的顺序。如果 sortCompare 对 items 的元素而言不是一致比较器,则排序顺序由实现定义。当 SortIndexedProperties 由 Array.prototype.sort 或 Array.prototype.toSorted 调用时,如果 comparator 是 undefined,并且对作为参数传递给 sortCompare 的某个特定值应用 ToString 时并非总是产生相同结果,则排序顺序也由实现定义。

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

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

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

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

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

上述条件是确保 comparator 将集合 values 划分为等价类,并使这些等价类具有全序关系的充要条件。

23.1.3.30.2 CompareArrayElements ( x, y, comparator )

抽象操作 CompareArrayElements,接收参数 x (ECMAScript 语言值)、y (ECMAScript 语言值) 和 comparator ((函数对象 或 undefined));返回值:(正常完成 (值:Number) 或 突兀完成)。 调用时执行以下步骤:

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

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

注 1

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

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 actualStart 为 ? ToClampedIndex(start, length)。
  4. 令 itemCount 为 items 中的元素数量。
  5. 如果不存在 start,则
    1. 令 actualDeleteCount 为 0。
  6. 否则如果不存在 deleteCount,则
    1. 令 actualDeleteCount 为 length - actualStart。
    2. 断言:actualDeleteCount ≥ 0。
  7. 否则,
    1. 令 dc 为 ? ToIntegerOrInfinity(deleteCount)。
    2. 令 actualDeleteCount 为将 dc 限制在 0 和 length - actualStart 之间的结果。
  8. 如果 length + itemCount - actualDeleteCount > 253 - 1,则抛出 TypeError 异常。
  9. 令 deletedArray 为 ? ArraySpeciesCreate(obj, actualDeleteCount)。
  10. 令 k 为 0。
  11. 当 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。
  12. 执行 ? Set(deletedArray, "length", 𝔽(actualDeleteCount), true)。
  13. 如果 itemCount < actualDeleteCount,则
    1. 将 k 设置为 actualStart。
    2. 当 k < (length - 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 设置为 length。
    4. 当 k > (length - actualDeleteCount + itemCount) 时,重复执行,
      1. 执行 ? DeletePropertyOrThrow(obj, ! ToString(𝔽(k - 1)))。
      2. 将 k 设置为 k - 1。
  14. 否则如果 itemCount > actualDeleteCount,则
    1. 将 k 设置为 (length - 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。
  15. 将 k 设置为 actualStart。
  16. 对 items 的每个元素 item,执行
    1. 执行 ? Set(obj, ! ToString(𝔽(k)), item, true)。
    2. 将 k 设置为 k + 1。
  17. 执行 ? Set(obj, "length", 𝔽(length - actualDeleteCount + itemCount), true)。
  18. 返回 deletedArray。
注 2

在步骤 12 和 17 中显式设置 "length" 属性,是为了确保即使这些对象不是内置 Array,长度仍然正确。

注 3

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

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

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

注 1

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

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

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

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

此方法使用数组元素各自的 toLocaleString 方法将它们转换为 Strings,然后连接这些 Strings,并在它们之间插入由实现定义、对区域设置敏感的分隔符 String。此方法类似于 toString,只是它旨在产生符合宿主环境当前区域设置惯例的区域设置敏感结果。

注 3

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

23.1.3.33 Array.prototype.toReversed ( )

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

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 array 为 ? ArrayCreate(length)。
  4. 令 k 为 0。
  5. 当 k < length 时,重复执行,
    1. 令 from 为 ! ToString(𝔽(length - 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 不是 undefined 且 IsCallable(comparator) 是 false,则抛出 TypeError 异常。
  2. 令 obj 为 ? ToObject(this value)。
  3. 令 length 为 ? LengthOfArrayLike(obj)。
  4. 令 array 为 ? ArrayCreate(length)。
  5. 令 sortCompare 为参数为 (x, y) 的新 Abstract Closure,它捕获 comparator,并在被调用时执行以下步骤:
    1. 返回 ? CompareArrayElements(x, y, comparator)。
  6. 令 sortedList 为 ? SortIndexedProperties(obj, length, sortCompare, read-through-holes)。
  7. 令 j 为 0。
  8. 当 j < length 时,重复执行,
    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. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 actualStart 为 ? ToClampedIndex(start, length)。
  4. 令 insertCount 为 items 中的元素数量。
  5. 令 maxSkipCount 为 length - actualStart。
  6. 如果不存在 start,则
    1. 令 actualSkipCount 为 0。
  7. 否则如果不存在 skipCount,则
    1. 令 actualSkipCount 为 maxSkipCount。
  8. 否则,
    1. 令 actualSkipCount 为将 ? ToIntegerOrInfinity(skipCount) 限制在 0 和 maxSkipCount 之间的结果。
  9. 令 newLength 为 length + insertCount - actualSkipCount。
  10. 断言:newLength ≥ 0。
  11. 如果 newLength > 253 - 1,则抛出 TypeError 异常。
  12. 令 newArray 为 ? ArrayCreate(newLength)。
  13. 令 writeIndex 为 0。
  14. 令 readIndex 为 actualStart + actualSkipCount。
  15. 当 writeIndex < actualStart 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(writeIndex))。
    2. 令 iValue 为 ? Get(obj, propertyKey)。
    3. 执行 ! CreateDataPropertyOrThrow(newArray, propertyKey, iValue)。
    4. 将 writeIndex 设置为 writeIndex + 1。
  16. 对 items 的每个元素 item,执行
    1. 令 propertyKey 为 ! ToString(𝔽(writeIndex))。
    2. 执行 ! CreateDataPropertyOrThrow(newArray, propertyKey, item)。
    3. 将 writeIndex 设置为 writeIndex + 1。
  17. 当 writeIndex < newLength 时,重复执行,
    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。
  18. 返回 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)。
注

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

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

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

调用时,它执行以下步骤:

  1. 令 obj 为 ? ToObject(this value)。
  2. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 argCount 为 items 中的元素数量。
  4. 如果 argCount > 0,则
    1. 如果 length + argCount > 253 - 1,则抛出 TypeError 异常。
    2. 令 k 为 length。
    3. 当 k > 0 时,重复执行,
      1. 令 from 为 ! ToString(𝔽(k - 1))。
      2. 令 to 为 ! ToString(𝔽(k + argCount - 1))。
      3. 令 fromPresent 为 ? HasProperty(obj, from)。
      4. 如果 fromPresent 是 true,则
        1. 令 fromValue 为 ? Get(obj, from)。
        2. 执行 ? Set(obj, to, fromValue, true)。
      5. 否则,
        1. 断言:fromPresent 是 false。
        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", 𝔽(length + argCount), true)。
  6. 返回 𝔽(length + argCount)。

此方法的 "length" 属性是 1𝔽。

注

此方法有意设计为通用方法;它不要求其 this value 是 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. 令 length 为 ? LengthOfArrayLike(obj)。
  3. 令 actualIndex 为 ? ToAbsoluteIndex(index, length)。
  4. 如果 actualIndex < 0 或 actualIndex ≥ length,则抛出 RangeError 异常。
  5. 令 array 为 ? ArrayCreate(length)。
  6. 令 k 为 0。
  7. 当 k < length 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 如果 k = actualIndex,则令 fromValue 为 value。
    3. 否则,令 fromValue 为 ? Get(obj, propertyKey)。
    4. 执行 ! CreateDataPropertyOrThrow(array, propertyKey, fromValue)。
    5. 将 k 设置为 k + 1。
  8. 返回 array。

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

%Symbol.iterator% 属性的初始值是 23.1.3.38 中定义的 %Array.prototype.values%。

23.1.3.41 Array.prototype [ %Symbol.unscopables% ]

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

  1. 令 unscopableList 为 OrdinaryObjectCreate(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 }。

注

此对象的自有属性名称,是在 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 }。

注

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

23.1.5 Array 迭代器对象

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

23.1.5.1 CreateArrayIterator ( array, kind )

抽象操作 CreateArrayIterator,接收参数 array (Object) 和 kind ((key+value、key 或 value));返回值:Object。 它用于为返回此类迭代器的 Array 方法创建迭代器对象。 调用时执行以下步骤:

  1. 令 iterator 为 OrdinaryObjectCreate(%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. 令 iteratorObj 为 this value。
  2. 如果 iteratorObj 不是 Object,则抛出 TypeError 异常。
  3. 如果 iteratorObj 不具有 Array Iterator Instance 的所有内部槽(23.1.5.3),则抛出 TypeError 异常。
  4. 令 array 为 iteratorObj.[[IteratedArrayLike]]。
  5. 如果 array 是 undefined,则返回 CreateIteratorResultObject(undefined, true)。
  6. 令 index 为 iteratorObj.[[ArrayLikeNextIndex]]。
  7. 令 kind 为 iteratorObj.[[ArrayLikeIterationKind]]。
  8. 如果 array 具有 [[TypedArrayName]] 内部槽,则
    1. 令 taRecord 为 ? ValidateTypedArrayBounds(array, seq-cst)。
    2. 令 length 为 TypedArrayLength(taRecord)。
  9. 否则,
    1. 令 length 为 ? LengthOfArrayLike(array)。
  10. 如果 index ≥ length,则
    1. 将 iteratorObj.[[IteratedArrayLike]] 设置为 undefined。
    2. 返回 CreateIteratorResultObject(undefined, true)。
  11. 将 iteratorObj.[[ArrayLikeNextIndex]] 设置为 index + 1。
  12. 令 indexNumber 为 𝔽(index)。
  13. 如果 kind 是 key,则
    1. 令 result 为 indexNumber。
  14. 否则,
    1. 令 elementKey 为 ! ToString(indexNumber)。
    2. 令 elementValue 为 ? Get(array, elementKey)。
    3. 如果 kind 是 value,则
      1. 令 result 为 elementValue。
    4. 否则,
      1. 断言:kind 是 key+value。
      2. 令 result 为 CreateArrayFromList(« indexNumber, 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 实例最初创建时具有 表 70 中列出的内部槽。

表 70: Array Iterator 实例的内部槽
内部槽 类型 描述
[[IteratedArrayLike]] an Object or undefined 正在被迭代的类数组对象。
[[ArrayLikeNextIndex]] a non-negative integer 此迭代器下一次要检查的元素的整数索引。
[[ArrayLikeIterationKind]] key+value, key, or value 标识迭代中每个元素返回内容的值。

23.2 TypedArray 对象

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

表 71: 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%。
  • 具有 "name" 属性,其值是 "TypedArray"。
  • 具有以下属性:

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

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

  1. 令 ctor 为 this value。
  2. 如果 IsConstructor(ctor) 是 false,则抛出 TypeError 异常。
  3. 如果 mapper 是 undefined,则
    1. 令 mapping 为 false。
  4. 否则,
    1. 如果 IsCallable(mapper) 是 false,则抛出 TypeError 异常。
    2. 令 mapping 为 true。
  5. 令 usingIterator 为 ? GetMethod(source, %Symbol.iterator%)。
  6. 如果 usingIterator 不是 undefined,则
    1. 令 values 为 ? IteratorToList(? GetIteratorFromMethod(source, usingIterator))。
    2. 令 length 为 values 中的元素数量。
    3. 令 targetObj 为 ? TypedArrayCreateFromConstructor(ctor, « 𝔽(length) »)。
    4. 令 k 为 0。
    5. 当 k < length 时,重复执行,
      1. 令 kValue 为 values[k]。
      2. 如果 mapping 是 true,则
        1. 令 mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
      3. 否则,
        1. 令 mappedValue 为 kValue。
      4. 执行 ? TypedArraySetElement(targetObj, 𝔽(k), mappedValue)。
      5. 将 k 设置为 k + 1。
    6. 返回 targetObj。
  7. 注:source 不是可迭代对象,因此假定它已经是类数组对象。
  8. 令 arrayLike 为 ! ToObject(source)。
  9. 令 length 为 ? LengthOfArrayLike(arrayLike)。
  10. 令 targetObj 为 ? TypedArrayCreateFromConstructor(ctor, « 𝔽(length) »)。
  11. 令 k 为 0。
  12. 当 k < length 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 令 kValue 为 ? Get(arrayLike, propertyKey)。
    3. 如果 mapping 是 true,则
      1. 令 mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
    4. 否则,
      1. 令 mappedValue 为 kValue。
    5. 执行 ? TypedArraySetElement(targetObj, 𝔽(k), mappedValue)。
    6. 将 k 设置为 k + 1。
  13. 返回 targetObj。

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

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

  1. 令 length 为 items 中的元素数量。
  2. 令 ctor 为 this value。
  3. 如果 IsConstructor(ctor) 是 false,则抛出 TypeError 异常。
  4. 令 newObj 为 ? TypedArrayCreateFromConstructor(ctor, « 𝔽(length) »)。
  5. 令 k 为 0。
  6. 当 k < length 时,重复执行,
    1. 令 kValue 为 items[k]。
    2. 执行 ? TypedArraySetElement(newObj, 𝔽(k), kValue)。
    3. 将 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 value。

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

注

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

23.2.3 %TypedArray% 原型对象的属性

%TypedArray% 原型对象:

  • 具有 [[Prototype]] 内部槽,其值是 %Object.prototype%。
  • 是 %TypedArray.prototype%。
  • 是普通对象。
  • 不具有 [[ViewedArrayBuffer]],也不具有任何其他 TypedArray 实例对象特有的内部槽。

23.2.3.1 %TypedArray%.prototype.at ( index )

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 k 为 ? ToAbsoluteIndex(index, length)。
  5. 如果 k < 0 或 k ≥ length,则返回 undefined。
  6. 返回 TypedArrayGetElement(obj, 𝔽(k))。

23.2.3.2 get %TypedArray%.prototype.buffer

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

  1. 令 obj 为 this value。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj 具有 [[ViewedArrayBuffer]] 内部槽。
  4. 令 buffer 为 obj.[[ViewedArrayBuffer]]。
  5. 返回 buffer。

23.2.3.3 get %TypedArray%.prototype.byteLength

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

  1. 令 obj 为 this value。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj 具有 [[ViewedArrayBuffer]] 内部槽。
  4. 令 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  5. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,则返回 +0𝔽。
  6. 令 size 为 TypedArrayByteLength(taRecord)。
  7. 返回 𝔽(size)。

23.2.3.4 get %TypedArray%.prototype.byteOffset

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

  1. 令 obj 为 this value。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj 具有 [[ViewedArrayBuffer]] 内部槽。
  4. 令 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  5. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,则返回 +0𝔽。
  6. 令 offset 为 obj.[[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. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 targetIndex 为 ? ToClampedIndex(target, length)。
  5. 令 startIndex 为 ? ToClampedIndex(start, length)。
  6. 如果 end 是 undefined,则令 endIndex 为 length;否则令 endIndex 为 ? ToClampedIndex(end, length)。
  7. 令 count 为 min(endIndex - startIndex, length - targetIndex)。
  8. 如果 count > 0,则
    1. 注:复制必须以保留源数据位级编码的方式执行。
    2. 令 buffer 为 obj.[[ViewedArrayBuffer]]。
    3. 将 taRecord 设置为 ? ValidateTypedArrayBounds(obj, seq-cst)。
    4. 将 length 设置为 TypedArrayLength(taRecord)。
    5. 注:上述步骤的副作用可能已经减小了 obj 的大小,在这种情况下,应使用仍然适用的最长前缀继续复制。
    6. 将 count 设置为 min(count, length - startIndex, length - targetIndex)。
    7. 令 elementSize 为 TypedArrayElementSize(obj)。
    8. 令 byteOffset 为 obj.[[ByteOffset]]。
    9. 令 toByteIndex 为 (targetIndex × elementSize) + byteOffset。
    10. 令 fromByteIndex 为 (startIndex × elementSize) + byteOffset。
    11. 令 countBytes 为 count × elementSize。
    12. 如果 fromByteIndex < toByteIndex 且 toByteIndex < fromByteIndex + countBytes,则
      1. 令 direction 为 -1。
      2. 将 fromByteIndex 设置为 fromByteIndex + countBytes - 1。
      3. 将 toByteIndex 设置为 toByteIndex + countBytes - 1。
    13. 否则,
      1. 令 direction 为 1。
    14. 当 countBytes > 0 时,重复执行,
      1. 断言:fromByteIndex ≥ 0 且 toByteIndex ≥ 0。
      2. 令 value 为 GetValueFromBuffer(buffer, fromByteIndex, uint8, true, unordered)。
      3. 执行 SetValueInBuffer(buffer, toByteIndex, uint8, value, true, unordered)。
      4. 将 fromByteIndex 设置为 fromByteIndex + direction。
      5. 将 toByteIndex 设置为 toByteIndex + direction。
      6. 将 countBytes 设置为 countBytes - 1。
  9. 返回 obj。

23.2.3.7 %TypedArray%.prototype.entries ( )

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

  1. 令 obj 为 this value。
  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. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,则抛出 TypeError 异常。
  5. 令 k 为 0。
  6. 当 k < length 时,重复执行,
    1. 令 kValue 为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 令 testResult 为 ToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
    3. 如果 testResult 是 false,则返回 false。
    4. 将 k 设置为 k + 1。
  7. 返回 true。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 obj.[[ContentType]] 是 bigint,则将 value 设置为 ? ToBigInt(value)。
  5. 否则,将 value 设置为 ? ToNumber(value)。
  6. 令 startIndex 为 ? ToClampedIndex(start, length)。
  7. 如果 end 是 undefined,则令 endIndex 为 length;否则令 endIndex 为 ? ToClampedIndex(end, length)。
  8. 将 taRecord 设置为 ? ValidateTypedArrayBounds(obj, seq-cst)。
  9. 将 length 设置为 TypedArrayLength(taRecord)。
  10. 将 endIndex 设置为 min(endIndex, length)。
  11. 令 k 为 startIndex。
  12. 当 k < endIndex 时,重复执行,
    1. 执行 ! TypedArraySetElement(obj, 𝔽(k), value)。
    2. 将 k 设置为 k + 1。
  13. 返回 obj。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,则抛出 TypeError 异常。
  5. 令 kept 为新的空 List。
  6. 令 captured 为 0。
  7. 令 k 为 0。
  8. 当 k < length 时,重复执行,
    1. 令 kValue 为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 令 selected 为 ToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
    3. 如果 selected 是 true,则
      1. 将 kValue 追加到 kept。
      2. 将 captured 设置为 captured + 1。
    4. 将 k 设置为 k + 1。
  9. 令 result 为 ? TypedArraySpeciesCreate(obj, « 𝔽(captured) »)。
  10. 令 n 为 0。
  11. 对 kept 的每个元素 element,执行
    1. 执行 ! TypedArraySetElement(result, 𝔽(n), element)。
    2. 将 n 设置为 n + 1。
  12. 返回 result。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 findRecord 为 ? FindViaPredicate(obj, length, ascending, predicate, thisArg)。
  5. 返回 findRecord.[[Value]]。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 findRecord 为 ? FindViaPredicate(obj, length, ascending, predicate, thisArg)。
  5. 返回 findRecord.[[Index]]。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 findRecord 为 ? FindViaPredicate(obj, length, descending, predicate, thisArg)。
  5. 返回 findRecord.[[Value]]。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 findRecord 为 ? FindViaPredicate(obj, length, descending, predicate, thisArg)。
  5. 返回 findRecord.[[Index]]。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,则抛出 TypeError 异常。
  5. 令 k 为 0。
  6. 当 k < length 时,重复执行,
    1. 令 kValue 为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 执行 ? Call(callback, thisArg, « kValue, 𝔽(k), obj »)。
    3. 将 k 设置为 k + 1。
  7. 返回 undefined。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 length = 0,则返回 false。
  5. 令 k 为 ? ToClampedIndex(fromIndex, length)。
  6. 当 k < length 时,重复执行,
    1. 令 elementK 为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 如果 SameValueZero(searchElement, elementK) 是 true,则返回 true。
    3. 将 k 设置为 k + 1。
  7. 返回 false。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 length = 0,则返回 -1𝔽。
  5. 令 k 为 ? ToClampedIndex(fromIndex, length)。
  6. 当 k < length 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 令 kPresent 为 ! HasProperty(obj, propertyKey)。
    3. 如果 kPresent 是 true,则
      1. 令 elementK 为 TypedArrayGetElement(obj, 𝔽(k))。
      2. 如果 IsStrictlyEqual(searchElement, elementK) 是 true,则返回 𝔽(k)。
    4. 将 k 设置为 k + 1。
  7. 返回 -1𝔽。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.18 %TypedArray%.prototype.join ( separator )

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 separator 是 undefined,则令 separatorString 为 ","。
  5. 否则,令 separatorString 为 ? ToString(separator)。
  6. 令 result 为空 String。
  7. 令 k 为 0。
  8. 当 k < length 时,重复执行,
    1. 如果 k > 0,则将 result 设置为 result 和 separatorString 的字符串连接。
    2. 令 element 为 TypedArrayGetElement(obj, 𝔽(k))。
    3. 如果 element 不是 undefined,则
      1. 令 elementString 为 ! ToString(element)。
      2. 将 result 设置为 result 和 elementString 的字符串连接。
    4. 将 k 设置为 k + 1。
  9. 返回 result。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.19 %TypedArray%.prototype.keys ( )

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

  1. 令 obj 为 this value。
  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. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 length = 0,则返回 -1𝔽。
  5. 如果不存在 fromIndex,则令 k 为 length - 1;否则令 k 为 min(? ToAbsoluteIndex(fromIndex, length), length - 1)。
  6. 当 k ≥ 0 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 令 kPresent 为 ! HasProperty(obj, propertyKey)。
    3. 如果 kPresent 是 true,则
      1. 令 elementK 为 TypedArrayGetElement(obj, 𝔽(k))。
      2. 如果 IsStrictlyEqual(searchElement, elementK) 是 true,则返回 𝔽(k)。
    4. 将 k 设置为 k - 1。
  7. 返回 -1𝔽。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.21 get %TypedArray%.prototype.length

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

  1. 令 obj 为 this value。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj 具有 [[ViewedArrayBuffer]] 和 [[ArrayLength]] 内部槽。
  4. 令 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  5. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,则返回 +0𝔽。
  6. 令 length 为 TypedArrayLength(taRecord)。
  7. 返回 𝔽(length)。

此函数不是通用函数。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,则抛出 TypeError 异常。
  5. 令 result 为 ? TypedArraySpeciesCreate(obj, « 𝔽(length) »)。
  6. 令 k 为 0。
  7. 当 k < length 时,重复执行,
    1. 令 kValue 为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 令 mappedValue 为 ? Call(callback, thisArg, « kValue, 𝔽(k), obj »)。
    3. 执行 ? TypedArraySetElement(result, 𝔽(k), mappedValue)。
    4. 将 k 设置为 k + 1。
  8. 返回 result。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,则抛出 TypeError 异常。
  5. 如果 length = 0 且不存在 initialValue,则抛出 TypeError 异常。
  6. 令 k 为 0。
  7. 令 accumulator 为 undefined。
  8. 如果存在 initialValue,则
    1. 将 accumulator 设置为 initialValue。
  9. 否则,
    1. 将 accumulator 设置为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 将 k 设置为 k + 1。
  10. 当 k < length 时,重复执行,
    1. 令 kValue 为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 将 accumulator 设置为 ? Call(callback, undefined, « accumulator, kValue, 𝔽(k), obj »)。
    3. 将 k 设置为 k + 1。
  11. 返回 accumulator。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,则抛出 TypeError 异常。
  5. 如果 length = 0 且不存在 initialValue,则抛出 TypeError 异常。
  6. 令 k 为 length - 1。
  7. 令 accumulator 为 undefined。
  8. 如果存在 initialValue,则
    1. 将 accumulator 设置为 initialValue。
  9. 否则,
    1. 将 accumulator 设置为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 将 k 设置为 k - 1。
  10. 当 k ≥ 0 时,重复执行,
    1. 令 kValue 为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 将 accumulator 设置为 ? Call(callback, undefined, « accumulator, kValue, 𝔽(k), obj »)。
    3. 将 k 设置为 k - 1。
  11. 返回 accumulator。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.25 %TypedArray%.prototype.reverse ( )

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 middle 为 floor(length / 2)。
  5. 令 lower 为 0。
  6. 当 lower ≠ middle 时,重复执行,
    1. 令 upper 为 length - lower - 1。
    2. 令 lowerValue 为 TypedArrayGetElement(obj, 𝔽(lower))。
    3. 令 upperValue 为 TypedArrayGetElement(obj, 𝔽(upper))。
    4. 执行 ! TypedArraySetElement(obj, 𝔽(lower), upperValue)。
    5. 执行 ! TypedArraySetElement(obj, 𝔽(upper), lowerValue)。
    6. 将 lower 设置为 lower + 1。
  7. 返回 obj。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

调用时,它执行以下步骤:

  1. 令 target 为 this value。
  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 value 必须是具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.26.1 SetTypedArrayFromArrayLike ( target, targetOffset, source )

抽象操作 SetTypedArrayFromArrayLike,接收参数 target (TypedArray)、targetOffset ((非负整数 或 +∞)) 和 source (an ECMAScript language value, but not a TypedArray);返回值:(正常完成 (值:unused) 或 抛出完成)。 它从 source 读取值,从索引 targetOffset 开始在 target 中设置多个值。 调用时执行以下步骤:

  1. 令 targetRecord 为 ? ValidateTypedArrayBounds(target, seq-cst)。
  2. 令 targetLength 为 TypedArrayLength(targetRecord)。
  3. 将 source 设置为 ? ToObject(source)。
  4. 令 sourceLength 为 ? LengthOfArrayLike(source)。
  5. 如果 targetOffset = +∞,则抛出 RangeError 异常。
  6. 如果 sourceLength + targetOffset > targetLength,则抛出 RangeError 异常。
  7. 令 k 为 0。
  8. 当 k < sourceLength 时,重复执行,
    1. 令 propertyKey 为 ! ToString(𝔽(k))。
    2. 令 value 为 ? Get(source, propertyKey)。
    3. 令 targetIndex 为 𝔽(targetOffset + k)。
    4. 执行 ? TypedArraySetElement(target, targetIndex, value)。
    5. 将 k 设置为 k + 1。
  9. 返回 unused。

23.2.3.26.2 SetTypedArrayFromTypedArray ( target, targetOffset, source )

抽象操作 SetTypedArrayFromTypedArray,接收参数 target (TypedArray)、targetOffset ((非负整数 或 +∞)) 和 source (TypedArray);返回值:(正常完成 (值:unused) 或 抛出完成)。 它从 source 读取值,从索引 targetOffset 开始在 target 中设置多个值。 调用时执行以下步骤:

  1. 令 targetBuffer 为 target.[[ViewedArrayBuffer]]。
  2. 令 targetRecord 为 ? ValidateTypedArrayBounds(target, seq-cst)。
  3. 令 targetLength 为 TypedArrayLength(targetRecord)。
  4. 令 sourceBuffer 为 source.[[ViewedArrayBuffer]]。
  5. 令 sourceRecord 为 ? ValidateTypedArrayBounds(source, seq-cst)。
  6. 令 sourceLength 为 TypedArrayLength(sourceRecord)。
  7. 令 targetType 为 TypedArrayElementType(target)。
  8. 令 targetElementSize 为 TypedArrayElementSize(target)。
  9. 令 targetByteOffset 为 target.[[ByteOffset]]。
  10. 令 sourceType 为 TypedArrayElementType(source)。
  11. 令 sourceElementSize 为 TypedArrayElementSize(source)。
  12. 令 sourceByteOffset 为 source.[[ByteOffset]]。
  13. 如果 targetOffset = +∞,则抛出 RangeError 异常。
  14. 如果 sourceLength + targetOffset > targetLength,则抛出 RangeError 异常。
  15. 如果 target.[[ContentType]] 不是 source.[[ContentType]],则抛出 TypeError 异常。
  16. 如果 IsSharedArrayBuffer(sourceBuffer) 是 true、IsSharedArrayBuffer(targetBuffer) 是 true,并且 sourceBuffer.[[ArrayBufferData]] 是 targetBuffer.[[ArrayBufferData]],则令 sameSharedArrayBuffer 为 true;否则令 sameSharedArrayBuffer 为 false。
  17. 如果 SameValue(sourceBuffer, targetBuffer) 是 true 或 sameSharedArrayBuffer 是 true,则
    1. 令 sourceByteLength 为 TypedArrayByteLength(sourceRecord)。
    2. 将 sourceBuffer 设置为 ? CloneArrayBuffer(sourceBuffer, sourceByteOffset, sourceByteLength)。
    3. 令 sourceByteIndex 为 0。
  18. 否则,
    1. 令 sourceByteIndex 为 sourceByteOffset。
  19. 令 targetByteIndex 为 (targetOffset × targetElementSize) + targetByteOffset。
  20. 令 limit 为 targetByteIndex + (targetElementSize × sourceLength)。
  21. 如果 sourceType 是 targetType,则
    1. 注:传输必须以保留源数据位级编码的方式执行。
    2. 当 targetByteIndex < limit 时,重复执行,
      1. 令 value 为 GetValueFromBuffer(sourceBuffer, sourceByteIndex, uint8, true, unordered)。
      2. 执行 SetValueInBuffer(targetBuffer, targetByteIndex, uint8, value, true, unordered)。
      3. 将 sourceByteIndex 设置为 sourceByteIndex + 1。
      4. 将 targetByteIndex 设置为 targetByteIndex + 1。
  22. 否则,
    1. 当 targetByteIndex < limit 时,重复执行,
      1. 令 value 为 GetValueFromBuffer(sourceBuffer, sourceByteIndex, sourceType, true, unordered)。
      2. 执行 SetValueInBuffer(targetBuffer, targetByteIndex, targetType, value, true, unordered)。
      3. 将 sourceByteIndex 设置为 sourceByteIndex + sourceElementSize。
      4. 将 targetByteIndex 设置为 targetByteIndex + targetElementSize。
  23. 返回 unused。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 sourceArrayLength 为 TypedArrayLength(taRecord)。
  4. 令 startIndex 为 ? ToClampedIndex(start, sourceArrayLength)。
  5. 如果 end 是 undefined,则令 endIndex 为 sourceArrayLength;否则令 endIndex 为 ? ToClampedIndex(end, sourceArrayLength)。
  6. 令 countBytes 为 max(endIndex - startIndex, 0)。
  7. 令 resultArray 为 ? TypedArraySpeciesCreate(obj, « 𝔽(countBytes) »)。
  8. 如果 countBytes > 0,则
    1. 将 taRecord 设置为 ? ValidateTypedArrayBounds(obj, seq-cst)。
    2. 将 endIndex 设置为 min(endIndex, TypedArrayLength(taRecord))。
    3. 将 countBytes 设置为 max(endIndex - startIndex, 0)。
    4. 令 sourceType 为 TypedArrayElementType(obj)。
    5. 令 targetType 为 TypedArrayElementType(resultArray)。
    6. 如果 sourceType 是 targetType,则
      1. 注:传输必须以保留源数据位级编码的方式执行。
      2. 令 sourceBuffer 为 obj.[[ViewedArrayBuffer]]。
      3. 令 targetBuffer 为 resultArray.[[ViewedArrayBuffer]]。
      4. 令 elementSize 为 TypedArrayElementSize(obj)。
      5. 令 sourceByteOffset 为 obj.[[ByteOffset]]。
      6. 令 sourceByteIndex 为 (startIndex × elementSize) + sourceByteOffset。
      7. 令 targetByteIndex 为 resultArray.[[ByteOffset]]。
      8. 令 endByteIndex 为 targetByteIndex + (countBytes × elementSize)。
      9. 当 targetByteIndex < endByteIndex 时,重复执行,
        1. 令 value 为 GetValueFromBuffer(sourceBuffer, sourceByteIndex, uint8, true, unordered)。
        2. 执行 SetValueInBuffer(targetBuffer, targetByteIndex, uint8, value, true, unordered)。
        3. 将 sourceByteIndex 设置为 sourceByteIndex + 1。
        4. 将 targetByteIndex 设置为 targetByteIndex + 1。
    7. 否则,
      1. 令 n 为 0。
      2. 令 k 为 startIndex。
      3. 当 k < endIndex 时,重复执行,
        1. 令 kValue 为 TypedArrayGetElement(obj, 𝔽(k))。
        2. 执行 ! TypedArraySetElement(resultArray, 𝔽(n), kValue)。
        3. 将 k 设置为 k + 1。
        4. 将 n 设置为 n + 1。
  9. 返回 resultArray。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 如果 IsCallable(callback) 是 false,则抛出 TypeError 异常。
  5. 令 k 为 0。
  6. 当 k < length 时,重复执行,
    1. 令 kValue 为 TypedArrayGetElement(obj, 𝔽(k))。
    2. 令 testResult 为 ToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
    3. 如果 testResult 是 true,则返回 true。
    4. 将 k 设置为 k + 1。
  7. 返回 false。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

23.2.3.29 %TypedArray%.prototype.sort ( comparator )

这是一个独立的方法,除下述内容外,它实现与 23.1.3.30 中定义的 Array.prototype.sort 相同的要求。实现可以利用 this value 是具有固定长度且整数索引属性不稀疏的对象这一事实来优化此方法。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

调用时,它执行以下步骤:

  1. 如果 comparator 不是 undefined 且 IsCallable(comparator) 是 false,则抛出 TypeError 异常。
  2. 令 obj 为 this value。
  3. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  4. 令 length 为 TypedArrayLength(taRecord)。
  5. 注:以下闭包执行数值比较,而不是 23.1.3.30 中使用的字符串比较。
  6. 令 sortCompare 为参数为 (x, y) 的新 Abstract Closure,它捕获 comparator,并在被调用时执行以下步骤:
    1. 返回 ? CompareTypedArrayElements(x, y, comparator)。
  7. 令 sortedList 为 ? SortIndexedProperties(obj, length, sortCompare, read-through-holes)。
  8. 令 j 为 0。
  9. 当 j < length 时,重复执行,
    1. 执行 ! TypedArraySetElement(obj, 𝔽(j), sortedList[j])。
    2. 将 j 设置为 j + 1。
  10. 返回 obj。
注

由于 NaN 始终比较为大于任何其他值(参见 CompareTypedArrayElements),因此在未提供 comparator 时,NaN 属性值总是排序到结果末尾。

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

此方法返回新的 TypedArray,其元素类型与此 TypedArray 的元素类型相同,其 ArrayBuffer 是此 TypedArray 的 ArrayBuffer,并引用从 start(包含)到 end(不包含)区间内的元素。如果 start 或 end 为负,则它表示从数组末尾而不是开头起算的索引。

调用时,它执行以下步骤:

  1. 令 obj 为 this value。
  2. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  3. 断言:obj 具有 [[ViewedArrayBuffer]] 内部槽。
  4. 令 buffer 为 obj.[[ViewedArrayBuffer]]。
  5. 令 sourceRecord 为 MakeTypedArrayWithBufferWitnessRecord(obj, seq-cst)。
  6. 如果 IsTypedArrayOutOfBounds(sourceRecord) 是 true,则
    1. 令 sourceLength 为 0。
  7. 否则,
    1. 令 sourceLength 为 TypedArrayLength(sourceRecord)。
  8. 令 startIndex 为 ? ToClampedIndex(start, sourceLength)。
  9. 令 elementSize 为 TypedArrayElementSize(obj)。
  10. 令 sourceByteOffset 为 obj.[[ByteOffset]]。
  11. 令 beginByteOffset 为 sourceByteOffset + (startIndex × elementSize)。
  12. 如果 obj.[[ArrayLength]] 是 auto 且 end 是 undefined,则
    1. 返回 ? TypedArraySpeciesCreate(obj, « buffer, 𝔽(beginByteOffset) »)。
  13. 如果 end 是 undefined,则令 endIndex 为 sourceLength;否则令 endIndex 为 ? ToClampedIndex(end, sourceLength)。
  14. 令 newLength 为 max(endIndex - startIndex, 0)。
  15. 返回 ? TypedArraySpeciesCreate(obj, « buffer, 𝔽(beginByteOffset), 𝔽(newLength) »)。

此方法不是通用方法。this value 必须是具有 [[TypedArrayName]] 内部槽的对象。

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

这是一个独立的方法,它实现与 23.1.3.32 中定义的 Array.prototype.toLocaleString 相同的算法,但使用 TypedArrayLength 替代对 "length" 执行 [[Get]]。当底层缓冲区不可调整大小时,可以利用 this value 具有固定长度且其整数索引属性不稀疏这一事实优化该算法的实现。不过,此类优化不得对算法规定的行为引入任何可观察的变化。

此方法不是通用方法。在求值该算法之前,会以 this value 和 seq-cst 作为参数调用 ValidateTypedArray。如果其结果是突然完成,则会抛出该异常,而不是求值该算法。

注

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

23.2.3.32 %TypedArray%.prototype.toReversed ( )

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 resultArray 为 ? TypedArrayCreateSameType(obj, length)。
  5. 令 k 为 0。
  6. 当 k < length 时,重复执行,
    1. 令 from 为 length - k - 1。
    2. 令 fromValue 为 TypedArrayGetElement(obj, 𝔽(from))。
    3. 执行 ! TypedArraySetElement(resultArray, 𝔽(k), fromValue)。
    4. 将 k 设置为 k + 1。
  7. 返回 resultArray。

23.2.3.33 %TypedArray%.prototype.toSorted ( comparator )

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

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

23.2.3.34 %TypedArray%.prototype.toString ( )

"toString" 属性的初始值是 23.1.3.36 中定义的 %Array.prototype.toString%。

23.2.3.35 %TypedArray%.prototype.values ( )

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

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

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

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

  1. 令 obj 为 this value。
  2. 令 taRecord 为 ? ValidateTypedArray(obj, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 actualIndex 为 ? ToAbsoluteIndex(index, length)。
  5. 如果 obj.[[ContentType]] 是 bigint,则令 numericValue 为 ? ToBigInt(value)。
  6. 否则,令 numericValue 为 ? ToNumber(value)。
  7. 如果 IsValidIntegerIndex(obj, 𝔽(actualIndex)) 是 false,则抛出 RangeError 异常。
  8. 令 resultArray 为 ? TypedArrayCreateSameType(obj, length)。
  9. 令 k 为 0。
  10. 当 k < length 时,重复执行,
    1. 如果 k = actualIndex,则令 fromValue 为 numericValue。
    2. 否则,令 fromValue 为 TypedArrayGetElement(obj, 𝔽(k))。
    3. 执行 ! TypedArraySetElement(resultArray, 𝔽(k), fromValue)。
    4. 将 k 设置为 k + 1。
  11. 返回 resultArray。

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

%Symbol.iterator% 属性的初始值是 23.2.3.35 中定义的 %TypedArray.prototype.values%。

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

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

  1. 令 obj 为 this value。
  2. 如果 obj 不是 Object,则返回 undefined。
  3. 如果 obj 不具有 [[TypedArrayName]] 内部槽,则返回 undefined。
  4. 令 name 为 obj.[[TypedArrayName]]。
  5. 断言:name 是 String。
  6. 返回 name。

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

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

23.2.4 TypedArray 对象的抽象操作

23.2.4.1 TypedArrayCreateFromConstructor ( ctor, argList )

抽象操作 TypedArrayCreateFromConstructor,接收参数 ctor (构造函数) 和 argList (列表 (元素:ECMAScript 语言值));返回值:(正常完成 (值:TypedArray) 或 抛出完成)。 它用于规定使用构造函数创建新的 TypedArray。 调用时执行以下步骤:

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

23.2.4.2 TypedArrayCreateSameType ( exemplar, length )

抽象操作 TypedArrayCreateSameType,接收参数 exemplar (TypedArray) 和 length (非负整数);返回值:(正常完成 (值:TypedArray) 或 抛出完成)。 它用于规定使用从 exemplar 派生的构造函数创建新的 TypedArray。与可以通过使用 %Symbol.species% 构造自定义 TypedArray 子类的 TypedArraySpeciesCreate 不同,此操作始终使用某个内置 TypedArray 构造函数。 调用时执行以下步骤:

  1. 令 ctor 为 表 71 中与构造函数名称 exemplar.[[TypedArrayName]] 关联的内在对象。
  2. 令 result 为 ? TypedArrayCreateFromConstructor(ctor, « 𝔽(length) »)。
  3. 断言:result 具有 [[TypedArrayName]] 和 [[ContentType]] 内部槽。
  4. 断言:result.[[ContentType]] 是 exemplar.[[ContentType]]。
  5. 返回 result。

23.2.4.3 TypedArraySpeciesCreate ( exemplar, argList )

抽象操作 TypedArraySpeciesCreate,接收参数 exemplar (TypedArray) 和 argList (列表 (元素:ECMAScript 语言值));返回值:(正常完成 (值:TypedArray) 或 抛出完成)。 它用于规定使用从 exemplar 派生的构造函数创建新的 TypedArray。与可以通过使用 %Symbol.species% 创建非 Array 对象的 ArraySpeciesCreate 不同,此操作强制要求构造函数创建实际的 TypedArray。 调用时执行以下步骤:

  1. 令 defaultCtor 为 表 71 中与构造函数名称 exemplar.[[TypedArrayName]] 关联的内在对象。
  2. 令 ctor 为 ? SpeciesConstructor(exemplar, defaultCtor)。
  3. 令 result 为 ? TypedArrayCreateFromConstructor(ctor, argList)。
  4. 如果 result.[[ContentType]] 不是 exemplar.[[ContentType]],则抛出 TypeError 异常。
  5. 返回 result。

23.2.4.4 ValidateTypedArray ( obj, order )

抽象操作 ValidateTypedArray,接收参数 obj (ECMAScript 语言值) 和 order ((seq-cst 或 unordered));返回值:(正常完成 (值:TypedArray With Buffer Witness 记录) 或 抛出完成)。 调用时执行以下步骤:

  1. 执行 ? RequireInternalSlot(obj, [[TypedArrayName]])。
  2. 断言:obj 具有 [[ViewedArrayBuffer]] 内部槽。
  3. 返回 ? ValidateTypedArrayBounds(obj, order)。

23.2.4.5 ValidateTypedArrayBounds ( ta, order )

抽象操作 ValidateTypedArrayBounds,接收参数 ta (TypedArray) 和 order ((seq-cst 或 unordered));返回值:(正常完成 (值:TypedArray With Buffer Witness 记录) 或 抛出完成)。 它验证 ta 是否未超出其后备缓冲区的边界。 调用时执行以下步骤:

  1. 令 taRecord 为 MakeTypedArrayWithBufferWitnessRecord(ta, order)。
  2. 如果 IsTypedArrayOutOfBounds(taRecord) 是 true,则抛出 TypeError 异常。
  3. 返回 taRecord。

23.2.4.6 TypedArrayElementSize ( obj )

抽象操作 TypedArrayElementSize,接收参数 obj (TypedArray);返回值:非负整数。 调用时执行以下步骤:

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

23.2.4.7 TypedArrayElementType ( obj )

抽象操作 TypedArrayElementType,接收参数 obj (TypedArray);返回值:TypedArray 元素类型。 调用时执行以下步骤:

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

23.2.4.8 CompareTypedArrayElements ( x, y, comparator )

抽象操作 CompareTypedArrayElements,接收参数 x ((Number 或 BigInt))、y ((Number 或 BigInt)) 和 comparator ((函数对象 或 undefined));返回值:(正常完成 (值:Number) 或 突兀完成)。 调用时执行以下步骤:

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

23.2.5 TypedArray 构造函数

每个 TypedArray 构造函数:

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

23.2.5.1 TypedArray ( ...args )

调用时,每个 TypedArray 构造函数执行以下步骤:

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

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

抽象操作 AllocateTypedArray,接收参数 ctorName (a String which is the name of a TypedArray constructor in 表 71)、newTarget (构造函数) 和 defaultProto (String)和可选参数 length (非负整数);返回值:(正常完成 (值:TypedArray) 或 抛出完成)。 它用于验证并创建 TypedArray 构造函数的实例。如果传入 length 参数,还会分配具有该长度的 ArrayBuffer,并将其与新的 TypedArray 实例关联。AllocateTypedArray 提供由 TypedArray 使用的通用语义。 调用时执行以下步骤:

  1. 令 proto 为 ? GetPrototypeFromConstructor(newTarget, defaultProto)。
  2. 令 obj 为 TypedArrayCreate(proto)。
  3. 断言:obj.[[ViewedArrayBuffer]] 是 undefined。
  4. 将 obj.[[TypedArrayName]] 设置为 ctorName。
  5. 如果 ctorName 是 "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, sourceArray )

抽象操作 InitializeTypedArrayFromTypedArray,接收参数 obj (TypedArray) 和 sourceArray (TypedArray);返回值:(正常完成 (值:unused) 或 抛出完成)。 调用时执行以下步骤:

  1. 令 sourceData 为 sourceArray.[[ViewedArrayBuffer]]。
  2. 令 elementType 为 TypedArrayElementType(obj)。
  3. 令 elementSize 为 TypedArrayElementSize(obj)。
  4. 令 sourceType 为 TypedArrayElementType(sourceArray)。
  5. 令 sourceElementSize 为 TypedArrayElementSize(sourceArray)。
  6. 令 sourceByteOffset 为 sourceArray.[[ByteOffset]]。
  7. 令 sourceRecord 为 ? ValidateTypedArrayBounds(sourceArray, seq-cst)。
  8. 令 elementLength 为 TypedArrayLength(sourceRecord)。
  9. 令 byteLength 为 elementSize × elementLength。
  10. 如果 elementType 是 sourceType,则
    1. 令 data 为 ? CloneArrayBuffer(sourceData, sourceByteOffset, byteLength)。
  11. 否则,
    1. 令 data 为 ? AllocateArrayBuffer(%ArrayBuffer%, byteLength)。
    2. 如果 sourceArray.[[ContentType]] 不是 obj.[[ContentType]],则抛出 TypeError 异常。
    3. 令 sourceByteIndex 为 sourceByteOffset。
    4. 令 targetByteIndex 为 0。
    5. 令 count 为 elementLength。
    6. 当 count > 0 时,重复执行,
      1. 令 value 为 GetValueFromBuffer(sourceData, sourceByteIndex, sourceType, true, unordered)。
      2. 执行 SetValueInBuffer(data, targetByteIndex, elementType, value, true, unordered)。
      3. 将 sourceByteIndex 设置为 sourceByteIndex + sourceElementSize。
      4. 将 targetByteIndex 设置为 targetByteIndex + elementSize。
      5. 将 count 设置为 count - 1。
  12. 将 obj.[[ViewedArrayBuffer]] 设置为 data。
  13. 将 obj.[[ByteLength]] 设置为 byteLength。
  14. 将 obj.[[ByteOffset]] 设置为 0。
  15. 将 obj.[[ArrayLength]] 设置为 elementLength。
  16. 返回 unused。

23.2.5.1.3 InitializeTypedArrayFromArrayBuffer ( obj, buffer, byteOffset, length )

抽象操作 InitializeTypedArrayFromArrayBuffer,接收参数 obj (TypedArray)、buffer ((ArrayBuffer 或 SharedArrayBuffer))、byteOffset (ECMAScript 语言值) 和 length (ECMAScript 语言值);返回值:(正常完成 (值:unused) 或 抛出完成)。 调用时执行以下步骤:

  1. 令 elementSize 为 TypedArrayElementSize(obj)。
  2. 令 offset 为 ? ToIndex(byteOffset)。
  3. 如果 offset modulo elementSize ≠ 0,则抛出 RangeError 异常。
  4. 令 bufferIsFixedLength 为 IsFixedLengthArrayBuffer(buffer)。
  5. 如果 length 不是 undefined,则
    1. 令 newLength 为 ? ToIndex(length)。
  6. 如果 IsDetachedBuffer(buffer) 是 true,则抛出 TypeError 异常。
  7. 令 bufferByteLength 为 ArrayBufferByteLength(buffer, seq-cst)。
  8. 如果 length 是 undefined 且 bufferIsFixedLength 是 false,则
    1. 如果 offset > bufferByteLength,则抛出 RangeError 异常。
    2. 将 obj.[[ByteLength]] 设置为 auto。
    3. 将 obj.[[ArrayLength]] 设置为 auto。
  9. 否则,
    1. 如果 length 是 undefined,则
      1. 如果 bufferByteLength modulo elementSize ≠ 0,则抛出 RangeError 异常。
      2. 令 newByteLength 为 bufferByteLength - offset。
      3. 如果 newByteLength < 0,则抛出 RangeError 异常。
    2. 否则,
      1. 令 newByteLength 为 newLength × 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 )

抽象操作 InitializeTypedArrayFromList,接收参数 obj (TypedArray) 和 values (列表 (元素:ECMAScript 语言值));返回值:(正常完成 (值:unused) 或 抛出完成)。 调用时执行以下步骤:

  1. 令 length 为 values 中的元素数量。
  2. 执行 ? AllocateTypedArrayBuffer(obj, length)。
  3. 令 k 为 0。
  4. 当 k < length 时,重复执行,
    1. 令 kValue 为 values[k]。
    2. 执行 ? TypedArraySetElement(obj, 𝔽(k), kValue)。
    3. 将 k 设置为 k + 1。
  5. 返回 unused。

23.2.5.1.5 InitializeTypedArrayFromArrayLike ( obj, arrayLike )

抽象操作 InitializeTypedArrayFromArrayLike,接收参数 obj (TypedArray) 和 arrayLike (an Object, but not a TypedArray or an ArrayBuffer);返回值:(正常完成 (值:unused) 或 抛出完成)。 调用时执行以下步骤:

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

23.2.5.1.6 AllocateTypedArrayBuffer ( obj, length )

抽象操作 AllocateTypedArrayBuffer,接收参数 obj (TypedArray) 和 length (非负整数);返回值:(正常完成 (值:unused) 或 抛出完成)。 它分配一个 ArrayBuffer 并将其与 obj 关联。 调用时执行以下步骤:

  1. 断言:obj.[[ViewedArrayBuffer]] 是 undefined。
  2. 令 elementSize 为 TypedArrayElementSize(obj)。
  3. 令 byteLength 为 elementSize × 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%。
  • 具有 "length" 属性,其值是 3𝔽。
  • 具有 "name" 属性,其值是 表 71 中为其指定的构造函数名称的 String 值。
  • 具有以下属性:

23.2.6.1 TypedArray.BYTES_PER_ELEMENT

TypedArray.BYTES_PER_ELEMENT 的值是 表 71 中为 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 的值是 表 71 中为 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. 令 optionsObj 为 ? GetOptionsObject(options)。
  3. 令 alphabet 为 ? Get(optionsObj, "alphabet")。
  4. 如果 alphabet 是 undefined,则将 alphabet 设置为 "base64"。
  5. 如果 alphabet 既不是 "base64" 也不是 "base64url",则抛出 TypeError 异常。
  6. 令 lastChunkHandling 为 ? Get(optionsObj, "lastChunkHandling")。
  7. 如果 lastChunkHandling 是 undefined,则将 lastChunkHandling 设置为 "loose"。
  8. 如果 lastChunkHandling 不是 "loose"、"strict" 或 "stop-before-partial" 之一,则抛出 TypeError 异常。
  9. 令 result 为 FromBase64(string, alphabet, lastChunkHandling)。
  10. 如果 result.[[Error]] 不是 none,则
    1. 抛出 result.[[Error]]。
  11. 令 resultLength 为 result.[[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. 令 result 为 FromHex(string)。
  3. 如果 result.[[Error]] 不是 none,则
    1. 抛出 result.[[Error]]。
  4. 令 resultLength 为 result.[[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. 令 into 为 this value。
  2. 执行 ? ValidateUint8Array(into)。
  3. 如果 string 不是 String,则抛出 TypeError 异常。
  4. 令 optionsObj 为 ? GetOptionsObject(options)。
  5. 令 alphabet 为 ? Get(optionsObj, "alphabet")。
  6. 如果 alphabet 是 undefined,则将 alphabet 设置为 "base64"。
  7. 如果 alphabet 既不是 "base64" 也不是 "base64url",则抛出 TypeError 异常。
  8. 令 lastChunkHandling 为 ? Get(optionsObj, "lastChunkHandling")。
  9. 如果 lastChunkHandling 是 undefined,则将 lastChunkHandling 设置为 "loose"。
  10. 如果 lastChunkHandling 不是 "loose"、"strict" 或 "stop-before-partial" 之一,则抛出 TypeError 异常。
  11. 令 taRecord 为 ? ValidateTypedArrayBounds(into, seq-cst)。
  12. 令 byteLength 为 TypedArrayLength(taRecord)。
  13. 令 result 为 FromBase64(string, alphabet, lastChunkHandling, byteLength)。
  14. 令 bytes 为 result.[[Bytes]]。
  15. 令 written 为 bytes 中的元素数量。
  16. 注:FromBase64 不会调用任何用户代码,因此作为 into 后备存储的 ArrayBuffer 不可能已被分离或缩小。
  17. 断言:written ≤ byteLength。
  18. 执行 SetUint8ArrayBytes(into, bytes)。
  19. 如果 result.[[Error]] 不是 none,则
    1. 抛出 result.[[Error]]。
  20. 令 resultObj 为 OrdinaryObjectCreate(%Object.prototype%)。
  21. 执行 ! CreateDataPropertyOrThrow(resultObj, "read", 𝔽(result.[[Read]]))。
  22. 执行 ! CreateDataPropertyOrThrow(resultObj, "written", 𝔽(written))。
  23. 返回 resultObj。

23.3.2.2 Uint8Array.prototype.setFromHex ( string )

  1. 令 into 为 this value。
  2. 执行 ? ValidateUint8Array(into)。
  3. 如果 string 不是 String,则抛出 TypeError 异常。
  4. 令 taRecord 为 ? ValidateTypedArrayBounds(into, seq-cst)。
  5. 令 byteLength 为 TypedArrayLength(taRecord)。
  6. 令 result 为 FromHex(string, byteLength)。
  7. 令 bytes 为 result.[[Bytes]]。
  8. 令 written 为 bytes 中的元素数量。
  9. 注:FromHex 不会调用任何用户代码,因此作为 into 后备存储的 ArrayBuffer 不可能已被分离或缩小。
  10. 断言:written ≤ byteLength。
  11. 执行 SetUint8ArrayBytes(into, bytes)。
  12. 如果 result.[[Error]] 不是 none,则
    1. 抛出 result.[[Error]]。
  13. 令 resultObj 为 OrdinaryObjectCreate(%Object.prototype%)。
  14. 执行 ! CreateDataPropertyOrThrow(resultObj, "read", 𝔽(result.[[Read]]))。
  15. 执行 ! CreateDataPropertyOrThrow(resultObj, "written", 𝔽(written))。
  16. 返回 resultObj。

23.3.2.3 Uint8Array.prototype.toBase64 ( [ options ] )

  1. 令 obj 为 this value。
  2. 执行 ? ValidateUint8Array(obj)。
  3. 令 optionsObj 为 ? GetOptionsObject(options)。
  4. 令 alphabet 为 ? Get(optionsObj, "alphabet")。
  5. 如果 alphabet 是 undefined,则将 alphabet 设置为 "base64"。
  6. 如果 alphabet 既不是 "base64" 也不是 "base64url",则抛出 TypeError 异常。
  7. 令 omitPadding 为 ToBoolean(? Get(optionsObj, "omitPadding")).
  8. 令 toEncode 为 ? GetUint8ArrayBytes(obj)。
  9. 如果 alphabet 是 "base64",则
    1. 令 outAscii 为根据 RFC 4648 第 4 节规定的 base64 编码对 toEncode 进行编码所得的码点序列。当且仅当 omitPadding 是 false 时包含填充。
  10. 否则,
    1. 断言:alphabet 是 "base64url"。
    2. 令 outAscii 为根据 RFC 4648 第 5 节规定的 base64url 编码对 toEncode 进行编码所得的码点序列。当且仅当 omitPadding 是 false 时包含填充。
  11. 返回 CodePointsToString(outAscii)。

23.3.2.4 Uint8Array.prototype.toHex ( )

  1. 令 obj 为 this value。
  2. 执行 ? ValidateUint8Array(obj)。
  3. 令 toEncode 为 ? GetUint8ArrayBytes(obj)。
  4. 令 out 为空 String。
  5. 对 toEncode 的每个字节 byte,执行
    1. 令 hex 为 Number::toString(𝔽(byte), 16)。
    2. 将 hex 设置为 StringPad(hex, 2, "0", start)。
    3. 将 out 设置为 out 和 hex 的字符串连接。
  6. 返回 out。

23.3.3 Uint8Array 对象的抽象操作

23.3.3.1 ValidateUint8Array ( ta )

抽象操作 ValidateUint8Array,接收参数 ta (ECMAScript 语言值);返回值:(正常完成 (值:unused) 或 抛出完成)。 调用时执行以下步骤:

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

23.3.3.2 GetUint8ArrayBytes ( ta )

抽象操作 GetUint8ArrayBytes,接收参数 ta (Uint8Array);返回值:(正常完成 (值:列表 (元素:字节值)) 或 抛出完成)。 调用时执行以下步骤:

  1. 令 buffer 为 ta.[[ViewedArrayBuffer]]。
  2. 令 taRecord 为 ? ValidateTypedArrayBounds(ta, seq-cst)。
  3. 令 length 为 TypedArrayLength(taRecord)。
  4. 令 byteOffset 为 ta.[[ByteOffset]]。
  5. 令 bytes 为新的空 List。
  6. 令 index 为 0。
  7. 当 index < length 时,重复执行,
    1. 令 byteIndex 为 byteOffset + index。
    2. 令 byte 为 ℝ(GetValueFromBuffer(buffer, byteIndex, uint8, true, unordered))。
    3. 将 byte 追加到 bytes。
    4. 将 index 设置为 index + 1。
  8. 返回 bytes。

23.3.3.3 SetUint8ArrayBytes ( into, bytes )

抽象操作 SetUint8ArrayBytes,接收参数 into (Uint8Array) 和 bytes (列表 (元素:字节值));返回值:unused。 调用时执行以下步骤:

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

23.3.3.4 SkipAsciiWhitespace ( string, index )

抽象操作 SkipAsciiWhitespace,接收参数 string (String) 和 index (非负整数);返回值:非负整数。 调用时执行以下步骤:

  1. 令 length 为 string 的长度。
  2. 当 index < length 时,重复执行,
    1. 令 char 为 string 中索引 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 )

抽象操作 DecodeFinalBase64Chunk,接收参数 chunk ((a String of length 2 或 3)) 和 throwOnExtraBits (Boolean);返回值:(正常完成 (值:列表 (元素:字节值)) 或 抛出完成)。 调用时执行以下步骤:

  1. 令 chunkLength 为 chunk 的长度。
  2. 如果 chunkLength = 2,则
    1. 将 chunk 设置为 chunk 和 "AA" 的字符串连接。
  3. 否则,
    1. 断言:chunkLength 是 3。
    2. 将 chunk 设置为 chunk 和 "A" 的字符串连接。
  4. 令 bytes 为 DecodeFullLengthBase64Chunk(chunk)。
  5. 如果 chunkLength = 2,则
    1. 如果 throwOnExtraBits 是 true 且 bytes[1] ≠ 0,则抛出 SyntaxError 异常。
    2. 返回 « bytes[0] »。
  6. 如果 throwOnExtraBits 是 true 且 bytes[2] ≠ 0,则抛出 SyntaxError 异常。
  7. 返回 « bytes[0], bytes[1] »。

23.3.3.6 DecodeFullLengthBase64Chunk ( chunk )

抽象操作 DecodeFullLengthBase64Chunk,接收参数 chunk (a String of length 4);返回值:列表 (元素:byte values of length 3)。

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

  1. 令 byteSequence 为将 chunk 作为 base64 解码所得的唯一 3 字节序列(即,将 RFC 4648 第 4 节规定的 base64 编码应用于 byteSequence 会产生 chunk 的序列)。
  2. 返回一个 List,其元素依次为 byteSequence 的各个元素。

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

抽象操作 FromBase64,接收参数 string (String)、alphabet (("base64" 或 "base64url")) 和 lastChunkHandling (("loose"、"strict" 或 "stop-before-partial"))和可选参数 maxLength (非负整数);返回值:记录 (字段:[[Read]] (整数), [[Bytes]] (列表 (元素:字节值)), [[Error]] ((a SyntaxError object 或 none)))。 调用时执行以下步骤:

  1. 如果不存在 maxLength,则
    1. 将 maxLength 设置为 253 - 1。
    2. 注:由于输入是 String,Strings 的长度限制为 253 - 1 个字符,并且输出所需的字节数不会超过输入的字符数,因此永远无法达到此限制。不过,为 maxLength 使用有限值在编辑上更方便。
  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. 令 length 为 string 的长度。
  10. 重复执行,
    1. 断言:bytes 中的元素数量可以被 3 整除。
    2. 将 index 设置为 SkipAsciiWhitespace(string, index)。
    3. 如果 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 }。
    4. 令 char 为 string 从 index 到 index + 1 的子字符串。
    5. 将 index 设置为 index + 1。
    6. 如果 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 从 index 到 index + 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",则令 throwOnExtraBits 为 true;否则令 throwOnExtraBits 为 false。
      6. 令 decodeResult 为 Completion(DecodeFinalBase64Chunk(chunk, throwOnExtraBits))。
      7. 如果 decodeResult 是突然完成,则
        1. 令 error 为 decodeResult.[[Value]]。
        2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
      8. 将 bytes 设置为 bytes 和 ! decodeResult 的列表连接。
      9. 返回 Record { [[Read]]: length, [[Bytes]]: bytes, [[Error]]: none }。
    7. 如果 alphabet 是 "base64url",则
      1. 如果 char 是 "+" 或 "/",则
        1. 令 error 为新创建的 SyntaxError 对象。
        2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
      2. 否则如果 char 是 "-",则
        1. 将 char 设置为 "+"。
      3. 否则如果 char 是 "_",则
        1. 将 char 设置为 "/"。
    8. 如果 char 的唯一一个码元不是标准 base64 字母表中的元素,则
      1. 令 error 为新创建的 SyntaxError 对象。
      2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
    9. 令 remaining 为 maxLength - bytes 中的元素数量。
    10. 如果 remaining = 1 且 chunkLength = 2,或者 remaining = 2 且 chunkLength = 3,则
      1. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }。
    11. 将 chunk 设置为 chunk 和 char 的字符串连接。
    12. 将 chunkLength 设置为 chunk 的长度。
    13. 如果 chunkLength = 4,则
      1. 将 bytes 设置为 bytes 和 DecodeFullLengthBase64Chunk(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 ] )

抽象操作 FromHex,接收参数 string (String)和可选参数 maxLength (非负整数);返回值:记录 (字段:[[Read]] (整数), [[Bytes]] (列表 (元素:字节值)), [[Error]] ((a SyntaxError object 或 none)))。 调用时执行以下步骤:

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