Skip to content

Instantly share code, notes, and snippets.

View dshaw's full-sized avatar
🦄
Always bet on Node.js ✨

Dan Shaw dshaw

🦄
Always bet on Node.js ✨
View GitHub Profile
@dshaw
dshaw / arguments.callee
Created May 1, 2010 05:41
Replacing named function reference with arguments.callee
// From @jeresig
// http://ejohn.org/apps/learn/#15
var ninja = {
yell: function(n){
return n > 0 ? arguments.callee(n-1) + "a" : "hiy";
}
};
assert( ninja.yell(4) == "hiyaaaa", "arguments.callee is the function itself." );
@dshaw
dshaw / The .bind method from Prototype.js
Created May 1, 2010 05:04
The .bind method from Prototype.js
// From @jeresig
// http://ejohn.org/apps/learn/#2
// The .bind method from Prototype.js
Function.prototype.bind = function(){
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function(){
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};
@dshaw
dshaw / Array.prototype.forEach implementation in TWTR widget.js
Created April 29, 2010 19:28
Array.prototype.forEach implementation in TWTR widget.js
// from http://twitter.com/javascripts/widgets/widget.js
if(!Array.forEach){Array.prototype.forEach=function(D,E){var C=E||window;for(var B=0,A=this.length;B<A;++B){D.call(C,this[B],B,this)}};
@dshaw
dshaw / bury console.log
Created April 29, 2010 17:56
bury console.log from TWTR widget.js
// from http://twitter.com/javascripts/widgets/widget.js
if(!"console" in window){window.console={log:function(){}}}
@dshaw
dshaw / jQuery-like each implementation
Created April 29, 2010 17:11
jQuery-like each implementation
var each = [].forEach || function (fn) {
var len = this.length || 0, that = arguments[1];
if (typeof fn == 'function') {
for (var i = 0; i < len; i++) {
fn.call(that, this[i], i, this);
}
}
};
@dshaw
dshaw / gist:378192
Created April 25, 2010 05:56 — forked from paulirish/gist:315916
(function(window,document,undefined){ ... })(this,this.document);
// everyone's new favorite closure pattern:
(function(window,document,undefined){ ... })(this,this.document);
// when minified:
(function(w,d,u){ ... })(this,this.document);
// which means all uses of window/document/undefined inside the closure
// will be single-lettered, so big gains in minification.
// it also will speed up scope chain traversal a tiny tiny little bit.
@dshaw
dshaw / Array.prototype.remove(elem)
Created April 23, 2010 19:07
Array.prototype.remove( element )
// Check to make sure indexOf is implemented (!!IE6).
if (!Array.indexOf) {
Array.prototype.indexOf = function(elem) {
var i=0,
length=this.length;
for (; i < length; i++) {
if (this[i] === elem ) {
return i;
}
}
@dshaw
dshaw / JavaScript implementation of isDate()
Created April 23, 2010 19:04
JavaScript isDate() implementation
function isDate(str) {
return (Object.prototype.toString.call(new Date(str)) === "[object Date]");
}
var scrollTop = Math.max(doc.documentElement.scrollTop, doc.body.scrollTop);
// From @codepo8
// http://www.smashingmagazine.com/2010/04/20/seven-javascript-things-i-wish-i-knew-much-earlier-in-my-career/
// Take the list of class attributes on an element and add new class.
function addclass(elm,newclass){
var classes = elm.className.split(' ');
classes.push(newclass);
elm.className = classes.join(' ');
}