Skip to content

Instantly share code, notes, and snippets.

View angus-c's full-sized avatar

angus croll angus-c

View GitHub Profile
@angus-c
angus-c / nodeListMixin.js
Created October 12, 2011 19:06
array-like NodeList via mixins
var NodeList = function() {};
var nodeListExtras = {x: 23};
mixin(NodeList.prototype, Array.prototype, nodeListExtras);
function mixin(obj /*, mixins*/) {
var mixins = [].slice.call(arguments, 1);
for (var i=0; i<mixins.length; i++) {
var thisMixin = mixins[i];
var props = Object.getOwnPropertyNames(thisMixin);
for (var j=0; j<props.length; j++) {
@angus-c
angus-c / fatArrow.js
Created March 31, 2012 17:24
es6 fat arrow syntax blocks call/apply context setting
function mixin(obj, fn) {
fn.call(obj);
}
//this gets bound dynamically
var withCircleUtils = function() {
this.area = function() {return this.radius * this.radius * Math.PI};
this.diameter = function() {return this.radius + this.radius};
}
@angus-c
angus-c / ToPrimitiveNoHint.js
Created April 15, 2012 20:19
ToPrimitiveNoHint.js
//shim of ES5.1 internal ToPrimitive method
//see http://es5.github.com/#x9.1
//assumes no hint argument (as is the case with == operands)
var ToPrimitiveNoHint = function(value) {
if (typeof value != 'object') {
return value;
}
if (typeof value.valueOf() != 'object') {
return value.valueOf();
@angus-c
angus-c / mixinStorage.js
Created April 28, 2012 17:43
storage with functional mixins
function withStorage() {
if (window.localStorage) {
localStorage.call(this);
} else if (document.documentElement.addBehavior) {
userData.call(this);
} else {
memory.call(this);
}
define(
[],
function () {
var utils = {
//object properties
};
// Arrow functions would have been useful for defining concise formulae to be invoked in
// the context of other objects.
// For example here's an object describing a piece of rope. It includes a method which
// returns the area enclosed by the rope.
// Since the enclosed area will vary according to the shape of the rope, the area method
// accepts a formula for calculating the area based on a specified shape.
// However with fat arrows, the context is static and tight-bound so 'this' is wrong and
// 'call' is useless
//the old way
var a= {
aa: function() {
return this;
}
}
a.aa(); //a :)
So there we were at the Red Drum, a tableful of beers a few that is and all the gangs cutting in and out, paying a dollar quarter at the door, the little hip-pretending weasel there taking tickets, Paddy Cordavan floating in as prophesied (a big tall blond brakeman type subterranean from Eastern Washington cowboy-looking in jeans coming in to a wild generation party all smoky and mad and I yelled, "Paddy Cordavan?" and "Yeah?" and he'd come over)--all sitting together, interesting groups at various tables, Julien, Roxanne (a woman of 25 prophesying the future style of America with short almost crewcut but with curls black snaky hair, snaky walk, pale pale junky anemic face and we say junky when once Dostoevski would have said what? if not ascetic but saintly? but not in the least? but the cold pale booster face of the cold blue girl and wearing a man's white shirt but with the cuffs undone untied at the buttons so I remember her leaning over talking to someone after having been slinked across the floor with f
///////////////////////////
// ES6 original
////////////////////////////
class A extends B {
constructor(x) {
this.x = x;
}
b() {return this.x};
}
/*
sayIt accepts an unlimited number of chaining calls, each passing a word. When it's finally called
without arguments, all the passed words are printed in order
sayIt('have')('you')('got')('any')('fish')('fingers?')(); ->
"have you got any fish fingers?"
A regular implementaion would go sthg like this
function sayIt(firstWord) {