Map.prototype.map ( callbackfn [ , thisArg ] )
The map method calls MapTransform
abstract operation passing this value as M, callbackfn, thisArg as is, and false as updateKey.
The length property of the map method is 1.
Map.prototype.mapEntries ( callbackfn [ , thisArg ] )
The mapEntries method calls MapTransform
abstract operation passing this value as M, callbackfn, thisArg as is, and true as updateKey.
The length property of the mapEntries method is 1.
MapTransform(M, callbackfn, updateKey, [, thisArg ] ) abstract operation
NOTE callbackfn should be a function that accepts three arguments. MapTransform calls callbackfn once for each key/value pair present in the map object, in key insertion order, and returns a new transformed map. callbackfn is called only for keys of the map which actually exist; it is not called for keys that have been deleted from the map.
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 item, the key of the item, and the Map object being traversed.
MapTransform does not directly mutate the object on which it is called but the object may be mutated by the calls to callbackfn.
When the MapTransform method is called, the following steps are taken:
- If Type(M) is not Object, then throw a TypeError exception.
- If M does not have a [[MapData]] internal slot throw a TypeError exception.
- If M's [[MapData]] internal slot is undefined, then throw a TypeError exception.
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- If thisArg was supplied, let T be thisArg; else let T be undefined.
- Let entries be the List that is the value of M's [[MapData]] internal slot.
- Let resultMap be the result of calling
InstanceFromInstanceCreate
abstract operation, passing M as object, null as parameters list, and true for throw. - Repeat for each Record {[[key]], [[value]]} e that is an element of entries, in original key insertion order
- If e.[[key]] is not empty, then
- Let funcResult be the result of calling the [[Call]] internal method of callbackfn with T as thisArgument and a List containing e.[[value]], e.[[key]], and M as argumentsList.
- ReturnIfAbrupt(funcResult).
- If updateKey is true, then
- If funcResult is not an exotic Array object, then throw a TypeError
- Let newKey be
Get
(funcResult, 0) - Let newValue be
Get
(funcResult, 1)
- Else,
- Let newKey be e.[[key]]
- Let newValue be the funcResult
- Call resultMap's explicit set method passing newKey as key, and newValue as value.
- Return resultMap.
InstanceFromInstanceCreate(O, argumentsList, throw) Abstract Operation
When the InstanceFromInstanceCreate
abstract operation is called with an instance object O, argumentsList, and throw boolean parameter, the following steps are taken:
- Let C be
Get
(O, "constructor"). - ReturnIfAbrupt(C).
- Let newInstance be undefined
- If
IsConstructor
(C) is false, then - If
throw
is true, throw TypeError exception. - Else,
1. Let thisRealm be the running execution context’s
Realm
. 2. IfSameValue
(thisRealm,GetFunctionRealm
(C)) is true, then- If argumentsList is null, let argumentsList be an empty
List
. - Let newInstance be the result of calling the [[Construct]] internal method of C with the argumentsList as an argument list.
- If argumentsList is null, let argumentsList be an empty
- Return newInstance.
Why can't keys be changed as well? A map operation on a map should expect
return [key, value]
, which would allow changing ofkey