Mercurial > laserkard
comparison 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 |
comparison
equal
deleted
inserted
replaced
78:4ebd94bfecda | 79:343dc947f999 |
---|---|
1 document.writeln("hello world!"); | |
2 | |
3 Function.prototype.method = function (name, func) { | |
4 this.prototype[name] = func; | |
5 return this; | |
6 }; | |
7 | |
8 | |
9 if (typeof Object.create !== 'function') { | |
10 Object.create = function (o) { | |
11 var F = function () {}; | |
12 F.prototype = o; | |
13 return new F(); | |
14 };} | |
15 | |
16 Number.method('integer', function ( ) { | |
17 return Math[this < 0 ? 'ceil' : 'floor'](this); | |
18 }); | |
19 | |
20 String.method('trim', function ( ) { | |
21 return this.replace(/^\s+|\s+$/g, ''); | |
22 }); | |
23 | |
24 | |
25 document.writeln((-10 / 3).integer( )); | |
26 | |
27 document.writeln("whatev ".trim()); | |
28 document.writeln("whatev "); | |
29 | |
30 Function.method('curry', function ( ) { | |
31 var slice = Array.prototype.slice, | |
32 args = slice.apply(arguments), | |
33 that = this; | |
34 return function ( ) { | |
35 return that.apply(null, args.concat(slice.apply(arguments))); | |
36 }; | |
37 }); | |
38 | |
39 var memoizer = function (memo, formula) { | |
40 var recur = function (n) { | |
41 var result = memo[n]; | |
42 if (typeof result !== 'number') { | |
43 result = formula(recur, n); | |
44 memo[n] = result; | |
45 } | |
46 return result; | |
47 }; | |
48 return recur; | |
49 }; |