I really liked Jordan Harband's suggestion
<https://esdiscuss.org/topic/array-prototype-change-was-tostringtag-spoofing-for-null-and-undefined#content-12>
of
adding Array.empty, Function.empty, etc. to ES7. It is relatively easy to
polyfill as well.

```js
[Array,
 ArrayBuffer,
 Int8Array,
 Int16Array,
 Int32Array,
 Uint8Array,
 Uint8ClampedArray,
 Uint16Array,
 Uint32Array,
 Float32Array,
 Float64Array]
.map(T => [T, new T(0)])
.concat([
  [Object, {}],
  [String, ''],
  [RegExp, /(?:)/],
  [Function, function () {}],
  [GeneratorFunction, function* () {}]
])
.forEach(([root, empty]) =>
  Object.defineProperty(root, 'empty', {
    value: Object.freeze(empty),
    configurable: true,
    enumerable: false,
    writable: false
  }));
```

The code effectively explains what I think would make suitable replacements
for each. I don't see the use cases for some of these, though, such as
`String.empty` or `RegExp.empty`.

-- 
Isiah Meadows
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to