23 Indexed Collections
23.1 Array Objects
Arrays are
23.1.1 The Array Constructor
The Array
- is
%Array% . - is the initial value of the
"Array" property of theglobal object . - creates and initializes a new Array when called as a
constructor . - also creates and initializes a new Array when called as a function rather than as a
constructor . Thus the function callArray(…)is equivalent to the object creation expressionnew Array(…)with the same arguments. - is a function whose behaviour differs based upon the number and types of its arguments.
- may be used as the value of an
extendsclause of a class definition. Subclassconstructors that intend to inherit the exotic Array behaviour must include asupercall to the Arrayconstructor to initialize subclass instances that areArray exotic objects . However, most of theArray.prototypemethods are generic methods that are not dependent upon theirthis value being anArray exotic object . - has a
"length" property whose value is1 𝔽.
23.1.1.1 Array ( ...values )
When the Array function is called, the following steps are taken:
- If NewTarget is
undefined , let newTarget be theactive function object ; else let newTarget be NewTarget. - Let proto be ?
GetPrototypeFromConstructor (newTarget," ).%Array.prototype% " - Let numberOfArgs be the number of elements in values.
- If numberOfArgs = 0, then
- Return !
ArrayCreate (0, proto).
- Return !
- Else if numberOfArgs = 1, then
- Let len be values[0].
- Let array be !
ArrayCreate (0, proto). - If
Type (len) is not Number, then- Perform !
CreateDataPropertyOrThrow (array,"0" , len). - Let intLen be
1 𝔽.
- Perform !
- Else,
- Let intLen be !
ToUint32 (len). - If
SameValueZero (intLen, len) isfalse , throw aRangeError exception.
- Let intLen be !
- Perform !
Set (array,"length" , intLen,true ). - Return array.
- Else,
Assert : numberOfArgs ≥ 2.- Let array be ?
ArrayCreate (numberOfArgs, proto). - Let k be 0.
- Repeat, while k < numberOfArgs,
- Let Pk be !
ToString (𝔽(k)). - Let itemK be values[k].
- Perform !
CreateDataPropertyOrThrow (array, Pk, itemK). - Set k to k + 1.
- Let Pk be !
Assert : Themathematical value of array's"length" property is numberOfArgs.- Return array.
23.1.2 Properties of the Array Constructor
The Array
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has the following properties:
23.1.2.1 Array.from ( items [ , mapfn [ , thisArg ] ] )
When the from method is called, the following steps are taken:
- Let C be the
this value. - If mapfn is
undefined , let mapping befalse . - Else,
- If
IsCallable (mapfn) isfalse , throw aTypeError exception. - Let mapping be
true .
- If
- Let usingIterator be ?
GetMethod (items, @@iterator). - If usingIterator is not
undefined , then- If
IsConstructor (C) istrue , then- Let A be ?
Construct (C).
- Let A be ?
- Else,
- Let A be !
ArrayCreate (0).
- Let A be !
- Let iteratorRecord be ?
GetIterator (items,sync , usingIterator). - Let k be 0.
- Repeat,
- If k ≥ 253 - 1, then
- Let error be
ThrowCompletion (a newly createdTypeError object). - Return ?
IteratorClose (iteratorRecord, error).
- Let error be
- Let Pk be !
ToString (𝔽(k)). - Let next be ?
IteratorStep (iteratorRecord). - If next is
false , then- Perform ?
Set (A,"length" , 𝔽(k),true ). - Return A.
- Perform ?
- Let nextValue be ?
IteratorValue (next). - If mapping is
true , then- Let mappedValue be
Completion (Call (mapfn, thisArg, « nextValue, 𝔽(k) »)). IfAbruptCloseIterator (mappedValue, iteratorRecord).
- Let mappedValue be
- Else, let mappedValue be nextValue.
- Let defineStatus be
Completion (CreateDataPropertyOrThrow (A, Pk, mappedValue)). IfAbruptCloseIterator (defineStatus, iteratorRecord).- Set k to k + 1.
- If k ≥ 253 - 1, then
- If
- NOTE: items is not an Iterable so assume it is an
array-like object . - Let arrayLike be !
ToObject (items). - Let len be ?
LengthOfArrayLike (arrayLike). - If
IsConstructor (C) istrue , then- Let A be ?
Construct (C, « 𝔽(len) »).
- Let A be ?
- Else,
- Let A be ?
ArrayCreate (len).
- Let A be ?
- Let k be 0.
- Repeat, while k < len,
- Let Pk be !
ToString (𝔽(k)). - Let kValue be ?
Get (arrayLike, Pk). - If mapping is
true , then- Let mappedValue be ?
Call (mapfn, thisArg, « kValue, 𝔽(k) »).
- Let mappedValue be ?
- Else, let mappedValue be kValue.
- Perform ?
CreateDataPropertyOrThrow (A, Pk, mappedValue). - Set k to k + 1.
- Let Pk be !
- Perform ?
Set (A,"length" , 𝔽(len),true ). - Return A.
The from function is an intentionally generic factory method; it does not require that its
23.1.2.2 Array.isArray ( arg )
When the isArray method is called, the following steps are taken:
- Return ?
IsArray (arg).
23.1.2.3 Array.of ( ...items )
When the of method is called, the following steps are taken:
- Let len be the number of elements in items.
- Let lenNumber be 𝔽(len).
- Let C be the
this value. - If
IsConstructor (C) istrue , then- Let A be ?
Construct (C, « lenNumber »).
- Let A be ?
- Else,
- Let A be ?
ArrayCreate (len).
- Let A be ?
- Let k be 0.
- Repeat, while k < len,
- Let kValue be items[k].
- Let Pk be !
ToString (𝔽(k)). - Perform ?
CreateDataPropertyOrThrow (A, Pk, kValue). - Set k to k + 1.
- Perform ?
Set (A,"length" , lenNumber,true ). - Return A.
The of function is an intentionally generic factory method; it does not require that its
23.1.2.4 Array.prototype
The value of Array.prototype is the
This property has the attributes { [[Writable]]:
23.1.2.5 get Array [ @@species ]
Array[@@species] is an
- Return the
this value.
The value of the
Array prototype methods normally use their
23.1.3 Properties of the Array Prototype Object
The Array prototype object:
- is
%Array.prototype% . - is an
Array exotic object and has the internal methods specified for such objects. - has a
"length" property whose initial value is+0 𝔽 and whose attributes are { [[Writable]]:true , [[Enumerable]]:false , [[Configurable]]:false }. - has a [[Prototype]] internal slot whose value is
%Object.prototype% .
The Array prototype object is specified to be an
23.1.3.1 Array.prototype.at ( index )
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - Let relativeIndex be ?
ToIntegerOrInfinity (index). - If relativeIndex ≥ 0, then
- Let k be relativeIndex.
- Else,
- Let k be len + relativeIndex.
- If k < 0 or k ≥ len, return
undefined . - Return ?
Get (O, !ToString (𝔽(k))).
23.1.3.2 Array.prototype.concat ( ...items )
Returns an array containing the array elements of the object followed by the array elements of each argument.
When the concat method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let A be ?
ArraySpeciesCreate (O, 0). - Let n be 0.
- Prepend O to items.
- For each element E of items, do
- Let spreadable be ?
IsConcatSpreadable (E). - If spreadable is
true , then- Let k be 0.
- Let len be ?
LengthOfArrayLike (E). - If n + len > 253 - 1, throw a
TypeError exception. - Repeat, while k < len,
- Let P be !
ToString (𝔽(k)). - Let exists be ?
HasProperty (E, P). - If exists is
true , then- Let subElement be ?
Get (E, P). - Perform ?
CreateDataPropertyOrThrow (A, !ToString (𝔽(n)), subElement).
- Let subElement be ?
- Set n to n + 1.
- Set k to k + 1.
- Let P be !
- Else,
- NOTE: E is added as a single item rather than spread.
- If n ≥ 253 - 1, throw a
TypeError exception. - Perform ?
CreateDataPropertyOrThrow (A, !ToString (𝔽(n)), E). - Set n to n + 1.
- Let spreadable be ?
- Perform ?
Set (A,"length" , 𝔽(n),true ). - Return A.
The concat method is
The explicit setting of the
The concat function is intentionally generic; it does not require that its
23.1.3.2.1 IsConcatSpreadable ( O )
The abstract operation IsConcatSpreadable takes argument O and returns either a
23.1.3.3 Array.prototype.constructor
The initial value of Array.prototype.constructor is
23.1.3.4 Array.prototype.copyWithin ( target, start [ , end ] )
The end argument is optional. If it is not provided, the length of the
If target is negative, it is treated as
When the copyWithin method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - Let relativeTarget be ?
ToIntegerOrInfinity (target). - If relativeTarget is -∞, let to be 0.
- Else if relativeTarget < 0, let to be
max (len + relativeTarget, 0). - Else, let to be
min (relativeTarget, len). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart is -∞, let from be 0.
- Else if relativeStart < 0, let from be
max (len + relativeStart, 0). - Else, let from be
min (relativeStart, len). - If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd is -∞, let final be 0.
- Else if relativeEnd < 0, let final be
max (len + relativeEnd, 0). - Else, let final be
min (relativeEnd, len). - Let count be
min (final - from, len - to). - If from < to and to < from + count, then
- Let direction be -1.
- Set from to from + count - 1.
- Set to to to + count - 1.
- Else,
- Let direction be 1.
- Repeat, while count > 0,
- Let fromKey be !
ToString (𝔽(from)). - Let toKey be !
ToString (𝔽(to)). - Let fromPresent be ?
HasProperty (O, fromKey). - If fromPresent is
true , then - Else,
Assert : fromPresent isfalse .- Perform ?
DeletePropertyOrThrow (O, toKey).
- Set from to from + direction.
- Set to to to + direction.
- Set count to count - 1.
- Let fromKey be !
- Return O.
The copyWithin function is intentionally generic; it does not require that its
23.1.3.5 Array.prototype.entries ( )
When the entries method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Return
CreateArrayIterator (O,key+value ).
23.1.3.6 Array.prototype.every ( callbackfn [ , thisArg ] )
callbackfn should be a function that accepts three arguments and returns a value that is coercible to a Boolean value. every calls callbackfn once for each element present in the array, in ascending order, until it finds one where callbackfn returns every immediately returns every will return
If a thisArg parameter is provided, it will be used as the
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
every does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
The range of elements processed by every is set before the first call to callbackfn. Elements which are appended to the array after the call to every begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time every visits them; elements that are deleted after the call to every begins and before being visited are not visited. every acts like the "for all" quantifier in mathematics. In particular, for an empty array, it returns
When the every method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (O, Pk). - If kPresent is
true , then - Set k to k + 1.
- Let Pk be !
- Return
true .
The every function is intentionally generic; it does not require that its
23.1.3.7 Array.prototype.fill ( value [ , start [ , end ] ] )
The start argument is optional. If it is not provided,
The end argument is optional. If it is not provided, the length of the
If start is negative, it is treated as
When the fill method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart is -∞, let k be 0.
- Else if relativeStart < 0, let k be
max (len + relativeStart, 0). - Else, let k be
min (relativeStart, len). - If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd is -∞, let final be 0.
- Else if relativeEnd < 0, let final be
max (len + relativeEnd, 0). - Else, let final be
min (relativeEnd, len). - Repeat, while k < final,
- Return O.
The fill function is intentionally generic; it does not require that its
23.1.3.8 Array.prototype.filter ( callbackfn [ , thisArg ] )
callbackfn should be a function that accepts three arguments and returns a value that is coercible to a Boolean value. filter calls callbackfn once for each element in the array, in ascending order, and constructs a new array of all the values for which callbackfn returns
If a thisArg parameter is provided, it will be used as the
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
filter does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
The range of elements processed by filter is set before the first call to callbackfn. Elements which are appended to the array after the call to filter begins will not be visited by callbackfn. If existing elements of the array are changed their value as passed to callbackfn will be the value at the time filter visits them; elements that are deleted after the call to filter begins and before being visited are not visited.
When the filter method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let A be ?
ArraySpeciesCreate (O, 0). - Let k be 0.
- Let to be 0.
- Repeat, while k < len,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (O, Pk). - If kPresent is
true , then- Let kValue be ?
Get (O, Pk). - Let selected be
ToBoolean (?Call (callbackfn, thisArg, « kValue, 𝔽(k), O »)). - If selected is
true , then- Perform ?
CreateDataPropertyOrThrow (A, !ToString (𝔽(to)), kValue). - Set to to to + 1.
- Perform ?
- Let kValue be ?
- Set k to k + 1.
- Let Pk be !
- Return A.
The filter function is intentionally generic; it does not require that its
23.1.3.9 Array.prototype.find ( predicate [ , thisArg ] )
predicate should be a function that accepts three arguments and returns a value that is coercible to a Boolean value. find calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns find immediately returns that element value. Otherwise, find returns
If a thisArg parameter is provided, it will be used as the
predicate is called with three arguments: the value of the element, the index of the element, and the object being traversed.
find does not directly mutate the object on which it is called but the object may be mutated by the calls to predicate.
The range of elements processed by find is set before the first call to predicate. Elements that are appended to the array after the call to find begins will not be visited by predicate. If existing elements of the array are changed, their value as passed to predicate will be the value at the time that find visits them; elements that are deleted after the call to find begins and before being visited are still visited and are either looked up from the prototype or are
When the find method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (predicate) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
undefined .
The find function is intentionally generic; it does not require that its
23.1.3.10 Array.prototype.findIndex ( predicate [ , thisArg ] )
predicate should be a function that accepts three arguments and returns a value that is coercible to a Boolean value. findIndex calls predicate once for each element of the array, in ascending order, until it finds one where predicate returns findIndex immediately returns the index of that element value. Otherwise, findIndex returns -1.
If a thisArg parameter is provided, it will be used as the
predicate is called with three arguments: the value of the element, the index of the element, and the object being traversed.
findIndex does not directly mutate the object on which it is called but the object may be mutated by the calls to predicate.
The range of elements processed by findIndex is set before the first call to predicate. Elements that are appended to the array after the call to findIndex begins will not be visited by predicate. If existing elements of the array are changed, their value as passed to predicate will be the value at the time that findIndex visits them; elements that are deleted after the call to findIndex begins and before being visited are still visited and are either looked up from the prototype or are
When the findIndex method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (predicate) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
-1 𝔽.
The findIndex function is intentionally generic; it does not require that its
23.1.3.11 Array.prototype.flat ( [ depth ] )
When the flat method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let sourceLen be ?
LengthOfArrayLike (O). - Let depthNum be 1.
- If depth is not
undefined , then- Set depthNum to ?
ToIntegerOrInfinity (depth). - If depthNum < 0, set depthNum to 0.
- Set depthNum to ?
- Let A be ?
ArraySpeciesCreate (O, 0). - Perform ?
FlattenIntoArray (A, O, sourceLen, 0, depthNum). - Return A.
23.1.3.11.1 FlattenIntoArray ( target, source, sourceLen, start, depth [ , mapperFunction [ , thisArg ] ] )
The abstract operation FlattenIntoArray takes arguments target (an Object), source (an Object), sourceLen (a non-negative
Assert : If mapperFunction is present, thenIsCallable (mapperFunction) istrue , thisArg is present, and depth is 1.- Let targetIndex be start.
- Let sourceIndex be
+0 𝔽. - Repeat, while ℝ(sourceIndex) < sourceLen,
- Let P be !
ToString (sourceIndex). - Let exists be ?
HasProperty (source, P). - If exists is
true , then- Let element be ?
Get (source, P). - If mapperFunction is present, then
- Set element to ?
Call (mapperFunction, thisArg, « element, sourceIndex, source »).
- Set element to ?
- Let shouldFlatten be
false . - If depth > 0, then
- Set shouldFlatten to ?
IsArray (element).
- Set shouldFlatten to ?
- If shouldFlatten is
true , then- If depth is +∞, let newDepth be +∞.
- Else, let newDepth be depth - 1.
- Let elementLen be ?
LengthOfArrayLike (element). - Set targetIndex to ? FlattenIntoArray(target, element, elementLen, targetIndex, newDepth).
- Else,
- If targetIndex ≥ 253 - 1, throw a
TypeError exception. - Perform ?
CreateDataPropertyOrThrow (target, !ToString (𝔽(targetIndex)), element). - Set targetIndex to targetIndex + 1.
- If targetIndex ≥ 253 - 1, throw a
- Let element be ?
- Set sourceIndex to sourceIndex +
1 𝔽.
- Let P be !
- Return targetIndex.
23.1.3.12 Array.prototype.flatMap ( mapperFunction [ , thisArg ] )
When the flatMap method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let sourceLen be ?
LengthOfArrayLike (O). - If
IsCallable (mapperFunction) isfalse , throw aTypeError exception. - Let A be ?
ArraySpeciesCreate (O, 0). - Perform ?
FlattenIntoArray (A, O, sourceLen, 0, 1, mapperFunction, thisArg). - Return A.
23.1.3.13 Array.prototype.forEach ( callbackfn [ , thisArg ] )
callbackfn should be a function that accepts three arguments. forEach calls callbackfn once for each element present in the array, in ascending order. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.
If a thisArg parameter is provided, it will be used as the
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
forEach does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
The range of elements processed by forEach is set before the first call to callbackfn. Elements which are appended to the array after the call to forEach begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time forEach visits them; elements that are deleted after the call to forEach begins and before being visited are not visited.
When the forEach method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (O, Pk). - If kPresent is
true , then - Set k to k + 1.
- Let Pk be !
- Return
undefined .
The forEach function is intentionally generic; it does not require that its
23.1.3.14 Array.prototype.includes ( searchElement [ , fromIndex ] )
includes compares searchElement to the elements of the array, in ascending order, using the
The optional second argument fromIndex defaults to
When the includes method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If len is 0, return
false . - Let n be ?
ToIntegerOrInfinity (fromIndex). Assert : If fromIndex isundefined , then n is 0.- If n is +∞, return
false . - Else if n is -∞, set n to 0.
- If n ≥ 0, then
- Let k be n.
- Else,
- Let k be len + n.
- If k < 0, set k to 0.
- Repeat, while k < len,
- Let elementK be ?
Get (O, !ToString (𝔽(k))). - If
SameValueZero (searchElement, elementK) istrue , returntrue . - Set k to k + 1.
- Let elementK be ?
- Return
false .
The includes function is intentionally generic; it does not require that its
The includes method intentionally differs from the similar indexOf method in two ways. First, it uses the
23.1.3.15 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
indexOf compares searchElement to the elements of the array, in ascending order, using the
The optional second argument fromIndex defaults to
When the indexOf method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If len is 0, return
-1 𝔽. - Let n be ?
ToIntegerOrInfinity (fromIndex). Assert : If fromIndex isundefined , then n is 0.- If n is +∞, return
-1 𝔽. - Else if n is -∞, set n to 0.
- If n ≥ 0, then
- Let k be n.
- Else,
- Let k be len + n.
- If k < 0, set k to 0.
- Repeat, while k < len,
- Let kPresent be ?
HasProperty (O, !ToString (𝔽(k))). - If kPresent is
true , then- Let elementK be ?
Get (O, !ToString (𝔽(k))). - Let same be
IsStrictlyEqual (searchElement, elementK). - If same is
true , return 𝔽(k).
- Let elementK be ?
- Set k to k + 1.
- Let kPresent be ?
- Return
-1 𝔽.
The indexOf function is intentionally generic; it does not require that its
23.1.3.16 Array.prototype.join ( separator )
The elements of the array are converted to Strings, and these Strings are then concatenated, separated by occurrences of the separator. If no separator is provided, a single comma is used as the separator.
When the join method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If separator is
undefined , let sep be the single-element String"," . - Else, let sep be ?
ToString (separator). - Let R be the empty String.
- Let k be 0.
- Repeat, while k < len,
- If k > 0, set R to the
string-concatenation of R and sep. - Let element be ?
Get (O, !ToString (𝔽(k))). - If element is
undefined ornull , let next be the empty String; otherwise, let next be ?ToString (element). - Set R to the
string-concatenation of R and next. - Set k to k + 1.
- If k > 0, set R to the
- Return R.
The join function is intentionally generic; it does not require that its
23.1.3.17 Array.prototype.keys ( )
When the keys method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Return
CreateArrayIterator (O,key ).
23.1.3.18 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
lastIndexOf compares searchElement to the elements of the array in descending order using the
The optional second argument fromIndex defaults to the array's length minus one (i.e. the whole array is searched). If it is greater than or equal to the length of the array, the whole array will be searched. If it is less than
When the lastIndexOf method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If len is 0, return
-1 𝔽. - If fromIndex is present, let n be ?
ToIntegerOrInfinity (fromIndex); else let n be len - 1. - If n is -∞, return
-1 𝔽. - If n ≥ 0, then
- Let k be
min (n, len - 1).
- Let k be
- Else,
- Let k be len + n.
- Repeat, while k ≥ 0,
- Let kPresent be ?
HasProperty (O, !ToString (𝔽(k))). - If kPresent is
true , then- Let elementK be ?
Get (O, !ToString (𝔽(k))). - Let same be
IsStrictlyEqual (searchElement, elementK). - If same is
true , return 𝔽(k).
- Let elementK be ?
- Set k to k - 1.
- Let kPresent be ?
- Return
-1 𝔽.
The lastIndexOf function is intentionally generic; it does not require that its
23.1.3.19 Array.prototype.map ( callbackfn [ , thisArg ] )
callbackfn should be a function that accepts three arguments. map calls callbackfn once for each element in the array, in ascending order, and constructs a new Array from the results. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.
If a thisArg parameter is provided, it will be used as the
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
map does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
The range of elements processed by map is set before the first call to callbackfn. Elements which are appended to the array after the call to map begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time map visits them; elements that are deleted after the call to map begins and before being visited are not visited.
When the map method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let A be ?
ArraySpeciesCreate (O, len). - Let k be 0.
- Repeat, while k < len,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (O, Pk). - If kPresent is
true , then- Let kValue be ?
Get (O, Pk). - Let mappedValue be ?
Call (callbackfn, thisArg, « kValue, 𝔽(k), O »). - Perform ?
CreateDataPropertyOrThrow (A, Pk, mappedValue).
- Let kValue be ?
- Set k to k + 1.
- Let Pk be !
- Return A.
The map function is intentionally generic; it does not require that its
23.1.3.20 Array.prototype.pop ( )
The last element of the array is removed from the array and returned.
When the pop method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If len = 0, then
- Perform ?
Set (O,"length" ,+0 𝔽,true ). - Return
undefined .
- Perform ?
- Else,
Assert : len > 0.- Let newLen be 𝔽(len - 1).
- Let index be !
ToString (newLen). - Let element be ?
Get (O, index). - Perform ?
DeletePropertyOrThrow (O, index). - Perform ?
Set (O,"length" , newLen,true ). - Return element.
The pop function is intentionally generic; it does not require that its
23.1.3.21 Array.prototype.push ( ...items )
The arguments are appended to the end of the array, in the order in which they appear. The new length of the array is returned as the result of the call.
When the push method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - Let argCount be the number of elements in items.
- If len + argCount > 253 - 1, throw a
TypeError exception. - For each element E of items, do
- Perform ?
Set (O,"length" , 𝔽(len),true ). - Return 𝔽(len).
The push method is
The push function is intentionally generic; it does not require that its
23.1.3.22 Array.prototype.reduce ( callbackfn [ , initialValue ] )
callbackfn should be a function that takes four arguments. reduce calls the callback, as a function, once for each element after the first element present in the array, in ascending order.
callbackfn is called with four arguments: the previousValue (value from the previous call to callbackfn), the currentValue (value of the current element), the currentIndex, and the object being traversed. The first time that callback is called, the previousValue and currentValue can be one of two values. If an initialValue was supplied in the call to reduce, then previousValue will be equal to initialValue and currentValue will be equal to the first value in the array. If no initialValue was supplied, then previousValue will be equal to the first value in the array and currentValue will be equal to the second. It is a
reduce does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
The range of elements processed by reduce is set before the first call to callbackfn. Elements that are appended to the array after the call to reduce begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time reduce visits them; elements that are deleted after the call to reduce begins and before being visited are not visited.
When the reduce method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - If len = 0 and initialValue is not present, throw a
TypeError exception. - Let k be 0.
- Let accumulator be
undefined . - If initialValue is present, then
- Set accumulator to initialValue.
- Else,
- Let kPresent be
false . - Repeat, while kPresent is
false and k < len,- Let Pk be !
ToString (𝔽(k)). - Set kPresent to ?
HasProperty (O, Pk). - If kPresent is
true , then- Set accumulator to ?
Get (O, Pk).
- Set accumulator to ?
- Set k to k + 1.
- Let Pk be !
- If kPresent is
false , throw aTypeError exception.
- Let kPresent be
- Repeat, while k < len,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (O, Pk). - If kPresent is
true , then - Set k to k + 1.
- Let Pk be !
- Return accumulator.
The reduce function is intentionally generic; it does not require that its
23.1.3.23 Array.prototype.reduceRight ( callbackfn [ , initialValue ] )
callbackfn should be a function that takes four arguments. reduceRight calls the callback, as a function, once for each element after the first element present in the array, in descending order.
callbackfn is called with four arguments: the previousValue (value from the previous call to callbackfn), the currentValue (value of the current element), the currentIndex, and the object being traversed. The first time the function is called, the previousValue and currentValue can be one of two values. If an initialValue was supplied in the call to reduceRight, then previousValue will be equal to initialValue and currentValue will be equal to the last value in the array. If no initialValue was supplied, then previousValue will be equal to the last value in the array and currentValue will be equal to the second-to-last value. It is a
reduceRight does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
The range of elements processed by reduceRight is set before the first call to callbackfn. Elements that are appended to the array after the call to reduceRight begins will not be visited by callbackfn. If existing elements of the array are changed by callbackfn, their value as passed to callbackfn will be the value at the time reduceRight visits them; elements that are deleted after the call to reduceRight begins and before being visited are not visited.
When the reduceRight method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - If len is 0 and initialValue is not present, throw a
TypeError exception. - Let k be len - 1.
- Let accumulator be
undefined . - If initialValue is present, then
- Set accumulator to initialValue.
- Else,
- Let kPresent be
false . - Repeat, while kPresent is
false and k ≥ 0,- Let Pk be !
ToString (𝔽(k)). - Set kPresent to ?
HasProperty (O, Pk). - If kPresent is
true , then- Set accumulator to ?
Get (O, Pk).
- Set accumulator to ?
- Set k to k - 1.
- Let Pk be !
- If kPresent is
false , throw aTypeError exception.
- Let kPresent be
- Repeat, while k ≥ 0,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (O, Pk). - If kPresent is
true , then - Set k to k - 1.
- Let Pk be !
- Return accumulator.
The reduceRight function is intentionally generic; it does not require that its
23.1.3.24 Array.prototype.reverse ( )
The elements of the array are rearranged so as to reverse their order. The object is returned as the result of the call.
When the reverse method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - Let middle be
floor (len / 2). - Let lower be 0.
- Repeat, while lower ≠ middle,
- Let upper be len - lower - 1.
- Let upperP be !
ToString (𝔽(upper)). - Let lowerP be !
ToString (𝔽(lower)). - Let lowerExists be ?
HasProperty (O, lowerP). - If lowerExists is
true , then- Let lowerValue be ?
Get (O, lowerP).
- Let lowerValue be ?
- Let upperExists be ?
HasProperty (O, upperP). - If upperExists is
true , then- Let upperValue be ?
Get (O, upperP).
- Let upperValue be ?
- If lowerExists is
true and upperExists istrue , then - Else if lowerExists is
false and upperExists istrue , then- Perform ?
Set (O, lowerP, upperValue,true ). - Perform ?
DeletePropertyOrThrow (O, upperP).
- Perform ?
- Else if lowerExists is
true and upperExists isfalse , then- Perform ?
DeletePropertyOrThrow (O, lowerP). - Perform ?
Set (O, upperP, lowerValue,true ).
- Perform ?
- Else,
Assert : lowerExists and upperExists are bothfalse .- No action is required.
- Set lower to lower + 1.
- Return O.
The reverse function is intentionally generic; it does not require that its
23.1.3.25 Array.prototype.shift ( )
The first element of the array is removed from the array and returned.
When the shift method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If len = 0, then
- Perform ?
Set (O,"length" ,+0 𝔽,true ). - Return
undefined .
- Perform ?
- Let first be ?
Get (O,"0" ). - Let k be 1.
- Repeat, while k < len,
- Let from be !
ToString (𝔽(k)). - Let to be !
ToString (𝔽(k - 1)). - Let fromPresent be ?
HasProperty (O, from). - If fromPresent is
true , then - Else,
Assert : fromPresent isfalse .- Perform ?
DeletePropertyOrThrow (O, to).
- Set k to k + 1.
- Let from be !
- Perform ?
DeletePropertyOrThrow (O, !ToString (𝔽(len - 1))). - Perform ?
Set (O,"length" , 𝔽(len - 1),true ). - Return first.
The shift function is intentionally generic; it does not require that its
23.1.3.26 Array.prototype.slice ( start, end )
The slice method returns an array containing the elements of the array from element start up to, but not including, element end (or through the end of the array if end is
When the slice method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart is -∞, let k be 0.
- Else if relativeStart < 0, let k be
max (len + relativeStart, 0). - Else, let k be
min (relativeStart, len). - If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd is -∞, let final be 0.
- Else if relativeEnd < 0, let final be
max (len + relativeEnd, 0). - Else, let final be
min (relativeEnd, len). - Let count be
max (final - k, 0). - Let A be ?
ArraySpeciesCreate (O, count). - Let n be 0.
- Repeat, while k < final,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (O, Pk). - If kPresent is
true , then- Let kValue be ?
Get (O, Pk). - Perform ?
CreateDataPropertyOrThrow (A, !ToString (𝔽(n)), kValue).
- Let kValue be ?
- Set k to k + 1.
- Set n to n + 1.
- Let Pk be !
- Perform ?
Set (A,"length" , 𝔽(n),true ). - Return A.
The explicit setting of the
The slice function is intentionally generic; it does not require that its
23.1.3.27 Array.prototype.some ( callbackfn [ , thisArg ] )
callbackfn should be a function that accepts three arguments and returns a value that is coercible to a Boolean value. some calls callbackfn once for each element present in the array, in ascending order, until it finds one where callbackfn returns some immediately returns some returns
If a thisArg parameter is provided, it will be used as the
callbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
some does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
The range of elements processed by some is set before the first call to callbackfn. Elements that are appended to the array after the call to some begins will not be visited by callbackfn. If existing elements of the array are changed, their value as passed to callbackfn will be the value at the time that some visits them; elements that are deleted after the call to some begins and before being visited are not visited. some acts like the "exists" quantifier in mathematics. In particular, for an empty array, it returns
When the some method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (O, Pk). - If kPresent is
true , then - Set k to k + 1.
- Let Pk be !
- Return
false .
The some function is intentionally generic; it does not require that its
23.1.3.28 Array.prototype.sort ( comparefn )
The elements of this array are sorted. The sort must be stable (that is, elements that compare equal must remain in their original order). If comparefn is not
When the sort method is called, the following steps are taken:
- If comparefn is not
undefined andIsCallable (comparefn) isfalse , throw aTypeError exception. - Let obj be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (obj). - Let SortCompare be a new
Abstract Closure with parameters (x, y) that captures comparefn and performs the following steps when called:- If x and y are both
undefined , return+0 𝔽. - If x is
undefined , return1 𝔽. - If y is
undefined , return-1 𝔽. - If comparefn is not
undefined , then - Let xString be ?
ToString (x). - Let yString be ?
ToString (y). - Let xSmaller be !
IsLessThan (xString, yString,true ). - If xSmaller is
true , return-1 𝔽. - Let ySmaller be !
IsLessThan (yString, xString,true ). - If ySmaller is
true , return1 𝔽. - Return
+0 𝔽.
- If x and y are both
- Return ?
SortIndexedProperties (obj, len, SortCompare).
Because non-existent property values always compare greater than
Method calls performed by the
The sort function is intentionally generic; it does not require that its
23.1.3.28.1 SortIndexedProperties ( obj, len, SortCompare )
The abstract operation SortIndexedProperties takes arguments obj (an Object), len (a non-negative
- Let items be a new empty
List . - Let k be 0.
- Repeat, while k < len,
- Let Pk be !
ToString (𝔽(k)). - Let kPresent be ?
HasProperty (obj, Pk). - If kPresent is
true , then- Let kValue be ?
Get (obj, Pk). - Append kValue to items.
- Let kValue be ?
- Set k to k + 1.
- Let Pk be !
- Let itemCount be the number of elements in items.
- Sort items using an
implementation-defined sequence of calls to SortCompare. If any such call returns anabrupt completion , stop before performing any further calls to SortCompare and return thatCompletion Record . - Let j be 0.
- Repeat, while j < itemCount,
- Repeat, while j < len,
- Perform ?
DeletePropertyOrThrow (obj, !ToString (𝔽(j))). - Set j to j + 1.
- Perform ?
- Return obj.
The sort order is the ordering of items after completion of step
Unless the
-
There must be some mathematical permutation π of the non-negative
integers less than itemCount, such that for every non-negativeinteger j less than itemCount, the elementold[j] is exactly the same asnew[π(j)] . -
Then for all non-negative
integers j and k, each less than itemCount, ifSortCompare(old[j], old[k]) < 0 , thenπ(j) < π(k) .
Here the notation
An abstract closure or function comparator is a consistent comparator for a set of values S if all of the requirements below are met for all values a, b, and c (possibly the same value) in the set S: The notation
-
Calling comparator(a, b) always returns the same value v when given a specific pair of values a and b as its two arguments. Furthermore,
Type (v) is Number, and v is notNaN . Note that this implies that exactly one of a <C b, a =C b, and a >C b will be true for a given pair of a and b. - Calling comparator(a, b) does not modify obj or any object on obj's prototype chain.
- a =C a (reflexivity)
- If a =C b, then b =C a (symmetry)
- If a =C b and b =C c, then a =C c (transitivity of =C)
- If a <C b and b <C c, then a <C c (transitivity of <C)
- If a >C b and b >C c, then a >C c (transitivity of >C)
The above conditions are necessary and sufficient to ensure that comparator divides the set S into equivalence classes and that these equivalence classes are totally ordered.
23.1.3.29 Array.prototype.splice ( start, deleteCount, ...items )
The deleteCount elements of the array starting at
When the splice method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart is -∞, let actualStart be 0.
- Else if relativeStart < 0, let actualStart be
max (len + relativeStart, 0). - Else, let actualStart be
min (relativeStart, len). - Let insertCount be the number of elements in items.
- If start is not present, then
- Let actualDeleteCount be 0.
- Else if deleteCount is not present, then
- Let actualDeleteCount be len - actualStart.
- Else,
- Let dc be ?
ToIntegerOrInfinity (deleteCount). - Let actualDeleteCount be the result of
clamping dc between 0 and len - actualStart.
- Let dc be ?
- If len + insertCount - actualDeleteCount > 253 - 1, throw a
TypeError exception. - Let A be ?
ArraySpeciesCreate (O, actualDeleteCount). - Let k be 0.
- Repeat, while k < actualDeleteCount,
- Let from be !
ToString (𝔽(actualStart + k)). - If ?
HasProperty (O, from) istrue , then- Let fromValue be ?
Get (O, from). - Perform ?
CreateDataPropertyOrThrow (A, !ToString (𝔽(k)), fromValue).
- Let fromValue be ?
- Set k to k + 1.
- Let from be !
- Perform ?
Set (A,"length" , 𝔽(actualDeleteCount),true ). - Let itemCount be the number of elements in items.
- If itemCount < actualDeleteCount, then
- Set k to actualStart.
- Repeat, while k < (len - actualDeleteCount),
- Let from be !
ToString (𝔽(k + actualDeleteCount)). - Let to be !
ToString (𝔽(k + itemCount)). - If ?
HasProperty (O, from) istrue , then - Else,
- Perform ?
DeletePropertyOrThrow (O, to).
- Perform ?
- Set k to k + 1.
- Let from be !
- Set k to len.
- Repeat, while k > (len - actualDeleteCount + itemCount),
- Perform ?
DeletePropertyOrThrow (O, !ToString (𝔽(k - 1))). - Set k to k - 1.
- Perform ?
- Else if itemCount > actualDeleteCount, then
- Set k to (len - actualDeleteCount).
- Repeat, while k > actualStart,
- Let from be !
ToString (𝔽(k + actualDeleteCount - 1)). - Let to be !
ToString (𝔽(k + itemCount - 1)). - If ?
HasProperty (O, from) istrue , then - Else,
- Perform ?
DeletePropertyOrThrow (O, to).
- Perform ?
- Set k to k - 1.
- Let from be !
- Set k to actualStart.
- For each element E of items, do
- Perform ?
Set (O,"length" , 𝔽(len - actualDeleteCount + itemCount),true ). - Return A.
The explicit setting of the
The splice function is intentionally generic; it does not require that its
23.1.3.30 Array.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
An ECMAScript implementation that includes the ECMA-402 Internationalization API must implement the Array.prototype.toLocaleString method as specified in the ECMA-402 specification. If an ECMAScript implementation does not include the ECMA-402 API the following specification of the toLocaleString method is used.
The first edition of ECMA-402 did not include a replacement specification for the Array.prototype.toLocaleString method.
The meanings of the optional parameters to this method are defined in the ECMA-402 specification; implementations that do not include ECMA-402 support must not use those parameter positions for anything else.
When the toLocaleString method is called, the following steps are taken:
- Let array be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (array). - Let separator be the
implementation-defined list-separator String value appropriate for thehost environment 's current locale (such as", " ). - Let R be the empty String.
- Let k be 0.
- Repeat, while k < len,
- If k > 0, then
- Set R to the
string-concatenation of R and separator.
- Set R to the
- Let nextElement be ?
Get (array, !ToString (𝔽(k))). - If nextElement is not
undefined ornull , then- Let S be ?
ToString (?Invoke (nextElement,"toLocaleString" )). - Set R to the
string-concatenation of R and S.
- Let S be ?
- Set k to k + 1.
- If k > 0, then
- Return R.
The elements of the array are converted to Strings using their toLocaleString methods, and these Strings are then concatenated, separated by occurrences of an toString except that it is intended to yield a locale-sensitive result corresponding with conventions of the
The toLocaleString function is intentionally generic; it does not require that its
23.1.3.31 Array.prototype.toString ( )
When the toString method is called, the following steps are taken:
- Let array be ?
ToObject (this value). - Let func be ?
Get (array,"join" ). - If
IsCallable (func) isfalse , set func to the intrinsic function%Object.prototype.toString% . - Return ?
Call (func, array).
The toString function is intentionally generic; it does not require that its
23.1.3.32 Array.prototype.unshift ( ...items )
The arguments are prepended to the start of the array, such that their order within the array is the same as the order in which they appear in the argument list.
When the unshift method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Let len be ?
LengthOfArrayLike (O). - Let argCount be the number of elements in items.
- If argCount > 0, then
- If len + argCount > 253 - 1, throw a
TypeError exception. - Let k be len.
- Repeat, while k > 0,
- Let from be !
ToString (𝔽(k - 1)). - Let to be !
ToString (𝔽(k + argCount - 1)). - Let fromPresent be ?
HasProperty (O, from). - If fromPresent is
true , then - Else,
Assert : fromPresent isfalse .- Perform ?
DeletePropertyOrThrow (O, to).
- Set k to k - 1.
- Let from be !
- Let j be
+0 𝔽. - For each element E of items, do
- If len + argCount > 253 - 1, throw a
- Perform ?
Set (O,"length" , 𝔽(len + argCount),true ). - Return 𝔽(len + argCount).
The unshift method is
The unshift function is intentionally generic; it does not require that its
23.1.3.33 Array.prototype.values ( )
When the values method is called, the following steps are taken:
- Let O be ?
ToObject (this value). - Return
CreateArrayIterator (O,value ).
23.1.3.34 Array.prototype [ @@iterator ] ( )
The initial value of the @@iterator property is
23.1.3.35 Array.prototype [ @@unscopables ]
The initial value of the @@unscopables
- Let unscopableList be
OrdinaryObjectCreate (null ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"at" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"copyWithin" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"entries" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"fill" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"find" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"findIndex" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"flat" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"flatMap" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"includes" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"keys" ,true ). - Perform !
CreateDataPropertyOrThrow (unscopableList,"values" ,true ). - Return unscopableList.
This property has the attributes { [[Writable]]:
The own property names of this object are property names that were not included as standard properties of Array.prototype prior to the ECMAScript 2015 specification. These names are ignored for with statement binding purposes in order to preserve the behaviour of existing code that might use one of these names as a binding in an outer scope that is shadowed by a with statement whose binding object is an Array.
23.1.4 Properties of Array Instances
Array instances are
Array instances have a
23.1.4.1 length
The
The
Reducing the value of the
23.1.5 Array Iterator Objects
An Array Iterator is an object, that represents a specific iteration over some specific Array instance object. There is not a named
23.1.5.1 CreateArrayIterator ( array, kind )
The abstract operation CreateArrayIterator takes arguments array (an Object) and kind (
- Let closure be a new
Abstract Closure with no parameters that captures kind and array and performs the following steps when called:- Let index be 0.
- Repeat,
- If array has a [[TypedArrayName]] internal slot, then
- If
IsDetachedBuffer (array.[[ViewedArrayBuffer]]) istrue , throw aTypeError exception. - Let len be array.[[ArrayLength]].
- If
- Else,
- Let len be ?
LengthOfArrayLike (array).
- Let len be ?
- If index ≥ len, return
undefined . - If kind is
key , perform ?GeneratorYield (CreateIterResultObject (𝔽(index),false )). - Else,
- Let elementKey be !
ToString (𝔽(index)). - Let elementValue be ?
Get (array, elementKey). - If kind is
value , perform ?GeneratorYield (CreateIterResultObject (elementValue,false )). - Else,
Assert : kind iskey+value .- Let result be
CreateArrayFromList (« 𝔽(index), elementValue »). - Perform ?
GeneratorYield (CreateIterResultObject (result,false )).
- Let elementKey be !
- Set index to index + 1.
- If array has a [[TypedArrayName]] internal slot, then
- Return
CreateIteratorFromClosure (closure," ,%ArrayIteratorPrototype% "%ArrayIteratorPrototype% ).
23.1.5.2 The %ArrayIteratorPrototype% Object
The
- has properties that are inherited by all Array Iterator Objects.
- is an
ordinary object . - has a [[Prototype]] internal slot whose value is
%IteratorPrototype% . - has the following properties:
23.1.5.2.1 %ArrayIteratorPrototype% .next ( )
- Return ?
GeneratorResume (this value,empty ," ).%ArrayIteratorPrototype% "
23.1.5.2.2 %ArrayIteratorPrototype% [ @@toStringTag ]
The initial value of the @@toStringTag property is the String value
This property has the attributes { [[Writable]]:
23.2 TypedArray Objects
A TypedArray presents an array-like view of an underlying binary data buffer (
|
|
Element Type | Element Size | Conversion Operation | Description |
|---|---|---|---|---|
|
Int8Array |
|
1 |
|
8-bit two's complement signed |
|
Uint8Array |
|
1 |
|
8-bit unsigned |
|
Uint8ClampedArray |
|
1 |
|
8-bit unsigned |
|
Int16Array |
|
2 |
|
16-bit two's complement signed |
|
Uint16Array |
|
2 |
|
16-bit unsigned |
|
Int32Array |
|
4 |
|
32-bit two's complement signed |
|
Uint32Array |
|
4 |
|
32-bit unsigned |
|
BigInt64Array |
|
8 |
|
64-bit two's complement signed |
|
BigUint64Array |
|
8 |
|
64-bit unsigned |
|
Float32Array |
|
4 | 32-bit IEEE floating point | |
|
Float64Array |
|
8 | 64-bit IEEE floating point |
In the definitions below, references to TypedArray should be replaced with the appropriate
23.2.1 The %TypedArray% Intrinsic Object
The
- is a
constructor function object that all of the TypedArrayconstructor objects inherit from. - along with its corresponding prototype object, provides common properties that are inherited by all TypedArray
constructors and their instances. - does not have a global name or appear as a property of the
global object . - acts as the abstract superclass of the various TypedArray
constructors . - will throw an error when invoked, because it is an abstract class
constructor . The TypedArrayconstructors do not perform asupercall to it.
23.2.1.1 %TypedArray% ( )
The
- Throw a
TypeError exception.
The
23.2.2 Properties of the %TypedArray% Intrinsic Object
The
- has a [[Prototype]] internal slot whose value is
%Function.prototype% . - has a
"name" property whose value is"TypedArray" . - has the following properties:
23.2.2.1 %TypedArray% .from ( source [ , mapfn [ , thisArg ] ] )
When the from method is called, the following steps are taken:
- Let C be the
this value. - If
IsConstructor (C) isfalse , throw aTypeError exception. - If mapfn is
undefined , let mapping befalse . - Else,
- If
IsCallable (mapfn) isfalse , throw aTypeError exception. - Let mapping be
true .
- If
- Let usingIterator be ?
GetMethod (source, @@iterator). - If usingIterator is not
undefined , then- Let values be ?
IterableToList (source, usingIterator). - Let len be the number of elements in values.
- Let targetObj be ?
TypedArrayCreate (C, « 𝔽(len) »). - Let k be 0.
- Repeat, while k < len,
Assert : values is now an emptyList .- Return targetObj.
- Let values be ?
- NOTE: source is not an Iterable so assume it is already an
array-like object . - Let arrayLike be !
ToObject (source). - Let len be ?
LengthOfArrayLike (arrayLike). - Let targetObj be ?
TypedArrayCreate (C, « 𝔽(len) »). - Let k be 0.
- Repeat, while k < len,
- Return targetObj.
23.2.2.2 %TypedArray% .of ( ...items )
When the of method is called, the following steps are taken:
- Let len be the number of elements in items.
- Let C be the
this value. - If
IsConstructor (C) isfalse , throw aTypeError exception. - Let newObj be ?
TypedArrayCreate (C, « 𝔽(len) »). - Let k be 0.
- Repeat, while k < len,
- Return newObj.
23.2.2.3 %TypedArray% .prototype
The initial value of .prototype is the
This property has the attributes { [[Writable]]:
23.2.2.4 get %TypedArray% [ @@species ]
[@@species] is an
- Return the
this value.
The value of the
23.2.3 Properties of the %TypedArray% Prototype Object
The
- has a [[Prototype]] internal slot whose value is
%Object.prototype% . - is
%TypedArray.prototype% . - is an
ordinary object . - does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to TypedArray instance objects.
23.2.3.1 %TypedArray% .prototype.at ( index )
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- Let relativeIndex be ?
ToIntegerOrInfinity (index). - If relativeIndex ≥ 0, then
- Let k be relativeIndex.
- Else,
- Let k be len + relativeIndex.
- If k < 0 or k ≥ len, return
undefined . - Return !
Get (O, !ToString (𝔽(k))).
23.2.3.2 get %TypedArray% .prototype.buffer
.prototype.buffer is an
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- Return buffer.
23.2.3.3 get %TypedArray% .prototype.byteLength
.prototype.byteLength is an
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , return+0 𝔽. - Let size be O.[[ByteLength]].
- Return 𝔽(size).
23.2.3.4 get %TypedArray% .prototype.byteOffset
.prototype.byteOffset is an
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , return+0 𝔽. - Let offset be O.[[ByteOffset]].
- Return 𝔽(offset).
23.2.3.5 %TypedArray% .prototype.constructor
The initial value of .prototype.constructor is
23.2.3.6 %TypedArray% .prototype.copyWithin ( target, start [ , end ] )
The interpretation and use of the arguments of .prototype.copyWithin are the same as for Array.prototype.copyWithin as defined in
When the copyWithin method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- Let relativeTarget be ?
ToIntegerOrInfinity (target). - If relativeTarget is -∞, let to be 0.
- Else if relativeTarget < 0, let to be
max (len + relativeTarget, 0). - Else, let to be
min (relativeTarget, len). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart is -∞, let from be 0.
- Else if relativeStart < 0, let from be
max (len + relativeStart, 0). - Else, let from be
min (relativeStart, len). - If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd is -∞, let final be 0.
- Else if relativeEnd < 0, let final be
max (len + relativeEnd, 0). - Else, let final be
min (relativeEnd, len). - Let count be
min (final - from, len - to). - If count > 0, then
- NOTE: The copying must be performed in a manner that preserves the bit-level encoding of the source data.
- Let buffer be O.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let elementSize be
TypedArrayElementSize (O). - Let byteOffset be O.[[ByteOffset]].
- Let toByteIndex be to × elementSize + byteOffset.
- Let fromByteIndex be from × elementSize + byteOffset.
- Let countBytes be count × elementSize.
- If fromByteIndex < toByteIndex and toByteIndex < fromByteIndex + countBytes, then
- Let direction be -1.
- Set fromByteIndex to fromByteIndex + countBytes - 1.
- Set toByteIndex to toByteIndex + countBytes - 1.
- Else,
- Let direction be 1.
- Repeat, while countBytes > 0,
- Let value be
GetValueFromBuffer (buffer, fromByteIndex,Uint8 ,true ,Unordered ). - Perform
SetValueInBuffer (buffer, toByteIndex,Uint8 , value,true ,Unordered ). - Set fromByteIndex to fromByteIndex + direction.
- Set toByteIndex to toByteIndex + direction.
- Set countBytes to countBytes - 1.
- Let value be
- Return O.
23.2.3.7 %TypedArray% .prototype.entries ( )
When the entries method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Return
CreateArrayIterator (O,key+value ).
23.2.3.8 %TypedArray% .prototype.every ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of .prototype.every are the same as for Array.prototype.every as defined in
When the every method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
true .
This function is not generic. The
23.2.3.9 %TypedArray% .prototype.fill ( value [ , start [ , end ] ] )
The interpretation and use of the arguments of .prototype.fill are the same as for Array.prototype.fill as defined in
When the fill method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If O.[[ContentType]] is
BigInt , set value to ?ToBigInt (value). - Otherwise, set value to ?
ToNumber (value). - Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart is -∞, let k be 0.
- Else if relativeStart < 0, let k be
max (len + relativeStart, 0). - Else, let k be
min (relativeStart, len). - If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd is -∞, let final be 0.
- Else if relativeEnd < 0, let final be
max (len + relativeEnd, 0). - Else, let final be
min (relativeEnd, len). - If
IsDetachedBuffer (O.[[ViewedArrayBuffer]]) istrue , throw aTypeError exception. - Repeat, while k < final,
- Return O.
23.2.3.10 %TypedArray% .prototype.filter ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of .prototype.filter are the same as for Array.prototype.filter as defined in
When the filter method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let kept be a new empty
List . - Let k be 0.
- Let captured be 0.
- Repeat, while k < len,
- Let A be ?
TypedArraySpeciesCreate (O, « 𝔽(captured) »). - Let n be 0.
- For each element e of kept, do
- Return A.
This function is not generic. The
23.2.3.11 %TypedArray% .prototype.find ( predicate [ , thisArg ] )
The interpretation and use of the arguments of .prototype.find are the same as for Array.prototype.find as defined in
When the find method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (predicate) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
undefined .
This function is not generic. The
23.2.3.12 %TypedArray% .prototype.findIndex ( predicate [ , thisArg ] )
The interpretation and use of the arguments of .prototype.findIndex are the same as for Array.prototype.findIndex as defined in
When the findIndex method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (predicate) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
-1 𝔽.
This function is not generic. The
23.2.3.13 %TypedArray% .prototype.forEach ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of .prototype.forEach are the same as for Array.prototype.forEach as defined in
When the forEach method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
undefined .
This function is not generic. The
23.2.3.14 %TypedArray% .prototype.includes ( searchElement [ , fromIndex ] )
The interpretation and use of the arguments of .prototype.includes are the same as for Array.prototype.includes as defined in
When the includes method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If len is 0, return
false . - Let n be ?
ToIntegerOrInfinity (fromIndex). Assert : If fromIndex isundefined , then n is 0.- If n is +∞, return
false . - Else if n is -∞, set n to 0.
- If n ≥ 0, then
- Let k be n.
- Else,
- Let k be len + n.
- If k < 0, set k to 0.
- Repeat, while k < len,
- Let elementK be !
Get (O, !ToString (𝔽(k))). - If
SameValueZero (searchElement, elementK) istrue , returntrue . - Set k to k + 1.
- Let elementK be !
- Return
false .
This function is not generic. The
23.2.3.15 %TypedArray% .prototype.indexOf ( searchElement [ , fromIndex ] )
The interpretation and use of the arguments of .prototype.indexOf are the same as for Array.prototype.indexOf as defined in
When the indexOf method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If len is 0, return
-1 𝔽. - Let n be ?
ToIntegerOrInfinity (fromIndex). Assert : If fromIndex isundefined , then n is 0.- If n is +∞, return
-1 𝔽. - Else if n is -∞, set n to 0.
- If n ≥ 0, then
- Let k be n.
- Else,
- Let k be len + n.
- If k < 0, set k to 0.
- Repeat, while k < len,
- Let kPresent be !
HasProperty (O, !ToString (𝔽(k))). - If kPresent is
true , then- Let elementK be !
Get (O, !ToString (𝔽(k))). - Let same be
IsStrictlyEqual (searchElement, elementK). - If same is
true , return 𝔽(k).
- Let elementK be !
- Set k to k + 1.
- Let kPresent be !
- Return
-1 𝔽.
This function is not generic. The
23.2.3.16 %TypedArray% .prototype.join ( separator )
The interpretation and use of the arguments of .prototype.join are the same as for Array.prototype.join as defined in
When the join method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If separator is
undefined , let sep be the single-element String"," . - Else, let sep be ?
ToString (separator). - Let R be the empty String.
- Let k be 0.
- Repeat, while k < len,
- If k > 0, set R to the
string-concatenation of R and sep. - Let element be !
Get (O, !ToString (𝔽(k))). - If element is
undefined , let next be the empty String; otherwise, let next be !ToString (element). - Set R to the
string-concatenation of R and next. - Set k to k + 1.
- If k > 0, set R to the
- Return R.
This function is not generic. The
23.2.3.17 %TypedArray% .prototype.keys ( )
When the keys method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Return
CreateArrayIterator (O,key ).
23.2.3.18 %TypedArray% .prototype.lastIndexOf ( searchElement [ , fromIndex ] )
The interpretation and use of the arguments of .prototype.lastIndexOf are the same as for Array.prototype.lastIndexOf as defined in
When the lastIndexOf method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If len is 0, return
-1 𝔽. - If fromIndex is present, let n be ?
ToIntegerOrInfinity (fromIndex); else let n be len - 1. - If n is -∞, return
-1 𝔽. - If n ≥ 0, then
- Let k be
min (n, len - 1).
- Let k be
- Else,
- Let k be len + n.
- Repeat, while k ≥ 0,
- Let kPresent be !
HasProperty (O, !ToString (𝔽(k))). - If kPresent is
true , then- Let elementK be !
Get (O, !ToString (𝔽(k))). - Let same be
IsStrictlyEqual (searchElement, elementK). - If same is
true , return 𝔽(k).
- Let elementK be !
- Set k to k - 1.
- Let kPresent be !
- Return
-1 𝔽.
This function is not generic. The
23.2.3.19 get %TypedArray% .prototype.length
.prototype.length is an
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has [[ViewedArrayBuffer]] and [[ArrayLength]] internal slots.- Let buffer be O.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , return+0 𝔽. - Let length be O.[[ArrayLength]].
- Return 𝔽(length).
This function is not generic. The
23.2.3.20 %TypedArray% .prototype.map ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of .prototype.map are the same as for Array.prototype.map as defined in
When the map method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let A be ?
TypedArraySpeciesCreate (O, « 𝔽(len) »). - Let k be 0.
- Repeat, while k < len,
- Return A.
This function is not generic. The
23.2.3.21 %TypedArray% .prototype.reduce ( callbackfn [ , initialValue ] )
The interpretation and use of the arguments of .prototype.reduce are the same as for Array.prototype.reduce as defined in
When the reduce method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - If len = 0 and initialValue is not present, throw a
TypeError exception. - Let k be 0.
- Let accumulator be
undefined . - If initialValue is present, then
- Set accumulator to initialValue.
- Else,
- Repeat, while k < len,
- Return accumulator.
This function is not generic. The
23.2.3.22 %TypedArray% .prototype.reduceRight ( callbackfn [ , initialValue ] )
The interpretation and use of the arguments of .prototype.reduceRight are the same as for Array.prototype.reduceRight as defined in
When the reduceRight method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - If len is 0 and initialValue is not present, throw a
TypeError exception. - Let k be len - 1.
- Let accumulator be
undefined . - If initialValue is present, then
- Set accumulator to initialValue.
- Else,
- Repeat, while k ≥ 0,
- Return accumulator.
This function is not generic. The
23.2.3.23 %TypedArray% .prototype.reverse ( )
The interpretation and use of the arguments of .prototype.reverse are the same as for Array.prototype.reverse as defined in
When the reverse method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- Let middle be
floor (len / 2). - Let lower be 0.
- Repeat, while lower ≠ middle,
- Return O.
This function is not generic. The
23.2.3.24 %TypedArray% .prototype.set ( source [ , offset ] )
.prototype.set is a function whose behaviour differs based upon the type of its first argument.
This function is not generic. The
Sets multiple values in this TypedArray, reading the values from source. The optional offset value indicates the first element index in this TypedArray where values are written. If omitted, it is assumed to be 0.
When the set method is called, the following steps are taken:
- Let target be the
this value. - Perform ?
RequireInternalSlot (target, [[TypedArrayName]]). Assert : target has a [[ViewedArrayBuffer]] internal slot.- Let targetOffset be ?
ToIntegerOrInfinity (offset). - If targetOffset < 0, throw a
RangeError exception. - If source is an Object that has a [[TypedArrayName]] internal slot, then
- Perform ?
SetTypedArrayFromTypedArray (target, targetOffset, source).
- Perform ?
- Else,
- Perform ?
SetTypedArrayFromArrayLike (target, targetOffset, source).
- Perform ?
- Return
undefined .
23.2.3.24.1 SetTypedArrayFromTypedArray ( target, targetOffset, source )
The abstract operation SetTypedArrayFromTypedArray takes arguments target (a TypedArray), targetOffset (a non-negative
- Let targetBuffer be target.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (targetBuffer) istrue , throw aTypeError exception. - Let targetLength be target.[[ArrayLength]].
- Let srcBuffer be source.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (srcBuffer) istrue , throw aTypeError exception. - Let targetType be
TypedArrayElementType (target). - Let targetElementSize be
TypedArrayElementSize (target). - Let targetByteOffset be target.[[ByteOffset]].
- Let srcType be
TypedArrayElementType (source). - Let srcElementSize be
TypedArrayElementSize (source). - Let srcLength be source.[[ArrayLength]].
- Let srcByteOffset be source.[[ByteOffset]].
- If targetOffset is +∞, throw a
RangeError exception. - If srcLength + targetOffset > targetLength, throw a
RangeError exception. - If target.[[ContentType]] ≠ source.[[ContentType]], throw a
TypeError exception. - If both
IsSharedArrayBuffer (srcBuffer) andIsSharedArrayBuffer (targetBuffer) aretrue , then- If srcBuffer.[[ArrayBufferData]] and targetBuffer.[[ArrayBufferData]] are the same
Shared Data Block values, let same betrue ; else let same befalse .
- If srcBuffer.[[ArrayBufferData]] and targetBuffer.[[ArrayBufferData]] are the same
- Else, let same be
SameValue (srcBuffer, targetBuffer). - If same is
true , then- Let srcByteLength be source.[[ByteLength]].
- Set srcBuffer to ?
.CloneArrayBuffer (srcBuffer, srcByteOffset, srcByteLength,%ArrayBuffer% ) - NOTE:
%ArrayBuffer% is used to clone srcBuffer because is it known to not have any observable side-effects. - Let srcByteIndex be 0.
- Else, let srcByteIndex be srcByteOffset.
- Let targetByteIndex be targetOffset × targetElementSize + targetByteOffset.
- Let limit be targetByteIndex + targetElementSize × srcLength.
- If srcType is the same as targetType, then
- NOTE: If srcType and targetType are the same, the transfer must be performed in a manner that preserves the bit-level encoding of the source data.
- Repeat, while targetByteIndex < limit,
- Let value be
GetValueFromBuffer (srcBuffer, srcByteIndex,Uint8 ,true ,Unordered ). - Perform
SetValueInBuffer (targetBuffer, targetByteIndex,Uint8 , value,true ,Unordered ). - Set srcByteIndex to srcByteIndex + 1.
- Set targetByteIndex to targetByteIndex + 1.
- Let value be
- Else,
- Repeat, while targetByteIndex < limit,
- Let value be
GetValueFromBuffer (srcBuffer, srcByteIndex, srcType,true ,Unordered ). - Perform
SetValueInBuffer (targetBuffer, targetByteIndex, targetType, value,true ,Unordered ). - Set srcByteIndex to srcByteIndex + srcElementSize.
- Set targetByteIndex to targetByteIndex + targetElementSize.
- Let value be
- Repeat, while targetByteIndex < limit,
- Return
unused .
23.2.3.24.2 SetTypedArrayFromArrayLike ( target, targetOffset, source )
The abstract operation SetTypedArrayFromArrayLike takes arguments target (a TypedArray), targetOffset (a non-negative
- Let targetBuffer be target.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (targetBuffer) istrue , throw aTypeError exception. - Let targetLength be target.[[ArrayLength]].
- Let targetElementSize be
TypedArrayElementSize (target). - Let targetType be
TypedArrayElementType (target). - Let targetByteOffset be target.[[ByteOffset]].
- Let src be ?
ToObject (source). - Let srcLength be ?
LengthOfArrayLike (src). - If targetOffset is +∞, throw a
RangeError exception. - If srcLength + targetOffset > targetLength, throw a
RangeError exception. - Let targetByteIndex be targetOffset × targetElementSize + targetByteOffset.
- Let k be 0.
- Let limit be targetByteIndex + targetElementSize × srcLength.
- Repeat, while targetByteIndex < limit,
- Let Pk be !
ToString (𝔽(k)). - Let value be ?
Get (src, Pk). - If target.[[ContentType]] is
BigInt , set value to ?ToBigInt (value). - Otherwise, set value to ?
ToNumber (value). - If
IsDetachedBuffer (targetBuffer) istrue , throw aTypeError exception. - Perform
SetValueInBuffer (targetBuffer, targetByteIndex, targetType, value,true ,Unordered ). - Set k to k + 1.
- Set targetByteIndex to targetByteIndex + targetElementSize.
- Let Pk be !
- Return
unused .
23.2.3.25 %TypedArray% .prototype.slice ( start, end )
The interpretation and use of the arguments of .prototype.slice are the same as for Array.prototype.slice as defined in
When the slice method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- Let relativeStart be ?
ToIntegerOrInfinity (start). - If relativeStart is -∞, let k be 0.
- Else if relativeStart < 0, let k be
max (len + relativeStart, 0). - Else, let k be
min (relativeStart, len). - If end is
undefined , let relativeEnd be len; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd is -∞, let final be 0.
- Else if relativeEnd < 0, let final be
max (len + relativeEnd, 0). - Else, let final be
min (relativeEnd, len). - Let count be
max (final - k, 0). - Let A be ?
TypedArraySpeciesCreate (O, « 𝔽(count) »). - If count > 0, then
- If
IsDetachedBuffer (O.[[ViewedArrayBuffer]]) istrue , throw aTypeError exception. - Let srcType be
TypedArrayElementType (O). - Let targetType be
TypedArrayElementType (A). - If srcType is different from targetType, then
- Else,
- Let srcBuffer be O.[[ViewedArrayBuffer]].
- Let targetBuffer be A.[[ViewedArrayBuffer]].
- Let elementSize be
TypedArrayElementSize (O). - NOTE: If srcType and targetType are the same, the transfer must be performed in a manner that preserves the bit-level encoding of the source data.
- Let srcByteOffset be O.[[ByteOffset]].
- Let targetByteIndex be A.[[ByteOffset]].
- Let srcByteIndex be (k × elementSize) + srcByteOffset.
- Let limit be targetByteIndex + count × elementSize.
- Repeat, while targetByteIndex < limit,
- Let value be
GetValueFromBuffer (srcBuffer, srcByteIndex,Uint8 ,true ,Unordered ). - Perform
SetValueInBuffer (targetBuffer, targetByteIndex,Uint8 , value,true ,Unordered ). - Set srcByteIndex to srcByteIndex + 1.
- Set targetByteIndex to targetByteIndex + 1.
- Let value be
- If
- Return A.
This function is not generic. The
23.2.3.26 %TypedArray% .prototype.some ( callbackfn [ , thisArg ] )
The interpretation and use of the arguments of .prototype.some are the same as for Array.prototype.some as defined in
When the some method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Let len be O.[[ArrayLength]].
- If
IsCallable (callbackfn) isfalse , throw aTypeError exception. - Let k be 0.
- Repeat, while k < len,
- Return
false .
This function is not generic. The
23.2.3.27 %TypedArray% .prototype.sort ( comparefn )
.prototype.sort is a distinct function that, except as described below, implements the same requirements as those of Array.prototype.sort as defined in .prototype.sort specification may be optimized with the knowledge that the
This function is not generic. The
The following steps are performed:
- If comparefn is not
undefined andIsCallable (comparefn) isfalse , throw aTypeError exception. - Let obj be the
this value. - Perform ?
ValidateTypedArray (obj). - Let buffer be obj.[[ViewedArrayBuffer]].
- Let len be obj.[[ArrayLength]].
- NOTE: The following closure performs a numeric comparison rather than the string comparison used in
23.1.3.28 . - Let SortCompare be a new
Abstract Closure with parameters (x, y) that captures comparefn and buffer and performs the following steps when called:Assert : BothType (x) andType (y) are Number or both are BigInt.- If comparefn is not
undefined , then- Let v be ?
ToNumber (?Call (comparefn,undefined , « x, y »)). - If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - If v is
NaN , return+0 𝔽. - Return v.
- Let v be ?
- If x and y are both
NaN , return+0 𝔽. - If x is
NaN , return1 𝔽. - If y is
NaN , return-1 𝔽. - If x < y, return
-1 𝔽. - If x > y, return
1 𝔽. - If x is
-0 𝔽 and y is+0 𝔽, return-1 𝔽. - If x is
+0 𝔽 and y is-0 𝔽, return1 𝔽. - Return
+0 𝔽.
- Return ?
SortIndexedProperties (obj, len, SortCompare).
Because
23.2.3.28 %TypedArray% .prototype.subarray ( begin, end )
Returns a new TypedArray whose element type is the same as this TypedArray and whose ArrayBuffer is the same as the ArrayBuffer of this TypedArray, referencing the elements at begin, inclusive, up to end, exclusive. If either begin or end is negative, it refers to an index from the end of the array, as opposed to from the beginning.
When the subarray method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- Let srcLength be O.[[ArrayLength]].
- Let relativeBegin be ?
ToIntegerOrInfinity (begin). - If relativeBegin is -∞, let beginIndex be 0.
- Else if relativeBegin < 0, let beginIndex be
max (srcLength + relativeBegin, 0). - Else, let beginIndex be
min (relativeBegin, srcLength). - If end is
undefined , let relativeEnd be srcLength; else let relativeEnd be ?ToIntegerOrInfinity (end). - If relativeEnd is -∞, let endIndex be 0.
- Else if relativeEnd < 0, let endIndex be
max (srcLength + relativeEnd, 0). - Else, let endIndex be
min (relativeEnd, srcLength). - Let newLength be
max (endIndex - beginIndex, 0). - Let elementSize be
TypedArrayElementSize (O). - Let srcByteOffset be O.[[ByteOffset]].
- Let beginByteOffset be srcByteOffset + beginIndex × elementSize.
- Let argumentsList be « buffer, 𝔽(beginByteOffset), 𝔽(newLength) ».
- Return ?
TypedArraySpeciesCreate (O, argumentsList).
This function is not generic. The
23.2.3.29 %TypedArray% .prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] )
.prototype.toLocaleString is a distinct function that implements the same algorithm as Array.prototype.toLocaleString as defined in
This function is not generic.
If the ECMAScript implementation includes the ECMA-402 Internationalization API this function is based upon the algorithm for Array.prototype.toLocaleString that is in the ECMA-402 specification.
23.2.3.30 %TypedArray% .prototype.toString ( )
The initial value of the
23.2.3.31 %TypedArray% .prototype.values ( )
When the values method is called, the following steps are taken:
- Let O be the
this value. - Perform ?
ValidateTypedArray (O). - Return
CreateArrayIterator (O,value ).
23.2.3.32 %TypedArray% .prototype [ @@iterator ] ( )
The initial value of the @@iterator property is
23.2.3.33 get %TypedArray% .prototype [ @@toStringTag ]
.prototype[@@toStringTag] is an
This property has the attributes { [[Enumerable]]:
The initial value of the
23.2.4 Abstract Operations for TypedArray Objects
23.2.4.1 TypedArraySpeciesCreate ( exemplar, argumentList )
The abstract operation TypedArraySpeciesCreate takes arguments exemplar (a TypedArray) and argumentList and returns either a
- Let defaultConstructor be the intrinsic object listed in column one of
Table 71 for exemplar.[[TypedArrayName]]. - Let constructor be ?
SpeciesConstructor (exemplar, defaultConstructor). - Let result be ?
TypedArrayCreate (constructor, argumentList). Assert : result has [[TypedArrayName]] and [[ContentType]] internal slots.- If result.[[ContentType]] ≠ exemplar.[[ContentType]], throw a
TypeError exception. - Return result.
23.2.4.2 TypedArrayCreate ( constructor, argumentList )
The abstract operation TypedArrayCreate takes arguments constructor and argumentList and returns either a
- Let newTypedArray be ?
Construct (constructor, argumentList). - Perform ?
ValidateTypedArray (newTypedArray). - If argumentList is a
List of a single Number, then- If newTypedArray.[[ArrayLength]] < ℝ(argumentList[0]), throw a
TypeError exception.
- If newTypedArray.[[ArrayLength]] < ℝ(argumentList[0]), throw a
- Return newTypedArray.
23.2.4.3 ValidateTypedArray ( O )
The abstract operation ValidateTypedArray takes argument O and returns either a
- Perform ?
RequireInternalSlot (O, [[TypedArrayName]]). Assert : O has a [[ViewedArrayBuffer]] internal slot.- Let buffer be O.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Return
unused .
23.2.4.4 TypedArrayElementSize ( O )
23.2.4.5 TypedArrayElementType ( O )
The abstract operation TypedArrayElementType takes argument O (a TypedArray) and returns a
- Return the Element Type value specified in
Table 71 for O.[[TypedArrayName]].
23.2.5 The TypedArray Constructors
Each TypedArray
- is an intrinsic object that has the structure described below, differing only in the name used as the
constructor name instead of TypedArray, inTable 71 . - is a function whose behaviour differs based upon the number and types of its arguments. The actual behaviour of a call of TypedArray depends upon the number and kind of arguments that are passed to it.
- is not intended to be called as a function and will throw an exception when called in that manner.
- may be used as the value of an
extendsclause of a class definition. Subclassconstructors that intend to inherit the specified TypedArray behaviour must include asupercall to the TypedArrayconstructor to create and initialize the subclass instance with the internal state necessary to support the%TypedArray% .prototypebuilt-in methods. - has a
"length" property whose value is3 𝔽.
23.2.5.1 TypedArray ( ...args )
Each TypedArray
- If NewTarget is
undefined , throw aTypeError exception. - Let constructorName be the String value of the
Constructor Name value specified inTable 71 for this TypedArrayconstructor . - Let proto be
"%TypedArray.prototype%". - Let numberOfArgs be the number of elements in args.
- If numberOfArgs = 0, then
- Return ?
AllocateTypedArray (constructorName, NewTarget, proto, 0).
- Return ?
- Else,
- Let firstArgument be args[0].
- If
Type (firstArgument) is Object, then- Let O be ?
AllocateTypedArray (constructorName, NewTarget, proto). - If firstArgument has a [[TypedArrayName]] internal slot, then
- Perform ?
InitializeTypedArrayFromTypedArray (O, firstArgument).
- Perform ?
- Else if firstArgument has an [[ArrayBufferData]] internal slot, then
- If numberOfArgs > 1, let byteOffset be args[1]; else let byteOffset be
undefined . - If numberOfArgs > 2, let length be args[2]; else let length be
undefined . - Perform ?
InitializeTypedArrayFromArrayBuffer (O, firstArgument, byteOffset, length).
- If numberOfArgs > 1, let byteOffset be args[1]; else let byteOffset be
- Else,
Assert :Type (firstArgument) is Object and firstArgument does not have either a [[TypedArrayName]] or an [[ArrayBufferData]] internal slot.- Let usingIterator be ?
GetMethod (firstArgument, @@iterator). - If usingIterator is not
undefined , then- Let values be ?
IterableToList (firstArgument, usingIterator). - Perform ?
InitializeTypedArrayFromList (O, values).
- Let values be ?
- Else,
- NOTE: firstArgument is not an Iterable so assume it is already an
array-like object . - Perform ?
InitializeTypedArrayFromArrayLike (O, firstArgument).
- NOTE: firstArgument is not an Iterable so assume it is already an
- Return O.
- Let O be ?
- Else,
Assert : firstArgument is not an Object.- Let elementLength be ?
ToIndex (firstArgument). - Return ?
AllocateTypedArray (constructorName, NewTarget, proto, elementLength).
23.2.5.1.1 AllocateTypedArray ( constructorName, newTarget, defaultProto [ , length ] )
The abstract operation AllocateTypedArray takes arguments constructorName (a String which is the name of a TypedArray
- Let proto be ?
GetPrototypeFromConstructor (newTarget, defaultProto). - Let obj be
IntegerIndexedObjectCreate (proto). Assert : obj.[[ViewedArrayBuffer]] isundefined .- Set obj.[[TypedArrayName]] to constructorName.
- If constructorName is
"BigInt64Array" or"BigUint64Array" , set obj.[[ContentType]] toBigInt . - Otherwise, set obj.[[ContentType]] to
Number . - If length is not present, then
- Set obj.[[ByteLength]] to 0.
- Set obj.[[ByteOffset]] to 0.
- Set obj.[[ArrayLength]] to 0.
- Else,
- Perform ?
AllocateTypedArrayBuffer (obj, length).
- Perform ?
- Return obj.
23.2.5.1.2 InitializeTypedArrayFromTypedArray ( O, srcArray )
The abstract operation InitializeTypedArrayFromTypedArray takes arguments O (a TypedArray) and srcArray (a TypedArray) and returns either a
- Let srcData be srcArray.[[ViewedArrayBuffer]].
- If
IsDetachedBuffer (srcData) istrue , throw aTypeError exception. - Let elementType be
TypedArrayElementType (O). - Let elementSize be
TypedArrayElementSize (O). - Let srcType be
TypedArrayElementType (srcArray). - Let srcElementSize be
TypedArrayElementSize (srcArray). - Let srcByteOffset be srcArray.[[ByteOffset]].
- Let elementLength be srcArray.[[ArrayLength]].
- Let byteLength be elementSize × elementLength.
- If
IsSharedArrayBuffer (srcData) isfalse , then- Let bufferConstructor be ?
SpeciesConstructor (srcData,%ArrayBuffer% ).
- Let bufferConstructor be ?
- Else,
- Let bufferConstructor be
%ArrayBuffer% .
- Let bufferConstructor be
- If elementType is the same as srcType, then
- Let data be ?
CloneArrayBuffer (srcData, srcByteOffset, byteLength, bufferConstructor).
- Let data be ?
- Else,
- Let data be ?
AllocateArrayBuffer (bufferConstructor, byteLength). - If
IsDetachedBuffer (srcData) istrue , throw aTypeError exception. - If srcArray.[[ContentType]] ≠ O.[[ContentType]], throw a
TypeError exception. - Let srcByteIndex be srcByteOffset.
- Let targetByteIndex be 0.
- Let count be elementLength.
- Repeat, while count > 0,
- Let value be
GetValueFromBuffer (srcData, srcByteIndex, srcType,true ,Unordered ). - Perform
SetValueInBuffer (data, targetByteIndex, elementType, value,true ,Unordered ). - Set srcByteIndex to srcByteIndex + srcElementSize.
- Set targetByteIndex to targetByteIndex + elementSize.
- Set count to count - 1.
- Let value be
- Let data be ?
- Set O.[[ViewedArrayBuffer]] to data.
- Set O.[[ByteLength]] to byteLength.
- Set O.[[ByteOffset]] to 0.
- Set O.[[ArrayLength]] to elementLength.
- Return
unused .
23.2.5.1.3 InitializeTypedArrayFromArrayBuffer ( O, buffer, byteOffset, length )
The abstract operation InitializeTypedArrayFromArrayBuffer takes arguments O (a TypedArray), buffer (an ArrayBuffer or a SharedArrayBuffer), byteOffset (an
- Let elementSize be
TypedArrayElementSize (O). - Let offset be ?
ToIndex (byteOffset). - If offset
modulo elementSize ≠ 0, throw aRangeError exception. - If length is not
undefined , then- Let newLength be ?
ToIndex (length).
- Let newLength be ?
- If
IsDetachedBuffer (buffer) istrue , throw aTypeError exception. - Let bufferByteLength be buffer.[[ArrayBufferByteLength]].
- If length is
undefined , then- If bufferByteLength
modulo elementSize ≠ 0, throw aRangeError exception. - Let newByteLength be bufferByteLength - offset.
- If newByteLength < 0, throw a
RangeError exception.
- If bufferByteLength
- Else,
- Let newByteLength be newLength × elementSize.
- If offset + newByteLength > bufferByteLength, throw a
RangeError exception.
- Set O.[[ViewedArrayBuffer]] to buffer.
- Set O.[[ByteLength]] to newByteLength.
- Set O.[[ByteOffset]] to offset.
- Set O.[[ArrayLength]] to newByteLength / elementSize.
- Return
unused .
23.2.5.1.4 InitializeTypedArrayFromList ( O, values )
The abstract operation InitializeTypedArrayFromList takes arguments O (a TypedArray) and values (a
- Let len be the number of elements in values.
- Perform ?
AllocateTypedArrayBuffer (O, len). - Let k be 0.
- Repeat, while k < len,
Assert : values is now an emptyList .- Return
unused .
23.2.5.1.5 InitializeTypedArrayFromArrayLike ( O, arrayLike )
The abstract operation InitializeTypedArrayFromArrayLike takes arguments O (a TypedArray) and arrayLike (an Object, but not a TypedArray or an ArrayBuffer) and returns either a
- Let len be ?
LengthOfArrayLike (arrayLike). - Perform ?
AllocateTypedArrayBuffer (O, len). - Let k be 0.
- Repeat, while k < len,
- Return
unused .
23.2.5.1.6 AllocateTypedArrayBuffer ( O, length )
The abstract operation AllocateTypedArrayBuffer takes arguments O (a TypedArray) and length (a non-negative
Assert : O.[[ViewedArrayBuffer]] isundefined .- Let elementSize be
TypedArrayElementSize (O). - Let byteLength be elementSize × length.
- Let data be ?
.AllocateArrayBuffer (%ArrayBuffer% , byteLength) - Set O.[[ViewedArrayBuffer]] to data.
- Set O.[[ByteLength]] to byteLength.
- Set O.[[ByteOffset]] to 0.
- Set O.[[ArrayLength]] to length.
- Return
unused .
23.2.6 Properties of the TypedArray Constructors
Each TypedArray
- has a [[Prototype]] internal slot whose value is
%TypedArray% . - has a
"name" property whose value is the String value of theconstructor name specified for it inTable 71 . - has the following properties:
23.2.6.1 TypedArray.BYTES_PER_ELEMENT
The value of TypedArray.BYTES_PER_ELEMENT is the Element Size value specified in
This property has the attributes { [[Writable]]:
23.2.6.2 TypedArray.prototype
The initial value of TypedArray.prototype is the corresponding TypedArray prototype intrinsic object (
This property has the attributes { [[Writable]]:
23.2.7 Properties of the TypedArray Prototype Objects
Each TypedArray prototype object:
- has a [[Prototype]] internal slot whose value is
%TypedArray.prototype% . - is an
ordinary object . - does not have a [[ViewedArrayBuffer]] or any other of the internal slots that are specific to TypedArray instance objects.
23.2.7.1 TypedArray.prototype.BYTES_PER_ELEMENT
The value of TypedArray.prototype.BYTES_PER_ELEMENT is the Element Size value specified in
This property has the attributes { [[Writable]]:
23.2.7.2 TypedArray.prototype.constructor
The initial value of a TypedArray.prototype.constructor is the corresponding
23.2.8 Properties of TypedArray Instances
TypedArray instances are