23 索引集合

23.1 Array 对象

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

23.1.1 Array 构造器

Array 构造器:

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

23.1.1.1 Array ( ...values )

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

  1. 如果 NewTarget 是 undefined,则令 newTarget活动函数对象;否则令 newTarget 为 NewTarget。
  2. proto 为 ? GetPrototypeFromConstructor(newTarget, "%Array.prototype%")。
  3. numberOfArgsvalues 中元素的数量。
  4. 如果 numberOfArgs = 0,则返回 ! ArrayCreate(0, proto)。
  5. 如果 numberOfArgs = 1,则
    1. lengthvalues[0]。
    2. array 为 ! ArrayCreate(0, proto)。
    3. 如果 length 不是 Number,则
      1. 执行 ! CreateDataPropertyOrThrow(array, "0", length)。
      2. intLength1𝔽
    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. itemKvalues[k]。
    3. 执行 ! CreateDataPropertyOrThrow(array, propertyKey, itemK)。
    4. k 设置为 k + 1。
  10. 断言:array"length" 属性的数学值numberOfArgs
  11. 返回 array

23.1.2 Array 构造器的属性

Array 构造器:

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

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

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

  1. ctorthis 值。
  2. 如果 mapperundefined,则
    1. mappingfalse
  3. 否则,
    1. 如果 IsCallable(mapper) 是 false,则抛出 TypeError 异常。
    2. mappingtrue
  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. errorThrowCompletion(一个新创建的 TypeError 对象)。
        2. 返回 ? IteratorClose(iteratorRecord, error)。
      2. propertyKey 为 ! ToString(𝔽(k))。
      3. next 为 ? IteratorStepValue(iteratorRecord)。
      4. 如果 nextdone,则
        1. 执行 ? Set(array, "length", 𝔽(k), true)。
        2. 返回 array
      5. 如果 mappingtrue,则
        1. mappedValueCompletion(Call(mapper, thisArg, « next, 𝔽(k) »))。
        2. IfAbruptCloseIterator(mappedValue, iteratorRecord)。
      6. 否则,
        1. mappedValuenext
      7. defineStatusCompletion(CreateDataPropertyOrThrow(array, propertyKey, mappedValue))。
      8. IfAbruptCloseIterator(defineStatus, iteratorRecord)。
      9. k 设置为 k + 1。
  6. 注:items 不可迭代,因此假定它是一个类数组对象
  7. arrayLike 为 ! ToObject(items)。
  8. 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. 如果 mappingtrue,则
      1. mappedValue 为 ? Call(mapper, thisArg, « kValue, 𝔽(k) »)。
    4. 否则,
      1. mappedValuekValue
    5. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, mappedValue)。
    6. k 设置为 k + 1。
  13. 执行 ? Set(array, "length", 𝔽(length), true)。
  14. 返回 array
Note

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

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

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

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

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

23.1.2.3 Array.isArray ( arg )

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

  1. 返回 ? IsArray(arg)。

23.1.2.4 Array.of ( ...items )

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

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

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

23.1.2.5 Array.prototype

Array.prototype 的值是 Array 原型对象

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

23.1.2.6 get Array [ %Symbol.species% ]

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

  1. 返回 this 值。

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

Note

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

23.1.3 Array 原型对象的属性

Array 原型对象

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

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

23.1.3.1 Array.prototype.at ( index )

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

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

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

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

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

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

Note 1

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

Note 2

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

23.1.3.2.1 IsConcatSpreadable ( obj )

The abstract operation IsConcatSpreadable takes argument obj (an ECMAScript language value) and returns either a normal completion containing a Boolean or a throw completion. It performs the following steps when called:

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

23.1.3.3 Array.prototype.constructor

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

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

Note 1

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

Note 2

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

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

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

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

23.1.3.5 Array.prototype.entries ( )

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

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

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

Note 1

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

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

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

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

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

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

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

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

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

Note 1

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

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

Note 2

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

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

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

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

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

Note 1

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

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

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

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

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

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

  1. obj 为 ? ToObject(this value)。
  2. 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. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. selectedToBoolean(? Call(callback, thisArg, « kValue, 𝔽(k), obj »))。
      3. 如果 selectedtrue,则
        1. 执行 ? CreateDataPropertyOrThrow(array, ! ToString(𝔽(to)), kValue)。
        2. to 设置为 to + 1。
    4. k 设置为 k + 1。
  8. 返回 array
Note 2

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

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

Note 1

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

更多信息见 FindViaPredicate

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

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

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

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

Note 1

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

更多信息见 FindViaPredicate

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

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

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

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

Note 1

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

更多信息见 FindViaPredicate

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

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

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

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

Note 1

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

更多信息见 FindViaPredicate

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

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

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

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

The abstract operation FindViaPredicate takes arguments obj (an Object), length (a non-negative integer), direction (ascending or descending), predicate (an ECMAScript language value), and thisArg (an ECMAScript language value) and returns either a normal completion containing a Record with fields [[Index]] (an integral Number) and [[Value]] (an ECMAScript language value) or a throw completion.

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

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

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

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

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

It performs the following steps when called:

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

23.1.3.13 Array.prototype.flat ( [ depth ] )

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

  1. obj 为 ? ToObject(this value)。
  2. 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 ] ] )

The abstract operation FlattenIntoArray takes arguments target (an Object), source (an Object), sourceLength (a non-negative integer), start (a non-negative integer), and depth (a non-negative integer or +∞) and optional arguments mapperFunc (a function object) and thisArg (an ECMAScript language value) and returns either a normal completion containing a non-negative integer or a throw completion. It performs the following steps when called:

  1. 断言:如果 mapperFunc 存在,则 IsCallable(mapperFunc) 是 truethisArg 存在,且 depth 是 1。
  2. targetIndexstart
  3. sourceIndex+0𝔽
  4. 重复,只要 (sourceIndex) < sourceLength
    1. propertyKey 为 ! ToString(sourceIndex)。
    2. exists 为 ? HasProperty(source, propertyKey)。
    3. 如果 existstrue,则
      1. element 为 ? Get(source, propertyKey)。
      2. 如果 mapperFunc 存在,则
        1. element 设置为 ? Call(mapperFunc, thisArg, « element, sourceIndex, source »)。
      3. shouldFlattenfalse
      4. 如果 depth > 0,则
        1. shouldFlatten 设置为 ? IsArray(element)。
      5. 如果 shouldFlattentrue,则
        1. 如果 depth = +∞,则令 newDepth 为 +∞。
        2. 否则,令 newDepthdepth - 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 ] )

Note 1

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

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

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

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

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

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

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

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

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

Note 1

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

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

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

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

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

Note 3

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

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

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

Note 1

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

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

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

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

23.1.3.18 Array.prototype.join ( separator )

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

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

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

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

23.1.3.19 Array.prototype.keys ( )

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

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

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

Note 1

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

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

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

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

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

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

Note 1

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

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

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

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

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

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

  1. obj 为 ? ToObject(this value)。
  2. 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. 如果 kPresenttrue,则
      1. kValue 为 ? Get(obj, propertyKey)。
      2. mappedValue 为 ? Call(callback, thisArg, « kValue, 𝔽(k), obj »)。
      3. 执行 ? CreateDataPropertyOrThrow(array, propertyKey, mappedValue)。
    4. k 设置为 k + 1。
  7. 返回 array
Note 2

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

23.1.3.22 Array.prototype.pop ( )

Note 1

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

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

  1. obj 为 ? ToObject(this value)。
  2. 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
Note 2

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

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

Note 1

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

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

  1. obj 为 ? ToObject(this value)。
  2. length 为 ? LengthOfArrayLike(obj)。
  3. argCountitems 中元素的数量。
  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𝔽

Note 2

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

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

Note 1

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

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

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

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

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

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

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

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

Note 1

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

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

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

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

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

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

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

23.1.3.26 Array.prototype.reverse ( )

Note 1

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

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

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

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

23.1.3.27 Array.prototype.shift ( )

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

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

  1. obj 为 ? ToObject(this value)。
  2. 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. 如果 fromPresenttrue,则
      1. fromValue 为 ? Get(obj, from)。
      2. 执行 ? Set(obj, to, fromValue, true)。
    5. 否则,
      1. 断言:fromPresentfalse
      2. 执行 ? DeletePropertyOrThrow(obj, to)。
    6. k 设置为 k + 1。
  7. 执行 ? DeletePropertyOrThrow(obj, ! ToString(𝔽(length - 1)))。
  8. 执行 ? Set(obj, "length", 𝔽(length - 1), true)。
  9. 返回 first
Note

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

23.1.3.28 Array.prototype.slice ( start, end )

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

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

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

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

Note 2

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

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

Note 1

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

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

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

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

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

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

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

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

23.1.3.30 Array.prototype.sort ( comparator )

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

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

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

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

Note 2

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

Note 3

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

23.1.3.30.1 SortIndexedProperties ( obj, length, sortCompare, holes )

The abstract operation SortIndexedProperties takes arguments obj (an Object), length (a non-negative integer), sortCompare (an Abstract Closure with two parameters), and holes (skip-holes or read-through-holes) and returns either a normal completion containing a List of ECMAScript language values or a throw completion. It performs the following steps when called:

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

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

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

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

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

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

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

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

23.1.3.30.2 CompareArrayElements ( x, y, comparator )

The abstract operation CompareArrayElements takes arguments x (an ECMAScript language value), y (an ECMAScript language value), and comparator (a function object or undefined) and returns either a normal completion containing a Number or an abrupt completion. It performs the following steps when called:

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

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

Note 1

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

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

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

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

Note 3

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

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

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

Note 1

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

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

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

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

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

Note 3

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

23.1.3.33 Array.prototype.toReversed ( )

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

  1. obj 为 ? ToObject(this value)。
  2. 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 不是 undefinedIsCallable(comparator) 是 false,则抛出 TypeError 异常。
  2. obj 为 ? ToObject(this value)。
  3. length 为 ? LengthOfArrayLike(obj)。
  4. array 为 ? ArrayCreate(length)。
  5. sortCompare 为一个新的 Abstract Closure,其参数为 (x, y),捕获 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. relativeStart 为 ? ToIntegerOrInfinity(start)。
  4. 如果 relativeStart = -∞,则令 actualStart 为 0。
  5. 否则,如果 relativeStart < 0,则令 actualStartmax(length + relativeStart, 0)。
  6. 否则,令 actualStartmin(relativeStart, length)。
  7. insertCountitems 中元素的数量。
  8. 如果 start 不存在,则
    1. actualSkipCount 为 0。
  9. 否则,如果 skipCount 不存在,则
    1. actualSkipCountlength - actualStart
  10. 否则,
    1. sc 为 ? ToIntegerOrInfinity(skipCount)。
    2. actualSkipCount 为将 sc 夹在 0 与 length - actualStart 之间的结果。
  11. newLengthlength + insertCount - actualSkipCount
  12. 如果 newLength > 253 - 1,则抛出 TypeError 异常。
  13. newArray 为 ? ArrayCreate(newLength)。
  14. writeIndex 为 0。
  15. readIndexactualStart + actualSkipCount
  16. 重复,只要 writeIndex < actualStart
    1. propertyKey 为 ! ToString(𝔽(writeIndex))。
    2. iValue 为 ? Get(obj, propertyKey)。
    3. 执行 ! CreateDataPropertyOrThrow(newArray, propertyKey, iValue)。
    4. writeIndex 设置为 writeIndex + 1。
  17. items 的每个元素 item,执行:
    1. propertyKey 为 ! ToString(𝔽(writeIndex))。
    2. 执行 ! CreateDataPropertyOrThrow(newArray, propertyKey, item)。
    3. writeIndex 设置为 writeIndex + 1。
  18. 重复,只要 writeIndex < 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。
  19. 返回 newArray

23.1.3.36 Array.prototype.toString ( )

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

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

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

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

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

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

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

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

Note

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

23.1.3.38 Array.prototype.values ( )

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

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

23.1.3.39 Array.prototype.with ( index, value )

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

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

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

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

23.1.3.41 Array.prototype [ %Symbol.unscopables% ]

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

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

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

Note

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

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

23.1.4 Array 实例的属性

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

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

23.1.4.1 length

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

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

Note

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

23.1.5 Array Iterator 对象

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

23.1.5.1 CreateArrayIterator ( array, kind )

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

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

23.1.5.2 %ArrayIteratorPrototype% 对象

%ArrayIteratorPrototype% 对象:

23.1.5.2.1 %ArrayIteratorPrototype%.next ( )

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

23.1.5.2.2 %ArrayIteratorPrototype% [ %Symbol.toStringTag% ]

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

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

23.1.5.3 Array Iterator 实例的属性

Array Iterator 实例是普通对象,继承自 %ArrayIteratorPrototype% 内置对象的属性。Array Iterator 实例最初以 Table 70 中列出的内部槽创建。

Table 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 构造器,列于 Table 71 中。Table 71 中的每个构造器都有一个对应的不同原型对象。

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

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

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

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

23.2.2.3 %TypedArray%.prototype

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

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

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

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

  1. 返回 this 值。

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

Note

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

23.2.3 %TypedArray% 原型对象的属性

%TypedArray% 原型对象

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

23.2.3.1 %TypedArray%.prototype.at ( index )

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

23.2.3.2 get %TypedArray%.prototype.buffer

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

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

23.2.3.3 get %TypedArray%.prototype.byteLength

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

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

23.2.3.4 get %TypedArray%.prototype.byteOffset

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

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

23.2.3.5 %TypedArray%.prototype.constructor

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

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

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

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

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

23.2.3.7 %TypedArray%.prototype.entries ( )

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

23.2.3.18 %TypedArray%.prototype.join ( separator )

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

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

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

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

23.2.3.19 %TypedArray%.prototype.keys ( )

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

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

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

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

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

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

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

23.2.3.21 get %TypedArray%.prototype.length

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

23.2.3.25 %TypedArray%.prototype.reverse ( )

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

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

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

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

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

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

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

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

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

23.2.3.26.1 SetTypedArrayFromArrayLike ( target, targetOffset, source )

The abstract operation SetTypedArrayFromArrayLike takes arguments target (a TypedArray), targetOffset (a non-negative integer or +∞), and source (an ECMAScript language value, but not a TypedArray) and returns either a normal completion containing unused or a throw completion. 它从 source 读取值,并从索引 targetOffset 开始在 target 中设置多个值。 It performs the following steps when called:

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

23.2.3.26.2 SetTypedArrayFromTypedArray ( target, targetOffset, source )

The abstract operation SetTypedArrayFromTypedArray takes arguments target (a TypedArray), targetOffset (a non-negative integer or +∞), and source (a TypedArray) and returns either a normal completion containing unused or a throw completion. 它从 source 读取值,并从索引 targetOffset 开始在 target 中设置多个值。 It performs the following steps when called:

  1. targetBuffertarget.[[ViewedArrayBuffer]]
  2. targetRecordMakeTypedArrayWithBufferWitnessRecord(target, seq-cst)。
  3. 如果 IsTypedArrayOutOfBounds(targetRecord) 是 true,则抛出 TypeError 异常。
  4. targetLengthTypedArrayLength(targetRecord)。
  5. sourceBuffersource.[[ViewedArrayBuffer]]
  6. sourceRecordMakeTypedArrayWithBufferWitnessRecord(source, seq-cst)。
  7. 如果 IsTypedArrayOutOfBounds(sourceRecord) 是 true,则抛出 TypeError 异常。
  8. sourceLengthTypedArrayLength(sourceRecord)。
  9. targetTypeTypedArrayElementType(target)。
  10. targetElementSizeTypedArrayElementSize(target)。
  11. targetByteOffsettarget.[[ByteOffset]]
  12. sourceTypeTypedArrayElementType(source)。
  13. sourceElementSizeTypedArrayElementSize(source)。
  14. sourceByteOffsetsource.[[ByteOffset]]
  15. 如果 targetOffset = +∞,则抛出 RangeError 异常。
  16. 如果 sourceLength + targetOffset > targetLength,则抛出 RangeError 异常。
  17. 如果 target.[[ContentType]] 不是 source.[[ContentType]],则抛出 TypeError 异常。
  18. 如果 IsSharedArrayBuffer(sourceBuffer) 是 trueIsSharedArrayBuffer(targetBuffer) 是 true,且 sourceBuffer.[[ArrayBufferData]]targetBuffer.[[ArrayBufferData]],则令 sameSharedArrayBuffertrue;否则令 sameSharedArrayBufferfalse
  19. 如果 SameValue(sourceBuffer, targetBuffer) 是 truesameSharedArrayBuffertrue,则
    1. sourceByteLengthTypedArrayByteLength(sourceRecord)。
    2. sourceBuffer 设置为 ? CloneArrayBuffer(sourceBuffer, sourceByteOffset, sourceByteLength)。
    3. sourceByteIndex 为 0。
  20. 否则,
    1. sourceByteIndexsourceByteOffset
  21. targetByteIndex 为 (targetOffset × targetElementSize) + targetByteOffset
  22. limittargetByteIndex + (targetElementSize × sourceLength)。
  23. 如果 sourceTypetargetType,则
    1. 注:传输必须以保留源数据的位级编码的方式执行。
    2. 重复,只要 targetByteIndex < limit
      1. valueGetValueFromBuffer(sourceBuffer, sourceByteIndex, uint8, true, unordered)。
      2. 执行 SetValueInBuffer(targetBuffer, targetByteIndex, uint8, value, true, unordered)。
      3. sourceByteIndex 设置为 sourceByteIndex + 1。
      4. targetByteIndex 设置为 targetByteIndex + 1。
  24. 否则,
    1. 重复,只要 targetByteIndex < limit
      1. valueGetValueFromBuffer(sourceBuffer, sourceByteIndex, sourceType, true, unordered)。
      2. 执行 SetValueInBuffer(targetBuffer, targetByteIndex, targetType, value, true, unordered)。
      3. sourceByteIndex 设置为 sourceByteIndex + sourceElementSize
      4. targetByteIndex 设置为 targetByteIndex + targetElementSize
  25. 返回 unused

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

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

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

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

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

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

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

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

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

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

23.2.3.29 %TypedArray%.prototype.sort ( comparator )

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

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

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

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

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

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

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

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

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

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

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

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

此方法不是泛型的。在对算法求值之前,以 this 值和 seq-cst 为实参调用 ValidateTypedArray。如果其结果是突然完成,则抛出该异常,而不是对算法求值。

Note

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

23.2.3.32 %TypedArray%.prototype.toReversed ( )

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

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

23.2.3.33 %TypedArray%.prototype.toSorted ( comparator )

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

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

23.2.3.34 %TypedArray%.prototype.toString ( )

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

23.2.3.35 %TypedArray%.prototype.values ( )

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

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

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

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

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

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

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

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

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

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

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

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

23.2.4 TypedArray 对象的抽象操作

23.2.4.1 TypedArrayCreateFromConstructor ( ctor, argList )

The abstract operation TypedArrayCreateFromConstructor takes arguments ctor (a constructor) and argList (a List of ECMAScript language values) and returns either a normal completion containing a TypedArray or a throw completion. 它用于指定使用构造器函数创建新的 TypedArray。 It performs the following steps when called:

  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. lengthTypedArrayLength(taRecord)。
    3. 如果 length < (argList[0]),则抛出 TypeError 异常。
  5. 返回 ta

23.2.4.2 TypedArrayCreateSameType ( exemplar, length )

The abstract operation TypedArrayCreateSameType takes arguments exemplar (a TypedArray) and length (a non-negative integer) and returns either a normal completion containing a TypedArray or a throw completion. 它用于指定使用从 exemplar 派生的构造器函数创建新的 TypedArray。不同于可以通过使用 %Symbol.species% 构造自定义 TypedArray 子类的 TypedArraySpeciesCreate,此操作总是使用内置 TypedArray 构造器之一。 It performs the following steps when called:

  1. ctorTable 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 )

The abstract operation TypedArraySpeciesCreate takes arguments exemplar (a TypedArray) and argList (a List of ECMAScript language values) and returns either a normal completion containing a TypedArray or a throw completion. 它用于指定使用从 exemplar 派生的构造器函数创建新的 TypedArray。不同于可以通过使用 %Symbol.species% 创建非 Array 对象的 ArraySpeciesCreate,此操作强制要求构造器函数创建实际的 TypedArray。 It performs the following steps when called:

  1. defaultCtorTable 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 )

The abstract operation ValidateTypedArray takes arguments obj (an ECMAScript language value) and order (seq-cst or unordered) and returns either a normal completion containing a TypedArray With Buffer Witness Record or a throw completion. It performs the following steps when called:

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

23.2.4.5 TypedArrayElementSize ( obj )

The abstract operation TypedArrayElementSize takes argument obj (a TypedArray) and returns a non-negative integer. It performs the following steps when called:

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

23.2.4.6 TypedArrayElementType ( obj )

The abstract operation TypedArrayElementType takes argument obj (a TypedArray) and returns a TypedArray element type. It performs the following steps when called:

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

23.2.4.7 CompareTypedArrayElements ( x, y, comparator )

The abstract operation CompareTypedArrayElements takes arguments x (a Number or a BigInt), y (a Number or a BigInt), and comparator (a function object or undefined) and returns either a normal completion containing a Number or an abrupt completion. It performs the following steps when called:

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

23.2.5 TypedArray 构造器

每个 TypedArray 构造器:

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

23.2.5.1 TypedArray ( ...args )

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

  1. 如果 NewTarget 是 undefined,则抛出 TypeError 异常。
  2. ctorNameTable 71 中为此 TypedArray 构造器指定的 Constructor Name 值的 String 值。
  3. proto"%TypedArray.prototype%"
  4. numberOfArgsargs 中元素的数量。
  5. 如果 numberOfArgs = 0,则返回 ? AllocateTypedArray(ctorName, NewTarget, proto, 0)。
  6. firstArgargs[0]。
  7. 如果 firstArg 是 Object,则
    1. obj 为 ? AllocateTypedArray(ctorName, NewTarget, proto)。
    2. 如果 firstArg 具有 [[TypedArrayName]] 内部槽,则
      1. 执行 ? InitializeTypedArrayFromTypedArray(obj, firstArg)。
    3. 否则,如果 firstArg 具有 [[ArrayBufferData]] 内部槽,则
      1. 如果 numberOfArgs > 1,则令 byteOffsetargs[1];否则令 byteOffsetundefined
      2. 如果 numberOfArgs > 2,则令 lengthargs[2];否则令 lengthundefined
      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 ] )

The abstract operation AllocateTypedArray takes arguments ctorName (a String which is the name of a TypedArray constructor in Table 71), newTarget (a constructor), and defaultProto (a String) and optional argument length (a non-negative integer) and returns either a normal completion containing a TypedArray or a throw completion. 它用于验证并创建 TypedArray 构造器的实例。如果传入了 length 实参,还会分配该长度的 ArrayBuffer,并将其与新的 TypedArray 实例关联。AllocateTypedArray 提供由 TypedArray 使用的公共语义。 It performs the following steps when called:

  1. proto 为 ? GetPrototypeFromConstructor(newTarget, defaultProto)。
  2. objTypedArrayCreate(proto)。
  3. 断言:obj.[[ViewedArrayBuffer]]undefined
  4. obj.[[TypedArrayName]] 设置为 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 )

The abstract operation InitializeTypedArrayFromTypedArray takes arguments obj (a TypedArray) and sourceArray (a TypedArray) and returns either a normal completion containing unused or a throw completion. It performs the following steps when called:

  1. sourceDatasourceArray.[[ViewedArrayBuffer]]
  2. elementTypeTypedArrayElementType(obj)。
  3. elementSizeTypedArrayElementSize(obj)。
  4. sourceTypeTypedArrayElementType(sourceArray)。
  5. sourceElementSizeTypedArrayElementSize(sourceArray)。
  6. sourceByteOffsetsourceArray.[[ByteOffset]]
  7. sourceRecordMakeTypedArrayWithBufferWitnessRecord(sourceArray, seq-cst)。
  8. 如果 IsTypedArrayOutOfBounds(sourceRecord) 是 true,则抛出 TypeError 异常。
  9. elementLengthTypedArrayLength(sourceRecord)。
  10. byteLengthelementSize × elementLength
  11. 如果 elementTypesourceType,则
    1. data 为 ? CloneArrayBuffer(sourceData, sourceByteOffset, byteLength)。
  12. 否则,
    1. data 为 ? AllocateArrayBuffer(%ArrayBuffer%, byteLength)。
    2. 如果 sourceArray.[[ContentType]] 不是 obj.[[ContentType]],则抛出 TypeError 异常。
    3. sourceByteIndexsourceByteOffset
    4. targetByteIndex 为 0。
    5. countelementLength
    6. 重复,只要 count > 0,
      1. valueGetValueFromBuffer(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。
  13. obj.[[ViewedArrayBuffer]] 设置为 data
  14. obj.[[ByteLength]] 设置为 byteLength
  15. obj.[[ByteOffset]] 设置为 0。
  16. obj.[[ArrayLength]] 设置为 elementLength
  17. 返回 unused

23.2.5.1.3 InitializeTypedArrayFromArrayBuffer ( obj, buffer, byteOffset, length )

The abstract operation InitializeTypedArrayFromArrayBuffer takes arguments obj (a TypedArray), buffer (an ArrayBuffer or a SharedArrayBuffer), byteOffset (an ECMAScript language value), and length (an ECMAScript language value) and returns either a normal completion containing unused or a throw completion. It performs the following steps when called:

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

23.2.5.1.4 InitializeTypedArrayFromList ( obj, values )

The abstract operation InitializeTypedArrayFromList takes arguments obj (a TypedArray) and values (a List of ECMAScript language values) and returns either a normal completion containing unused or a throw completion. It performs the following steps when called:

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

23.2.5.1.5 InitializeTypedArrayFromArrayLike ( obj, arrayLike )

The abstract operation InitializeTypedArrayFromArrayLike takes arguments obj (a TypedArray) and arrayLike (an Object, but not a TypedArray or an ArrayBuffer) and returns either a normal completion containing unused or a throw completion. It performs the following steps when called:

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

23.2.5.1.6 AllocateTypedArrayBuffer ( obj, length )

The abstract operation AllocateTypedArrayBuffer takes arguments obj (a TypedArray) and length (a non-negative integer) and returns either a normal completion containing unused or a throw completion. 它分配一个 ArrayBuffer,并将其与 obj 关联。 It performs the following steps when called:

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

23.2.6 TypedArray 构造器的属性

每个 TypedArray 构造器:

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

23.2.6.1 TypedArray.BYTES_PER_ELEMENT

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

23.3.1.2 Uint8Array.fromHex ( string )

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

23.3.2 Uint8Array 原型对象的额外属性

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

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

23.3.2.2 Uint8Array.prototype.setFromHex ( string )

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

23.3.2.3 Uint8Array.prototype.toBase64 ( [ options ] )

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

23.3.2.4 Uint8Array.prototype.toHex ( )

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

23.3.3 Uint8Array 对象的抽象操作

23.3.3.1 ValidateUint8Array ( ta )

The abstract operation ValidateUint8Array takes argument ta (an ECMAScript language value) and returns either a normal completion containing unused or a throw completion. It performs the following steps when called:

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

23.3.3.2 GetUint8ArrayBytes ( ta )

The abstract operation GetUint8ArrayBytes takes argument ta (a Uint8Array) and returns either a normal completion containing a List of byte values or a throw completion. It performs the following steps when called:

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

23.3.3.3 SetUint8ArrayBytes ( into, bytes )

The abstract operation SetUint8ArrayBytes takes arguments into (a Uint8Array) and bytes (a List of byte values) and returns unused. It performs the following steps when called:

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

23.3.3.4 SkipAsciiWhitespace ( string, index )

The abstract operation SkipAsciiWhitespace takes arguments string (a String) and index (a non-negative integer) and returns a non-negative integer. It performs the following steps when called:

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

23.3.3.5 DecodeFinalBase64Chunk ( chunk, throwOnExtraBits )

The abstract operation DecodeFinalBase64Chunk takes arguments chunk (a String of length 2 or 3) and throwOnExtraBits (a Boolean) and returns either a normal completion containing a List of byte values, or a throw completion. It performs the following steps when called:

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

23.3.3.6 DecodeFullLengthBase64Chunk ( chunk )

The abstract operation DecodeFullLengthBase64Chunk takes argument chunk (a String of length 4) and returns a List of byte values of length 3.

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

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

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

The abstract operation FromBase64 takes arguments string (a String), alphabet ("base64" or "base64url"), and lastChunkHandling ("loose", "strict", or "stop-before-partial") and optional argument maxLength (a non-negative integer) and returns a Record with fields [[Read]] (an integer), [[Bytes]] (a List of byte values), and [[Error]] (a SyntaxError object or none). It performs the following steps when called:

  1. 如果 maxLength 不存在,则
    1. maxLength 设置为 253 - 1。
    2. 注:因为输入是 String,String 的长度被限制为 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. lengthstring 的长度。
  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. charstringindexindex + 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 设置为 stringindexindex + 1 的子字符串
        3. 如果 char"=",则
          1. index 设置为 SkipAsciiWhitespace(string, index + 1)。
      4. 如果 index < length,则
        1. error 为新创建的 SyntaxError 对象。
        2. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: error }。
      5. 如果 lastChunkHandling"strict",则令 throwOnExtraBitstrue;否则令 throwOnExtraBitsfalse
      6. decodeResultCompletion(DecodeFinalBase64Chunk(chunk, throwOnExtraBits))。
      7. 如果 decodeResult突然完成,则
        1. errordecodeResult.[[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. remainingmaxLength - bytes 中元素的数量。
    10. 如果 remaining = 1 且 chunkLength = 2,或如果 remaining = 2 且 chunkLength = 3,则
      1. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }。
    11. chunk 设置为 chunkchar字符串连接
    12. chunkLength 设置为 chunk 的长度。
    13. 如果 chunkLength = 4,则
      1. bytes 设置为 bytesDecodeFullLengthBase64Chunk(chunk) 的列表连接
      2. chunk 设置为空 String。
      3. chunkLength 设置为 0。
      4. read 设置为 index
      5. 如果 bytes 中元素的数量 = maxLength,则
        1. 返回 Record { [[Read]]: read, [[Bytes]]: bytes, [[Error]]: none }。

23.3.3.8 FromHex ( string [ , maxLength ] )

The abstract operation FromHex takes argument string (a String) and optional argument maxLength (a non-negative integer) and returns a Record with fields [[Read]] (an integer), [[Bytes]] (a List of byte values), and [[Error]] (a SyntaxError object or none). It performs the following steps when called:

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