Skip to content

Instantly share code, notes, and snippets.

@kgarfinkel
kgarfinkel / gist:7063475
Created October 20, 2013 00:53
Underscore.js's 'once' re-implemented. Returns a function that can only be called one time. Repeated calls to the modified function will result in returning the value from the original call
_.once = function(func) {
var alreadyCalled = false,
result;
return function() {
if(!alreadyCalled) {
result = func.apply(this, arguments);
alreadyCalled = true;
}
@kgarfinkel
kgarfinkel / gist:7063179
Created October 20, 2013 00:12
Javascript's 'call' re-implemented
var call = function(fn, context) {
var args = Array.prototype.slice.call(arguments, 2).join(‘,’),
result;
context.fn = fn;
result = eval(“context.fn(“+ args +”)”);
delete this.fn;
return result;
};
@kgarfinkel
kgarfinkel / gist:7062984
Created October 19, 2013 23:47
_.first return's an array of the first n elements of an array.
_.first = function(array, n) {
if (n === undefined) {
return array[0];
}
return slice.call(array, 0, n);
};