Skip to content

Instantly share code, notes, and snippets.

View allenwb's full-sized avatar

Allen Wirfs-Brock allenwb

View GitHub Profile
Array.prototype.fill = function fill(value,start=0, end=this.length) {
/*
Every element of array from start up to but not including end are assigned value.
start and end are interpretation the same as for slice,
negative start or stop indices are converted to positive indices relative to the lenth of the array.
If end<=start no elements are modified.
If end>this.length and this.length is read-only a range error is thrown and no elements are modified.
If end>this.length and this.length is not read-only, this.length is set to end.
Array.prototype.transfer = function transfer(target=0,start=0, end=this.length, fill=missingMarker) {
/*
The sequence of array elements of array from start up to but not including end are transferred within
the array to the span starting at the index target. The length of the array is not modified.
start and end are interpretation the same as for slice,
negative start, end, and target indices are converted to positive indices relative to the lenth of the array.
If end<=start no elements are transfered.
If end>this.length or target+(end-start)>this.length a range error is thrown and no elements are modified.
@allenwb
allenwb / forin.js
Created May 9, 2013 15:24
The ES5 for-in requirements expressed as an ES6 generator function
function *forin(obj) {
let processed = new Set();
while (obj!==null) {
let here = Object.getOwnPropertyNames(obj);
for (let i=0; i<here.length; i++) {
let name = here[i];
if (processed.has(name)) continue;
processed.add(name);
let desc = Object.getOwnPropertyDescriptor(obj,name);
if (desc && desc.enumerable) yield name;
@allenwb
allenwb / mapreviver.js
Created October 5, 2012 01:13
A JSON.parse reviver from ES6 maps
function mapReviver(key, value) {
if (typeof value != "object") return value;
switch (value["<kind>"]){
case undefined: return value;
case "Map": {
let newValue = new Map;
let mapData = value["<mapData>"];
if (!mapData) return value;
mapData.forEach(e=>newValue.set(e[0], e[1]));
return newValue;
@allenwb
allenwb / 0Option2ConstructorSummary.md
Last active November 4, 2023 14:39
New ES6 constructor features and semantics: Alternative 2 manual super in derived classes

New ES6 Constructor Semantics and Usage Examples

Manual super: Alternative Design where subclass constructors do not automatically call superclass constructors

This Gist presents a new design of class-based object construction in ES6 that does not require use of the two-phase @@create protocol.

One of the characteristics of this proposal is that subclass constructors must explicitly super invoke their superclass's constructor if they wish to use the base class' object allocation and initialization logic.

An alternative version of this design automatically invokes the base constructor in most situations.