Mercurial > laserkard
diff 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 |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/good-parts/first.js Sun Jul 25 01:33:22 2010 -0400 1.3 @@ -0,0 +1,49 @@ 1.4 +document.writeln("hello world!"); 1.5 + 1.6 +Function.prototype.method = function (name, func) { 1.7 + this.prototype[name] = func; 1.8 + return this; 1.9 +}; 1.10 + 1.11 + 1.12 +if (typeof Object.create !== 'function') { 1.13 + Object.create = function (o) { 1.14 + var F = function () {}; 1.15 + F.prototype = o; 1.16 + return new F(); 1.17 + };} 1.18 + 1.19 +Number.method('integer', function ( ) { 1.20 + return Math[this < 0 ? 'ceil' : 'floor'](this); 1.21 + }); 1.22 + 1.23 +String.method('trim', function ( ) { 1.24 + return this.replace(/^\s+|\s+$/g, ''); 1.25 + }); 1.26 + 1.27 + 1.28 +document.writeln((-10 / 3).integer( )); 1.29 + 1.30 +document.writeln("whatev ".trim()); 1.31 +document.writeln("whatev "); 1.32 + 1.33 +Function.method('curry', function ( ) { 1.34 + var slice = Array.prototype.slice, 1.35 + args = slice.apply(arguments), 1.36 + that = this; 1.37 + return function ( ) { 1.38 + return that.apply(null, args.concat(slice.apply(arguments))); 1.39 + }; 1.40 + }); 1.41 + 1.42 +var memoizer = function (memo, formula) { 1.43 + var recur = function (n) { 1.44 + var result = memo[n]; 1.45 + if (typeof result !== 'number') { 1.46 + result = formula(recur, n); 1.47 + memo[n] = result; 1.48 + } 1.49 + return result; 1.50 + }; 1.51 + return recur; 1.52 +};