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 / 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);
}
@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);

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

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
@angus-c
angus-c / sleepSpeakMemory
Last active August 29, 2015 13:57
9 year-old Vladimir Nabokov trying to fall asleep (from Speak, Memory)
Now she has entered her room. A brisk interchange of light values tells me that the candle on her bed table takes over the job of the ceiling cluster of bulbs, which, having run up with a couple of clicks two additional steps of natural, and then supernatural, brightness, clicks off altogether. My line of light is still there, but it has grown old and wan, and flickers whenever Mademoiselle makes her bed creak by moving. For I still hear her. Now it is a silvery rustle spelling "Suchard"; now the trk-trk-trk of a fruit knife cutting the pages of La Revue des Deux Mondes. [...] I am in acute distress, desperately trying to coax sleep, opening my eyes every few seconds to check the faded gleam [...].
The inevitable happens: the pince-nez case shuts with a snap, the review shuffles onto the marble of the bed table, and gustily Mademoiselle's pursed lips blow; the first attempt fails, a groggy flame squirms and ducks; then comes a second lunge, and light collapses. In that pitchy blackness [...] finally my
/*
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) {