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
};
@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 / 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()
@angus-c
angus-c / gist:1081818
Created July 14, 2011 02:08 — forked from max-mapper/gist:1007605
BS ≈ BreakfastScript
Conditionals
( false :)
console.log "if"
:( true )
console.log "else if"
:()
console.log "else"
@angus-c
angus-c / jqueryBug.js
Created June 1, 2011 03:15
adding event handler to the <html> element in IE<9 will crash jquery 1.5.2
/*
* probably not good practice to attach event handlers to the <html> element but...
*/
//IE < 9 throws operation not supported error just on property existence check (not function invocation)
typeof $('html')[0]; //"unknown"
$('html')[0].removeAttribute //throws operation not supported error
/************************************************/
@angus-c
angus-c / variables.js
Created May 21, 2011 16:51
variable decalration
//one statement - all on one line - good for short, similar purpose vars
var foo = [], bar = [], buzz = [];
//multiple var statements - good for large number or disperate purpose vars
var a = 12;
var collate = false;
var foo = [1,2,3,4];
var circle = {radius: 2, color: blue};
//one statement on multiple lines, no indent