Skip to content

Instantly share code, notes, and snippets.

View dannycallaghan's full-sized avatar

Danny Callaghan dannycallaghan

  • London
View GitHub Profile
@dannycallaghan
dannycallaghan / JavaScript Function Terminology.js
Created October 4, 2013 08:05
JavaScript Function Terminology
/* Function Terminology */
// - (unamed) function expression
// - anonymous function
// - a.k.a. function literal
var add = function ( a, b ) {
return a + b;
};
// - (unamed) function expression
@dannycallaghan
dannycallaghan / Hoisting - function declarations vs function expressions.js
Created October 4, 2013 08:06
Hoisting - function declarations vs function expressions
/* Hoisting - function declarations vs function expressions */
function foo () { alert( 'global foo' ); }
function bar () { alert( 'global bar' ); }
function hoistMe () {
console.log( typeof foo ); // "function"
console.log( typeof bar ); // "undefined"
foo(); // alerts "local foo"
@dannycallaghan
dannycallaghan / Cross Browser Events.js
Created October 9, 2013 08:08
Cross browser add and remove events - with init-time branching. From 'JavaScript Patterns' (O'Reilly) by Stoyan Stefanov.
/* Cross browser add and remove events - with init-time branching */
// the interface
var utils = {
addListener : null,
removeListener : null
};
// the implementation
if ( window.addEventListener ) {
@dannycallaghan
dannycallaghan / Currying - Partial Application.js
Created October 9, 2013 08:09
Currying / Partial Application From 'JavaScript Patterns' (O'Reilly) by Stoyan Stefanov.
/* Currying */
// Currying is making JavaScript functions understand and handle partial application
// A non-curried add functions
var add = function ( x, y ) {
return x + y;
};
// A curried add function
function add ( x, y ) {
@dannycallaghan
dannycallaghan / Generic Namespace Function.js
Created October 11, 2013 08:23
Generic Namespace Function. From 'JavaScript Patterns' (O'Reilly) by Stoyan Stefanov.
/* Namespace Function */
var MYAPP = MYAPP || {};
MYAPP.namespace = function ( ns_string ) {
var parts = ns_string.split( '.' ),
parent = MYAPP, i;
if ( parts[ 0 ] === "MYAPP" ) {
parts = parts.slice( 1 );
}
@dannycallaghan
dannycallaghan / Fast Max Min.js
Created October 17, 2013 11:24
Fast JavaScript Max/Min, John Resig. From http://ejohn.org/blog/fast-javascript-maxmin/
Array.max = function( array ){
return Math.max.apply( Math, array );
};
Array.min = function( array ){
return Math.min.apply( Math, array );
};
// usage: log('inside coolFunc',this,arguments);
// http://paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function () {
log.history = log.history || []; // store logs to an array for reference
log.history.push( arguments );
if ( this.console ){
console.log( Array.prototype.slice.call( arguments ) );
}
};
@dannycallaghan
dannycallaghan / Web Performance - Optimise Caching
Created December 5, 2013 09:14
Google Developers - Web Performance Best Practices - Optimise Caching. From https://developers.google.com/speed/docs/best-practices/caching
##### Optimise caching #####
### Leverage Browser Caching ###
- 'Expires' and 'Cache-Control: max-age'
Specify time period browser can use a local copy without checking for a newer version. Strong cache headers - will not issue a GET request until the expiry or max age is reached.
- 'Last-Modified' and 'ETag'
Specify some characteristic that the browser checks to see if the files are the same - 'Last-Modified' is always a date; 'ETag' is any value that uniquely identifies that resource, like version number or hash. Weak cache header - the browser applies a heuristic to determine whether to use a cahce copy or not (each browser uses a different heuristic). Uses a conditional GET though - don't return a full response unless the resource has changed (thus lower latency than full GETS).
@dannycallaghan
dannycallaghan / dannycustom.zsh-theme
Last active January 3, 2016 13:49
Custom theme for zsh. Heavily borrowed from Remy Sharp's set up.
# vim:ft=zsh ts=2 sw=2 sts=2
#
# agnoster's Theme - https://gist.github.com/3712874
# A Powerline-inspired theme for ZSH
#
# # README
#
# In order for this theme to render correctly, you will need a
# [Powerline-patched font](https://gist.github.com/1595572).
#
@dannycallaghan
dannycallaghan / InstallHomebrew
Created January 24, 2014 10:05
Install Homebrew
// May ask you to install x-code command line tools
// If so, just follow the prompt
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"