Skip to content

Instantly share code, notes, and snippets.

@vestige
Created June 9, 2011 13:18
Show Gist options
  • Save vestige/1016712 to your computer and use it in GitHub Desktop.
Save vestige/1016712 to your computer and use it in GitHub Desktop.
tkbjs#23
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
Array.method('reduce', function(f, value) {
var i;
for (i = 0; i < this.length; i += 1) {
value = f(this[i], value);
}
return value;
});
var data = [4, 8, 15, 16, 23, 42];
var add = function (a, b) {
return a + b;
};
var mult = function (a, b) {
return a * b;
};
var sum = data.reduce(add, 0);
document.writeln(sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment