Skip to content

Instantly share code, notes, and snippets.

View angus-c's full-sized avatar

angus croll angus-c

View GitHub Profile
define(
[],
function () {
var utils = {
//object properties
};

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 / 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);
@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 / 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 / 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 / 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 / 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 / firstClass.dart
Created October 10, 2011 18:30
first class functions in dart
main() {
print((function(x) => x + 1)(multiply(6, 6)));
}
var multiply = function(a, b) {return a * b;};
@angus-c
angus-c / wackyRandomizer.js
Created July 20, 2011 06:12
Wacky Randomizer
/*
Iterates every Math function in random order, with random arguments,
then randomly adds or subtracts each result and rounds the grand total.
Typical output:
+ tan(9)
+ sqrt(0)
- sin(0)
+ random()