Skip to content

Instantly share code, notes, and snippets.

Allen Wirfs-Brock allenwb

Block or report user

Report or block allenwb

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@allenwb
allenwb / mapreviver.js
Created Oct 5, 2012
A JSON.parse reviver from ES6 maps
View mapreviver.js
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 Nov 25, 2018
New ES6 constructor features and semantics: Alternative 2 manual super in derived classes
View 0Option2ConstructorSummary.md

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.

@allenwb
allenwb / forin.js
Created May 9, 2013
The ES5 for-in requirements expressed as an ES6 generator function
View forin.js
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;
View ArrayTransfer.js
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.
View ArrayFill.js
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.
View Object.isAncestorOf
Object.isAncestorOf = function isAncestorOf(parent,child) {
if (child===null || child===undefined) return false;
if (parent === null) return true;
child = Object(child);
var proto = Object.getPrototypeOf(child);
if (proto === parent) return true;
return isAncestorOf(parent,proto);
}
@allenwb
allenwb / species.md
Last active Nov 18, 2015
A FAQ on the Design of the ECMAScript 2016 @@species protocol
View species.md

####Why is .constructor not good enough? What does doing .constructor[@@species] add?

@@species is an extension point that allows abstract algorithms that may be inherited by (or applied to) various subclasses with differing characteristics to decouple the class of the objects generated by an application of the method from the actual class of the objects the method was applied to. This is a realivately rare situation, but one that does infact occur when developing relatively complex class hierarchies such as a collections hierarchy.

The first known usage of the species pattern that I’m aware of (and the origin of the name species) was in the Smalltalk-80 collection hierarchy. For example, the Smalltalk-80 collection hierarchy includes a class called SortedCollection. SortedCollection is a subclass of OrderedCollection (essentially a double ended queue, similar to a JS Array) except that whenever a new element is added to the collection, the value ordering of the collection is resorted. The familiar operat

@allenwb
allenwb / short-functions.js
Created Mar 11, 2012 — forked from dherman/short-functions.js
using do () {} for concise functions
View short-functions.js
// 1. No new syntax for non-TCP functions.
//
// This approach does do include a shorter syntax for regular functions, so if a classic JS function
// is what you want you use the classic long form function expression:
a.some(function (x){
if (invalid(x))
return true;
console.log(x);
});
@allenwb
allenwb / introspectNumber.js
Created Mar 4, 2012
A hypothetical Number mirror factory that distingishes between number values and number objects
View introspectNumber.js
Number.prototype.introspect = function() {
"use strict"; //.See ES5.1 10.4.3 steps 1-3
if (typeof this == "object") return Object.prototype.introspect.call(this);//generic object mirror
return new Float(+this); //Number value mirror
};
@allenwb
allenwb / gist:1861530
Created Feb 19, 2012
JavaScript utf-16 JSON round-tripping experiment
View gist:1861530

This is a response to Git 1850768 For some reason the following wouldn't post as a comment

@mranney @piscisaureus

I did some experiments and I don't see any round-tripping issues showing up, at least in FF:

var z= "\ud83d\ude38";   //u+1f638
console.log("z.length: " + z.length);  //expect 2
You can’t perform that action at this time.