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 / advice.js
Created June 3, 2012 20:12
an advice functional mixin
//usage
withAdvice.call(targetObject);
//mixin augments target object with around, before and after methods
//method is the base method, advice is the augmenting function
withAdvice: function() {
['before', 'after', 'around'].forEach(function(m) {
this[m] = function(method, advice) {
if (typeof this[method] == 'function') {
return this[method] = fn[m](this[method], advice);
Array.apply(0,Array(123)).map(String.fromCharCode).slice(97)
i=96,r=[];while(i++<122)r.push(String.fromCharCode(i));r
String.fromCharCode[b='apply'](0,(a=Array)[b](0,a(123)).map(Date.call,Number)).slice(97)
eval('i=97;String.fromCharCode('+Array(26).join('i++,')+'i)')

Purists would say NO because 1) a module resembles a class not an instance - it has module props and instance props and need not be instantiated 2) when used as a mixin it actually extends the class not the instance 3) when module methods are accessed directly this is pure composition.

But in spirit YES (when used as mixins) because the relationship is a "like-a" not "is-a", and from a developer perspective, module instance methods are accessed by internal delegation. The convention is prototypes are referenced by cloning or internal delegation - the former relationship is static, the latter dynamic, as you point out in your question.

I think the definition of a prototype is not fully flushed out. We'd do better to identify them by intent rather than by their cold technical characteristics, as some attempt to do - there are just too many flavours and too many languages. To that end I feel mixins are quite prototypal in spirit - much more so that JS constructor-prototype chains because they exploit alikenes

@angus-c
angus-c / gist:1334100
Created November 2, 2011 16:28 — forked from abozhilov/gist:1333507
Arguments default value (allow empty args)
function func(a, f) {
return function (args) {
args = args || {};
args.__proto__ = a;
f.call(this, args);
};
};
var f = func({foo : 10, bar : 20}, function (args) {
@angus-c
angus-c / mixins.js
Created May 25, 2011 01:38
a fresh approach to mixins
//generic circle functions
var asCircle = function() {
this.area = function() {
return Math.PI * this.radius * this.radius;
}
this.grow = function() {
this.radius++;
}
this.shrink = function() {
this.radius--;
/*
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) {
///////////////////////////
// ES6 original
////////////////////////////
class A extends B {
constructor(x) {
this.x = x;
}
b() {return this.x};
}
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
//the old way
var a= {
aa: function() {
return this;
}
}
a.aa(); //a :)
// 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