From 7bf384adac6ec0619ecdb6513c14e7c8c089f87b Mon Sep 17 00:00:00 2001
From: Justin Ridgewell
Date: Thu, 15 Jul 2021 00:45:26 -0400
Subject: [PATCH] Use prototype-less return object
---
index.html | 3 ++-
spec.emu | 3 ++-
2 files changed, 4 insertions(+), 2 deletions(-)
diff --git a/index.html b/index.html
index f7a7b08..d96dd1d 100644
--- a/index.html
+++ b/index.html
@@ -2261,9 +2261,10 @@ 2.1 Array.prototype.groupBy ( callbackfncallbackfn is called with three arguments: the value of the element, the index of the element, and the object being traversed.
groupBy
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 groupBy
is set before the first call to callbackfn. Elements which are appended to the array after the call to groupBy
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 groupBy
visits them; elements that are deleted after the call to groupBy
begins and before being visited are not visited.
+ The return value of groupBy
is an object that does not inherit from Object.prototype.
When the groupBy
method is called with one or two arguments, the following steps are taken:
- - Let O be ? ToObject(this value).
- Let len be ? LengthOfArrayLike(O).
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- Let k be 0.
- Let obj be ! OrdinaryObjectCreate(%Object.prototype%).
- Assert: obj is an extensible ordinary object with no own properties.
- Repeat, while k < len
- Let Pk be ! ToString(k).
- Let kValue be ? Get(O, Pk).
- Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, k, O »)).
- Let seen be ? HasOwnProperty(obj, propertyKey).
- If seen is true, then
- Let arr be ! Get(obj, propertyKey).
- Else
- Let arr be ? ArraySpeciesCreate(O, 0).
- Perform ! CreateDataPropertyOrThrow(obj, propertyKey, arr).
- Perform ? Call(%Array.prototype.push%, arr, kValue).
- Set k to k + 1.
- Return obj.
+ - Let O be ? ToObject(this value).
- Let len be ? LengthOfArrayLike(O).
- If IsCallable(callbackfn) is false, throw a TypeError exception.
- Let k be 0.
- Let obj be ! OrdinaryObjectCreate(null).
- Assert: obj is an extensible ordinary object with no own properties.
- Repeat, while k < len
- Let Pk be ! ToString(k).
- Let kValue be ? Get(O, Pk).
- Let propertyKey be ? ToPropertyKey(? Call(callbackfn, thisArg, « kValue, k, O »)).
- Let seen be ? HasOwnProperty(obj, propertyKey).
- If seen is true, then
- Let arr be ! Get(obj, propertyKey).
- Else
- Let arr be ? ArraySpeciesCreate(O, 0).
- Perform ! CreateDataPropertyOrThrow(obj, propertyKey, arr).
- Perform ? Call(%Array.prototype.push%, arr, kValue).
- Set k to k + 1.
- Return obj.
Note 2
The groupBy
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.
diff --git a/spec.emu b/spec.emu
index 5390d35..1cf3a80 100644
--- a/spec.emu
+++ b/spec.emu
@@ -30,6 +30,7 @@ location: https://tc39.es/proposal-array-grouping/
_callbackfn_ is called with three arguments: the value of the element, the index of the element, and the object being traversed.
`groupBy` 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 `groupBy` is set before the first call to _callbackfn_. Elements which are appended to the array after the call to `groupBy` 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 `groupBy` visits them; elements that are deleted after the call to `groupBy` begins and before being visited are not visited.
+ The return value of `groupBy` is an object that does not inherit from _Object.prototype_.
When the `groupBy` method is called with one or two arguments, the following steps are taken:
@@ -37,7 +38,7 @@ location: https://tc39.es/proposal-array-grouping/
1. Let _len_ be ? LengthOfArrayLike(_O_).
1. If IsCallable(_callbackfn_) is *false*, throw a *TypeError* exception.
1. Let _k_ be 0.
- 1. Let _obj_ be ! OrdinaryObjectCreate(%Object.prototype%).
+ 1. Let _obj_ be ! OrdinaryObjectCreate(null).
1. Assert: _obj_ is an extensible ordinary object with no own properties.
1. Repeat, while _k_ < _len_
1. Let _Pk_ be ! ToString(_k_).