diff options
Diffstat (limited to 'js/src/builtin/Array.js')
-rw-r--r-- | js/src/builtin/Array.js | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/js/src/builtin/Array.js b/js/src/builtin/Array.js index 05fc41bc14..1e7776e228 100644 --- a/js/src/builtin/Array.js +++ b/js/src/builtin/Array.js @@ -833,7 +833,7 @@ function ArrayFrom(items, mapfn=undefined, thisArg=undefined) { } // Step 7. - assert(usingIterator === undefined, "`items` can't be an Iterable after step 6.g.iv"); + assert(usingIterator === undefined, "`items` cannot be an Iterable after step 6.g.iv"); // Steps 8-9. var arrayLike = ToObject(items); @@ -1181,6 +1181,35 @@ function FlattenIntoArray(target, source, sourceLen, start, depth, mapperFunctio return targetIndex; } +// ES2022 at() method on the built-in indexables +// Array.prototype.at(index) +function ArrayAt(index) { + // Step 1. + var O = ToObject(this); + + // Step 2. + var len = ToLength(O.length); + + // Step 3. + var relativeIndex = ToInteger(index); + + // Steps 4-5. + var k; + if (relativeIndex >= 0) { + k = relativeIndex; + } else { + k = len + relativeIndex; + } + + // Step 6. + if (k < 0 || k >= len) { + return undefined; + } + + // Step 7. + return O[k]; +} + function ArrayStaticConcat(arr, arg1) { if (arguments.length < 1) ThrowTypeError(JSMSG_MISSING_FUN_ARG, 0, 'Array.concat'); |