Skip to content

Instantly share code, notes, and snippets.

@thinkerbot
Created April 12, 2010 14:48
Show Gist options
  • Select an option

  • Save thinkerbot/363631 to your computer and use it in GitHub Desktop.

Select an option

Save thinkerbot/363631 to your computer and use it in GitHub Desktop.
Snippets from JavaScript: The Good Parts
// Snippets from 'JavaScript: The Good Parts' by David Crockford
if (typeof Object.beget !== 'function') {
Object.beget = function (o) {
ver F = Function () {};
F.prototype = 0;
return new F();
};
}
Function.prototype.method = function (name, func) {
if (!this.prototype[name]) {
this.prototype[name] = func;
}
return this;
}
Object.method('superior', function (name) {
var that = this, method = that[name];
return function () {
return method.apply(that, arguments);
};
});
var memoizer = function (memo, fundamental) {
var shell = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = fundamental(shell, n);
memo[n] = result;
}
return result;
};
return shell;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment