ECMA-262 (12th Edition) ECMAScript 2021 Language Specification — page 33

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%

.

NOTE

When the

concat

concat

method is called with zero or more arguments, it returns an array containing the array elements of

the object followed by the array elements of each argument.

The following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

A

be ?

ArraySpeciesCreate

(

O

, 0).

3. Let

n

be 0.

4. Prepend

O

to

items

.

5. For each element

E

of

items

, do

a. Let

spreadable

be ?

IsConcatSpreadable

(

E

).

b. If

spreadable

is

true

, then

i. Let

k

be 0.

ii. Let

len

be ?

LengthOfArrayLike

(

E

).

iii. If

n

+

len

> 2

53

- 1, throw a

TypeError

exception.

iv. Repeat, while

k

<

len

,

1. Let

P

be !

ToString

k

)).

2. Let

exists

be ?

HasProperty

(

E

,

P

).

3. If

exists

is

true

, then

a. Let

subElement

be ?

Get

(

E

,

P

).

b. Perform ?

CreateDataPropertyOrThrow

(

A

, !

ToString

(

(

n

)),

subElement

).

4. Set

n

to

n

+ 1.

5. Set

k

to

k

+ 1.

c. Else,

i. NOTE:

E

is added as a single item rather than spread.

ii. If

n

2

53

- 1, throw a

TypeError

exception.

iii. Perform ?

CreateDataPropertyOrThrow

(

A

, !

ToString

(

(

n

)),

E

).

iv. Set

n

to

n

+ 1.

6. Perform ?

Set

(

A

,

"length"

,

(

n

),

true

).

7. Return

A

.

The

"length"

property of the

concat

concat

method is

1

𝔽

.

The Array prototype object is specified to be an

Array exotic object

to ensure compatibility with

ECMAScript code that was created prior to the ECMAScript 2015 specification.

23.1.3.1 Array.prototype.concat ( ...

items

)

637

NOTE 1

NOTE 2

The abstract operation IsConcatSpreadable takes argument

O

. It performs the following steps when called:

1. If

Type

(

O

) is not Object, return

false

.

2. Let

spreadable

be ?

Get

(

O

,

@@isConcatSpreadable

).

3. If

spreadable

is not

undefined

, return !

ToBoolean

(

spreadable

).

4. Return ?

IsArray

(

O

).

The initial value of

Array.prototype.constructor

Array.prototype.constructor

is

%Array%

.

The

copyWithin

copyWithin

method takes up to three arguments

target

,

start

and

end

.

NOTE 1

The following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. Let

relativeTarget

be ?

ToIntegerOrInfinity

(

target

).

4. If

relativeTarget

is -

, let

to

be 0.

5. Else if

relativeTarget

< 0, let

to

be

max

(

len

+

relativeTarget

, 0).

6. Else, let

to

be

min

(

relativeTarget

,

len

).

7. Let

relativeStart

be ?

ToIntegerOrInfinity

(

start

).

8. If

relativeStart

is -

, let

from

be 0.

9. Else if

relativeStart

< 0, let

from

be

max

(

len

+

relativeStart

, 0).

10. Else, let

from

be

min

(

relativeStart

,

len

).

11. If

end

is

undefined

, let

relativeEnd

be

len

; else let

relativeEnd

be ?

ToIntegerOrInfinity

(

end

).

12. If

relativeEnd

is -

, let

final

be 0.

13. Else if

relativeEnd

< 0, let

final

be

max

(

len

+

relativeEnd

, 0).

14. Else, let

final

be

min

(

relativeEnd

,

len

).

15. Let

count

be

min

(

final

-

from

,

len

-

to

).

16. If

from

<

to

and

to

<

from

+

count

, then

a. Let

direction

be -1.

b. Set

from

to

from

+

count

- 1.

The explicit setting of the

"length"

property in step

6

is necessary to ensure that its value is

correct in situations where the trailing elements of the result Array are not present.

The

concat

concat

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

The

end

argument is optional with the length of the

this

value as its default value. If

target

is

negative, it is treated as

length

+

target

where

length

is the length of the array. If

start

is negative, it

is treated as

length

+

start

. If

end

is negative, it is treated as

length

+

end

.

23.1.3.1.1 IsConcatSpreadable (

O

)

23.1.3.2 Array.prototype.constructor

23.1.3.3 Array.prototype.copyWithin (

target

,

start

[ ,

end

] )

638

c. Set

to

to

to

+

count

- 1.

17. Else,

a. Let

direction

be 1.

18. Repeat, while

count

> 0,

a. Let

fromKey

be !

ToString

(

(

from

)).

b. Let

toKey

be !

ToString

(

(

to

)).

c. Let

fromPresent

be ?

HasProperty

(

O

,

fromKey

).

d. If

fromPresent

is

true

, then

i. Let

fromVal

be ?

Get

(

O

,

fromKey

).

ii. Perform ?

Set

(

O

,

toKey

,

fromVal

,

true

).

e. Else,

i.

Assert

:

fromPresent

is

false

.

ii. Perform ?

DeletePropertyOrThrow

(

O

,

toKey

).

f. Set

from

to

from

+

direction

.

g. Set

to

to

to

+

direction

.

h. Set

count

to

count

- 1.

19. Return

O

.

NOTE 2

The following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Return

CreateArrayIterator

(

O

,

key+value

).

The

copyWithin

copyWithin

function is intentionally generic; it does not require that its

this

value be an

Array object. Therefore it can be transferred to other kinds of objects for use as a method.

23.1.3.4 Array.prototype.entries ( )

23.1.3.5 Array.prototype.every (

callbackfn

[ ,

thisArg

] )

639

NOTE 1

When the

every

every

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

IsCallable

(

callbackfn

) is

false

, throw a

TypeError

exception.

4. Let

k

be 0.

5. Repeat, while

k

<

len

,

a. Let

Pk

be !

ToString

(

(

k

)).

b. Let

kPresent

be ?

HasProperty

(

O

,

Pk

).

c. If

kPresent

is

true

, then

i. Let

kValue

be ?

Get

(

O

,

Pk

).

ii. Let

testResult

be !

ToBoolean

(?

Call

(

callbackfn

,

thisArg

, «

kValue

k

),

O

»)).

iii. If

testResult

is

false

, return

false

.

d. Set

k

to

k

+ 1.

6. Return

true

.

NOTE 2

The

fill

fill

method takes up to three arguments

value

,

start

and

end

.

callbackfn

should be a function that accepts three arguments and returns a value that is coercible

to a Boolean value.

every

every

calls

callbackfn

once for each element present in the array, in

ascending order, until it finds one where

callbackfn

returns

false

. If such an element is found,

every

every

immediately returns

false

. Otherwise, if

callbackfn

returned

true

for all elements,

every

every

will return

true

.

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

this

value for each invocation of

callbackfn

. If it is not provided,

undefined

is used instead.

callbackfn

is called with three arguments: the value of the element, the index of the element, and

the object being traversed.

every

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

every

is set before the first call to

callbackfn

. Elements

which are appended to the array after the call to

every

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

every

visits them; elements that are deleted after the call to

every

every

begins and before

being visited are not visited.

every

every

acts like the "for all" quantifier in mathematics. In particular,

for an empty array, it returns

true

.

The

every

every

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

23.1.3.6 Array.prototype.fill (

value

[ ,

start

[ ,

end

] ] )

640

NOTE 1

The following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. Let

relativeStart

be ?

ToIntegerOrInfinity

(

start

).

4. If

relativeStart

is -

, let

k

be 0.

5. Else if

relativeStart

< 0, let

k

be

max

(

len

+

relativeStart

, 0).

6. Else, let

k

be

min

(

relativeStart

,

len

).

7. If

end

is

undefined

, let

relativeEnd

be

len

; else let

relativeEnd

be ?

ToIntegerOrInfinity

(

end

).

8. If

relativeEnd

is -

, let

final

be 0.

9. Else if

relativeEnd

< 0, let

final

be

max

(

len

+

relativeEnd

, 0).

10. Else, let

final

be

min

(

relativeEnd

,

len

).

11. Repeat, while

k

<

final

,

a. Let

Pk

be !

ToString

(

(

k

)).

b. Perform ?

Set

(

O

,

Pk

,

value

,

true

).

c. Set

k

to

k

+ 1.

12. Return

O

.

NOTE 2

NOTE 1

The

start

and

end

arguments are optional with default values of 0 and the length of the

this

value.

If

start

is negative, it is treated as

length

+

start

where

length

is the length of the array. If

end

is

negative, it is treated as

length

+

end

.

The

fill

fill

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

callbackfn

should be a function that accepts three arguments and returns a value that is coercible

to a Boolean value.

filter

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

true

.

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

this

value for each invocation of

callbackfn

. If it is not provided,

undefined

is used instead.

callbackfn

is called with three arguments: the value of the element, the index of the element, and

the object being traversed.

filter

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

filter

is set before the first call to

callbackfn

. Elements

which are appended to the array after the call to

filter

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

filter

visits them; elements that are deleted after the call to

filter

filter

begins and

before being visited are not visited.

23.1.3.7 Array.prototype.filter (

callbackfn

[ ,

thisArg

] )

641

When the

filter

filter

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

IsCallable

(

callbackfn

) is

false

, throw a

TypeError

exception.

4. Let

A

be ?

ArraySpeciesCreate

(

O

, 0).

5. Let

k

be 0.

6. Let

to

be 0.

7. Repeat, while

k

<

len

,

a. Let

Pk

be !

ToString

(

(

k

)).

b. Let

kPresent

be ?

HasProperty

(

O

,

Pk

).

c. If

kPresent

is

true

, then

i. Let

kValue

be ?

Get

(

O

,

Pk

).

ii. Let

selected

be !

ToBoolean

(?

Call

(

callbackfn

,

thisArg

, «

kValue

(

k

),

O

»)).

iii. If

selected

is

true

, then

1. Perform ?

CreateDataPropertyOrThrow

(

A

, !

ToString

(

(

to

)),

kValue

).

2. Set

to

to

to

+ 1.

d. Set

k

to

k

+ 1.

8. Return

A

.

NOTE 2

The

find

find

method is called with one or two arguments,

predicate

and

thisArg

.

NOTE 1

When the

find

find

method is called, the following steps are taken:

The

filter

filter

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

predicate

should be a function that accepts three arguments and returns a value that is coercible to

a Boolean value.

find

find

calls

predicate

once for each element of the array, in ascending order, until

it finds one where

predicate

returns

true

. If such an element is found,

find

find

immediately returns

that element value. Otherwise,

find

find

returns

undefined

.

If a

thisArg

parameter is provided, it will be used as the

this

value for each invocation of

predicate

.

If it is not provided,

undefined

is used instead.

predicate

is called with three arguments: the value of the element, the index of the element, and

the object being traversed.

find

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

find

is set before the first call to

predicate

. Elements that are

appended to the array after the call to

find

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

find

visits them; elements that are deleted after the call to

find

find

begins and before being

visited are not visited.

23.1.3.8 Array.prototype.find (

predicate

[ ,

thisArg

] )

642

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

IsCallable

(

predicate

) is

false

, throw a

TypeError

exception.

4. Let

k

be 0.

5. Repeat, while

k

<

len

,

a. Let

Pk

be !

ToString

(

(

k

)).

b. Let

kValue

be ?

Get

(

O

,

Pk

).

c. Let

testResult

be !

ToBoolean

(?

Call

(

predicate

,

thisArg

, «

kValue

k

),

O

»)).

d. If

testResult

is

true

, return

kValue

.

e. Set

k

to

k

+ 1.

6. Return

undefined

.

NOTE 2

NOTE 1

When the

findIndex

findIndex

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

IsCallable

(

predicate

) is

false

, throw a

TypeError

exception.

4. Let

k

be 0.

5. Repeat, while

k

<

len

,

a. Let

Pk

be !

ToString

(

(

k

)).

b. Let

kValue

be ?

Get

(

O

,

Pk

).

c. Let

testResult

be !

ToBoolean

(?

Call

(

predicate

,

thisArg

, «

kValue

k

),

O

»)).

d. If

testResult

is

true

, return

(

k

).

The

find

find

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

predicate

should be a function that accepts three arguments and returns a value that is coercible to

a Boolean value.

findIndex

findIndex

calls

predicate

once for each element of the array, in ascending

order, until it finds one where

predicate

returns

true

. If such an element is found,

findIndex

findIndex

immediately returns the index of that element value. Otherwise,

findIndex

findIndex

returns -1.

If a

thisArg

parameter is provided, it will be used as the

this

value for each invocation of

predicate

.

If it is not provided,

undefined

is used instead.

predicate

is called with three arguments: the value of the element, the index of the element, and

the object being traversed.

findIndex

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

findIndex

is set before the first call to

predicate

. Elements

that are appended to the array after the call to

findIndex

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

findIndex

visits them; elements that are deleted after the call to

findIndex

findIndex

begins and before being visited are not visited.

23.1.3.9 Array.prototype.findIndex (

predicate

[ ,

thisArg

] )

643

e. Set

k

to

k

+ 1.

6. Return

-1

𝔽

.

NOTE 2

When the

flat

flat

method is called with zero or one arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

sourceLen

be ?

LengthOfArrayLike

(

O

).

3. Let

depthNum

be 1.

4. If

depth

is not

undefined

, then

a. Set

depthNum

to ?

ToIntegerOrInfinity

(

depth

).

b. If

depthNum

< 0, set

depthNum

to 0.

5. Let

A

be ?

ArraySpeciesCreate

(

O

, 0).

6. Perform ?

FlattenIntoArray

(

A

,

O

,

sourceLen

, 0,

depthNum

).

7. Return

A

.

The abstract operation FlattenIntoArray takes arguments

target

,

source

,

sourceLen

(a non-negative

integer

),

start

(a non-

negative

integer

), and

depth

(a non-negative

integer

or +

) and optional arguments

mapperFunction

and

thisArg

. It

performs the following steps when called:

1.

Assert

:

Type

(

target

) is Object.

2.

Assert

:

Type

(

source

) is Object.

3.

Assert

: If

mapperFunction

is present, then !

IsCallable

(

mapperFunction

) is

true

,

thisArg

is present, and

depth

is 1.

4. Let

targetIndex

be

start

.

5. Let

sourceIndex

be

+0

𝔽

.

6. Repeat, while

(

sourceIndex

) <

sourceLen

,

a. Let

P

be !

ToString

(

sourceIndex

).

b. Let

exists

be ?

HasProperty

(

source

,

P

).

c. If

exists

is

true

, then

i. Let

element

be ?

Get

(

source

,

P

).

ii. If

mapperFunction

is present, then

1. Set

element

to ?

Call

(

mapperFunction

,

thisArg

, «

element

,

sourceIndex

,

source

»).

iii. Let

shouldFlatten

be

false

.

iv. If

depth

> 0, then

1. Set

shouldFlatten

to ?

IsArray

(

element

).

v. If

shouldFlatten

is

true

, then

1. If

depth

is +

, let

newDepth

be +

.

2. Else, let

newDepth

be

depth

- 1.

3. Let

elementLen

be ?

LengthOfArrayLike

(

element

).

4. Set

targetIndex

to ?

FlattenIntoArray

(

target

,

element

,

elementLen

,

targetIndex

,

newDepth

).

vi. Else,

1. If

targetIndex

2

53

- 1, throw a

TypeError

exception.

The

findIndex

findIndex

function is intentionally generic; it does not require that its

this

value be an

Array object. Therefore it can be transferred to other kinds of objects for use as a method.

23.1.3.10 Array.prototype.flat ( [

depth

] )

23.1.3.10.1 FlattenIntoArray (

target

,

source

,

sourceLen

,

start

,

depth

[ ,

mapperFunction

,

thisArg

] )

644

2. Perform ?

CreateDataPropertyOrThrow

(

target

, !

ToString

(

(

targetIndex

)),

element

).

3. Set

targetIndex

to

targetIndex

+ 1.

d. Set

sourceIndex

to

sourceIndex

+

1

𝔽

.

7. Return

targetIndex

.

When the

flatMap

flatMap

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

sourceLen

be ?

LengthOfArrayLike

(

O

).

3. If !

IsCallable

(

mapperFunction

) is

false

, throw a

TypeError

exception.

4. Let

A

be ?

ArraySpeciesCreate

(

O

, 0).

5. Perform ?

FlattenIntoArray

(

A

,

O

,

sourceLen

, 0, 1,

mapperFunction

,

thisArg

).

6. Return

A

.

NOTE 1

When the

forEach

forEach

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

IsCallable

(

callbackfn

) is

false

, throw a

TypeError

exception.

4. Let

k

be 0.

5. Repeat, while

k

<

len

,

a. Let

Pk

be !

ToString

(

(

k

)).

b. Let

kPresent

be ?

HasProperty

(

O

,

Pk

).

c. If

kPresent

is

true

, then

i. Let

kValue

be ?

Get

(

O

,

Pk

).

ii. Perform ?

Call

(

callbackfn

,

thisArg

, «

kValue

k

),

O

»).

callbackfn

should be a function that accepts three arguments.

forEach

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

this

value for each invocation of

callbackfn

. If it is not provided,

undefined

is used instead.

callbackfn

is called with three arguments: the value of the element, the index of the element, and

the object being traversed.

forEach

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

forEach

is set before the first call to

callbackfn

. Elements

which are appended to the array after the call to

forEach

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

forEach

visits them; elements that are deleted after the call to

forEach

forEach

begins and before being visited are not visited.

23.1.3.11 Array.prototype.flatMap (

mapperFunction

[ ,

thisArg

] )

23.1.3.12 Array.prototype.forEach (

callbackfn

[ ,

thisArg

] )

645

d. Set

k

to

k

+ 1.

6. Return

undefined

.

NOTE 2

NOTE 1

When the

includes

includes

method is called, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

len

is 0, return

false

.

4. Let

n

be ?

ToIntegerOrInfinity

(

fromIndex

).

5.

Assert

: If

fromIndex

is

undefined

, then

n

is 0.

6. If

n

is +

, return

false

.

7. Else if

n

is -

, set

n

to 0.

8. If

n

0, then

a. Let

k

be

n

.

9. Else,

a. Let

k

be

len

+

n

.

b. If

k

< 0, set

k

to 0.

10. Repeat, while

k

<

len

,

a. Let

elementK

be ?

Get

(

O

, !

ToString

(

(

k

))).

b. If

SameValueZero

(

searchElement

,

elementK

) is

true

, return

true

.

c. Set

k

to

k

+ 1.

11. Return

false

.

NOTE 2

NOTE 3

The

forEach

forEach

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

includes

includes

compares

searchElement

to the elements of the array, in ascending order, using the

SameValueZero

algorithm, and if found at any position, returns

true

; otherwise,

false

is returned.

The optional second argument

fromIndex

defaults to

+0

𝔽

(i.e. the whole array is searched). If it is

greater than or equal to the length of the array,

false

is returned, i.e. the array will not be

searched. If it is less than

+0

𝔽

, it is used as the offset from the end of the array to compute

fromIndex

. If the computed index is less than

+0

𝔽

, the whole array will be searched.

The

includes

includes

function is intentionally generic; it does not require that its

this

value be an

Array object. Therefore it can be transferred to other kinds of objects for use as a method.

The

includes

includes

method intentionally differs from the similar

indexOf

indexOf

method in two ways.

First, it uses the

SameValueZero

algorithm, instead of

Strict Equality Comparison

, allowing it to

detect

NaN

array elements. Second, it does not skip missing array elements, instead treating

them as

undefined

.

23.1.3.13 Array.prototype.includes (

searchElement

[ ,

fromIndex

] )

23.1.3.14 Array.prototype.indexOf (

searchElement

[ ,

fromIndex

] )

646

NOTE 1

When the

indexOf

indexOf

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

len

is 0, return

-1

𝔽

.

4. Let

n

be ?

ToIntegerOrInfinity

(

fromIndex

).

5.

Assert

: If

fromIndex

is

undefined

, then

n

is 0.

6. If

n

is +

, return

-1

𝔽

.

7. Else if

n

is -

, set

n

to 0.

8. If

n

0, then

a. Let

k

be

n

.

9. Else,

a. Let

k

be

len

+

n

.

b. If

k

< 0, set

k

to 0.

10. Repeat, while

k

<

len

,

a. Let

kPresent

be ?

HasProperty

(

O

, !

ToString

(

(

k

))).

b. If

kPresent

is

true

, then

i. Let

elementK

be ?

Get

(

O

, !

ToString

(

(

k

))).

ii. Let

same

be the result of performing

Strict Equality Comparison

searchElement

===

elementK

.

iii. If

same

is

true

, return

(

k

).

c. Set

k

to

k

+ 1.

11. Return

-1

𝔽

.

NOTE 2

NOTE 1

The

join

join

method takes one argument,

separator

, and performs the following steps:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

separator

is

undefined

, let

sep

be the single-element String

","

.

indexOf

indexOf

compares

searchElement

to the elements of the array, in ascending order, using the

Strict Equality Comparison

algorithm, and if found at one or more indices, returns the smallest

such index; otherwise,

-1

𝔽

is returned.

The optional second argument

fromIndex

defaults to

+0

𝔽

(i.e. the whole array is searched). If it is

greater than or equal to the length of the array,

-1

𝔽

is returned, i.e. the array will not be searched.

If it is less than

+0

𝔽

, it is used as the offset from the end of the array to compute

fromIndex

. If the

computed index is less than

+0

𝔽

, the whole array will be searched.

The

indexOf

indexOf

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

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.

23.1.3.15 Array.prototype.join (

separator

)

647

4. Else, let

sep

be ?

ToString

(

separator

).

5. Let

R

be the empty String.

6. Let

k

be 0.

7. Repeat, while

k

<

len

,

a. If

k

> 0, set

R

to the

string-concatenation

of

R

and

sep

.

b. Let

element

be ?

Get

(

O

, !

ToString

(

k

))).

c. If

element

is

undefined

or

null

, let

next

be the empty String; otherwise, let

next

be ?

ToString

(

element

).

d. Set

R

to the

string-concatenation

of

R

and

next

.

e. Set

k

to

k

+ 1.

8. Return

R

.

NOTE 2

The following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Return

CreateArrayIterator

(

O

,

key

).

NOTE 1

When the

lastIndexOf

lastIndexOf

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

len

is 0, return

-1

𝔽

.

4. If

fromIndex

is present, let

n

be ?

ToIntegerOrInfinity

(

fromIndex

); else let

n

be

len

- 1.

5. If

n

is -

, return

-1

𝔽

.

6. If

n

0, then

a. Let

k

be

min

(

n

,

len

- 1).

7. Else,

a. Let

k

be

len

+

n

.

8. Repeat, while

k

0,

a. Let

kPresent

be ?

HasProperty

(

O

, !

ToString

(

(

k

))).

b. If

kPresent

is

true

, then

i. Let

elementK

be ?

Get

(

O

, !

ToString

(

(

k

))).

The

join

join

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore, it can be transferred to other kinds of objects for use as a method.

lastIndexOf

lastIndexOf

compares

searchElement

to the elements of the array in descending order using

the

Strict Equality Comparison

algorithm, and if found at one or more indices, returns the largest

such index; otherwise,

-1

𝔽

is returned.

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

+0

𝔽

, it is used as the offset from the end of the array to compute

fromIndex

. If the computed index is less than

+0

𝔽

,

-1

𝔽

is returned.

23.1.3.16 Array.prototype.keys ( )

23.1.3.17 Array.prototype.lastIndexOf (

searchElement

[ ,

fromIndex

] )

648

ii. Let

same

be the result of performing

Strict Equality Comparison

searchElement

===

elementK

.

iii. If

same

is

true

, return

(

k

).

c. Set

k

to

k

- 1.

9. Return

-1

𝔽

.

NOTE 2

NOTE 1

When the

map

map

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

IsCallable

(

callbackfn

) is

false

, throw a

TypeError

exception.

4. Let

A

be ?

ArraySpeciesCreate

(

O

,

len

).

5. Let

k

be 0.

6. Repeat, while

k

<

len

,

a. Let

Pk

be !

ToString

(

(

k

)).

b. Let

kPresent

be ?

HasProperty

(

O

,

Pk

).

c. If

kPresent

is

true

, then

i. Let

kValue

be ?

Get

(

O

,

Pk

).

ii. Let

mappedValue

be ?

Call

(

callbackfn

,

thisArg

, «

kValue

k

),

O

»).

iii. Perform ?

CreateDataPropertyOrThrow

(

A

,

Pk

,

mappedValue

).

d. Set

k

to

k

+ 1.

7. Return

A

.

The

lastIndexOf

lastIndexOf

function is intentionally generic; it does not require that its

this

value be an

Array object. Therefore it can be transferred to other kinds of objects for use as a method.

callbackfn

should be a function that accepts three arguments.

map

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

this

value for each invocation of

callbackfn

. If it is not provided,

undefined

is used instead.

callbackfn

is called with three arguments: the value of the element, the index of the element, and

the object being traversed.

map

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

map

is set before the first call to

callbackfn

. Elements which

are appended to the array after the call to

map

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

map

visits them; elements that are deleted after the call to

map

map

begins and before being visited

are not visited.

23.1.3.18 Array.prototype.map (

callbackfn

[ ,

thisArg

] )

649

NOTE 2

NOTE 1

When the

pop

pop

method is called, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

len

= 0, then

a. Perform ?

Set

(

O

,

"length"

,

+0

𝔽

,

true

).

b. Return

undefined

.

4. Else,

a.

Assert

:

len

> 0.

b. Let

newLen

len

- 1).

c. Let

index

be !

ToString

(

newLen

).

d. Let

element

be ?

Get

(

O

,

index

).

e. Perform ?

DeletePropertyOrThrow

(

O

,

index

).

f. Perform ?

Set

(

O

,

"length"

,

newLen

,

true

).

g. Return

element

.

NOTE 2

NOTE 1

When the

push

push

method is called with zero or more arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. Let

argCount

be the number of elements in

items

.

4. If

len

+

argCount

> 2

53

- 1, throw a

TypeError

exception.

5. For each element

E

of

items

, do

a. Perform ?

Set

(

O

, !

ToString

(

(

len

)),

E

,

true

).

b. Set

len

to

len

+ 1.

6. Perform ?

Set

(

O

,

"length"

,

len

),

true

).

7. Return

len

).

The

"length"

property of the

push

push

method is

1

𝔽

.

The

map

map

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

The last element of the array is removed from the array and returned.

The

pop

pop

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

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.

23.1.3.19 Array.prototype.pop ( )

23.1.3.20 Array.prototype.push ( ...

items

)

650

NOTE 2

NOTE 1

When the

reduce

reduce

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

IsCallable

(

callbackfn

) is

false

, throw a

TypeError

exception.

4. If

len

= 0 and

initialValue

is not present, throw a

TypeError

exception.

5. Let

k

be 0.

6. Let

accumulator

be

undefined

.

7. If

initialValue

is present, then

a. Set

accumulator

to

initialValue

.

8. Else,

a. Let

kPresent

be

false

.

b. Repeat, while

kPresent

is

false

and

k

<

len

,

i. Let

Pk

be !

ToString

(

(

k

)).

ii. Set

kPresent

to ?

HasProperty

(

O

,

Pk

).

iii. If

kPresent

is

true

, then

1. Set

accumulator

to ?

Get

(

O

,

Pk

).

iv. Set

k

to

k

+ 1.

c. If

kPresent

is

false

, throw a

TypeError

exception.

9. Repeat, while

k

<

len

,

a. Let

Pk

be !

ToString

(

(

k

)).

The

push

push

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

callbackfn

should be a function that takes four arguments.

reduce

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

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

TypeError

if the array contains no elements and

initialValue

is not

provided.

reduce

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

reduce

is set before the first call to

callbackfn

. Elements that

are appended to the array after the call to

reduce

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

reduce

visits them; elements that are deleted after the call to

reduce

reduce

begins and

before being visited are not visited.

23.1.3.21 Array.prototype.reduce (

callbackfn

[ ,

initialValue

] )

651

b. Let

kPresent

be ?

HasProperty

(

O

,

Pk

).

c. If

kPresent

is

true

, then

i. Let

kValue

be ?

Get

(

O

,

Pk

).

ii. Set

accumulator

to ?

Call

(

callbackfn

,

undefined

, «

accumulator

,

kValue

k

),

O

»).

d. Set

k

to

k

+ 1.

10. Return

accumulator

.

NOTE 2

NOTE 1

When the

reduceRight

reduceRight

method is called with one or two arguments, the following steps are taken:

1. Let

O

be ?

ToObject

(

this

value).

2. Let

len

be ?

LengthOfArrayLike

(

O

).

3. If

IsCallable

(

callbackfn

) is

false

, throw a

TypeError

exception.

4. If

len

is 0 and

initialValue

is not present, throw a

TypeError

exception.

5. Let

k

be

len

- 1.

6. Let

accumulator

be

undefined

.

7. If

initialValue

is present, then

a. Set

accumulator

to

initialValue

.

8. Else,

a. Let

kPresent

be

false

.

b. Repeat, while

kPresent

is

false

and

k

0,

i. Let

Pk

be !

ToString

(

(

k

)).

The

reduce

reduce

function is intentionally generic; it does not require that its

this

value be an Array

object. Therefore it can be transferred to other kinds of objects for use as a method.

callbackfn

should be a function that takes four arguments.

reduceRight

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

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

TypeError

if the array contains no

elements and

initialValue

is not provided.

reduceRight

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

reduceRight

is set before the first call to

callbackfn

.

Elements that are appended to the array after the call to

reduceRight

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

reduceRight

visits them; elements that are

deleted after the call to

reduceRight

reduceRight

begins and before being visited are not visited.

23.1.3.22 Array.prototype.reduceRight (

callbackfn

[ ,

initialValue

] )

652

Была ли эта страница вам полезна?
Да!Нет
Большое спасибо!
Ваше мнение очень важно для нас.

Нет комментариевНе стесняйтесь поделиться с нами вашим ценным мнением.

Текст

Политика конфиденциальности