Skip to content

Instantly share code, notes, and snippets.

View allenwb's full-sized avatar

Allen Wirfs-Brock allenwb

View GitHub Profile
@allenwb
allenwb / minimalist-classes.js
Created November 2, 2011 03:04 — forked from BrendanEich/minimalist-classes.js
less minimalism, richer leather
//work in progress
// A response to jashkenas's fine proposal for minimalist JavaScript classes.
// and BrendanEich's Rich Corinthian Leather alternative proposal
//intro and justifications still to come
// Harmony always stipulated classes as sugar, so indeed we are keeping current
// JavaScript prototype semantics, and classes would only add a syntactic form
// that can desugar to ES5. This is mostly the same assumption that Jeremy
@allenwb
allenwb / gist:1861530
Created February 19, 2012 01:21
JavaScript utf-16 JSON round-tripping experiment

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
@allenwb
allenwb / introspectNumber.js
Created March 4, 2012 19:11
A hypothetical Number mirror factory that distingishes between number values and number objects
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 / short-functions.js
Created March 11, 2012 08:13 — forked from dherman/short-functions.js
using do () {} for concise functions
// 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 / 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;
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);
}
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 / gist:e4b7116aefcec840e9cf
Created June 20, 2014 20:55
experimenting witn alternatives new semantics for ES6
//The following is essentially the current ES6 spec. for Boolean, expressed in ES code
//$$foo calls are performing spec. level operations that don't have direct ES language equivalents
class Boolean {
static [Symbol.create](.){
//@@create allocates the object with private slots but doesn't do any slot initialization
return $$OrdinaryDateFromConstructor(this,"%BooleanPrototype%",["[[BooleanData]]"]);
// a newly created Boolean instance has undefined as the value of its [[BooleanData]] internal slot.
// the undefined value indicates that the instance has not yet been initialized by the constructor function
}