Skip to content

Instantly share code, notes, and snippets.

@psiborg
psiborg / queue.js
Created June 9, 2011 17:46
JS Queue (with support for a delay)
var queue = function () {
this.items = [];
this.timer = null;
};
queue.prototype = {
add: function (item) {
var self = this;
self.items.push(item);
},
@psiborg
psiborg / gist:1166156
Created August 23, 2011 19:05
Git Customizations
git config --global user.name "First Last"
git config --global user.email "[email protected]"
git config --global color.ui true
git config --global alias.lg "log --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr %an)%Creset' --abbrev-commit --date=relative"
git config --global alias.last 'cat-file commit HEAD'
@psiborg
psiborg / gist:1569628
Created January 6, 2012 08:11
JS Namespace
var RIM = RIM || {};
RIM.com = RIM.com || {};
RIM.com.alice = RIM.com.alice || {};
RIM.com.alice.hello = function () {
console.log('Hello World!');
};
(function() {
// save a local reference to namespace objects
@psiborg
psiborg / gist:1569630
Created January 6, 2012 08:12
JS Inheritance
var Person = function (name) {
this.name = name;
};
Person.prototype = {
says: function (saying) {
console.log(this.name + ' says "' + saying + '"');
}
};
@psiborg
psiborg / gist:1569634
Created January 6, 2012 08:13
JS Closure
for (var i = 0; i < 10; i++) {
var link = document.createElement("a");
link.innerHTML = "Link " + i;
link.href = "#";
link.onclick = (function (num) {
return function () {
alert("This is link " + num);
return false;
};
}(i));
@psiborg
psiborg / gist:1569635
Created January 6, 2012 08:14
JS Caching
var fibonacci = (function () {
var cache = {};
return function (n) {
if (cache[n]) {
return cache[n];
}
//console.log("Solving for " + n);
var result;
@psiborg
psiborg / gist:1569638
Created January 6, 2012 08:15
JS Memoization
var getElement = function () {
console.log("Creating element");
var elem = document.createElement("div");
// do a bunch of expensive operations
// overwrite function with a simpler version
getElement = function () {
return elem;
};
@psiborg
psiborg / gist:1569640
Created January 6, 2012 08:15
JS Context Bind 2
var myObject = {
someProperty: 42
};
myObject.method = function () {
console.log(this.someProperty);
};
function bindContext (func, context) {
return function () {
@psiborg
psiborg / gist:1569643
Created January 6, 2012 08:16
JS Context Bind 1
var myObject = {
someProperty: 42
};
function adder (num1, num2) {
return this.someProperty + num1 + num2;
}
console.log(adder.call(myObject, 10, 20)); // 72
console.log(adder.apply(myObject, [10, 20])); // 72
@psiborg
psiborg / gist:1569645
Created January 6, 2012 08:16
JS Dynamic Function
// creating a function with a dynamic function body
var Product = new Function("x", "y", "return x*y;"); // avoid unless you have to because it uses eval on 3rd arg