annotate good-parts/first.js @ 79:343dc947f999 laserkard

read JavaSctipt: the good parts
author Robert McIntyre <rlm@mit.edu>
date Sun, 25 Jul 2010 01:33:22 -0400
parents
children
rev   line source
rlm@79 1 document.writeln("hello world!");
rlm@79 2
rlm@79 3 Function.prototype.method = function (name, func) {
rlm@79 4 this.prototype[name] = func;
rlm@79 5 return this;
rlm@79 6 };
rlm@79 7
rlm@79 8
rlm@79 9 if (typeof Object.create !== 'function') {
rlm@79 10 Object.create = function (o) {
rlm@79 11 var F = function () {};
rlm@79 12 F.prototype = o;
rlm@79 13 return new F();
rlm@79 14 };}
rlm@79 15
rlm@79 16 Number.method('integer', function ( ) {
rlm@79 17 return Math[this < 0 ? 'ceil' : 'floor'](this);
rlm@79 18 });
rlm@79 19
rlm@79 20 String.method('trim', function ( ) {
rlm@79 21 return this.replace(/^\s+|\s+$/g, '');
rlm@79 22 });
rlm@79 23
rlm@79 24
rlm@79 25 document.writeln((-10 / 3).integer( ));
rlm@79 26
rlm@79 27 document.writeln("whatev ".trim());
rlm@79 28 document.writeln("whatev ");
rlm@79 29
rlm@79 30 Function.method('curry', function ( ) {
rlm@79 31 var slice = Array.prototype.slice,
rlm@79 32 args = slice.apply(arguments),
rlm@79 33 that = this;
rlm@79 34 return function ( ) {
rlm@79 35 return that.apply(null, args.concat(slice.apply(arguments)));
rlm@79 36 };
rlm@79 37 });
rlm@79 38
rlm@79 39 var memoizer = function (memo, formula) {
rlm@79 40 var recur = function (n) {
rlm@79 41 var result = memo[n];
rlm@79 42 if (typeof result !== 'number') {
rlm@79 43 result = formula(recur, n);
rlm@79 44 memo[n] = result;
rlm@79 45 }
rlm@79 46 return result;
rlm@79 47 };
rlm@79 48 return recur;
rlm@79 49 };