-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpractice.js
More file actions
122 lines (103 loc) · 1.83 KB
/
practice.js
File metadata and controls
122 lines (103 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Quo function
*/
var quo = function (status) {
return {
get_status: function () {
return status;
}
}
}
/*
* Augmenting Types
*/
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
}
/*
* Closure function
*/
var myObject = (function () {
var value = 0;
return {
increment: function (inc) {
value += typeof inc === 'number' ? inc : 1;
},
getValue: function () {
return value;
}
};
}());
/*
* Another Closure function
*/
var fade = function (node) {
var level = 1;
var step = function () {
var hex = level.toString(16);
node.style.backgroundColor = '#FFFF' + hex + hex;
//console.log('#FFFF' + hex + hex);
if (level < 15) {
level += 1;
setTimeout(step, 100);
}
};
setTimeout(step, 100)
};
//fade(document.body);
/*
* Modules
*/
String.method('deentityify', function () {
// The entry table. It maps entity names to characters
var entity = {
quot: '"',
lt: '<',
gt: '>'
};
// Return the deentifyify method
return function () {
return this.replace(/&([^&;]+);/g,
function (a, b) {
var r = entity[b];
return typeof r === 'string' ? r : a;
}
)
};
}());
/*
* Functional Ineritance
*/
var mammal = function(spec) {
var that = {};
that.get_name = function() {
return spec.name;
};
that.says = function() {
return spec.saying || '';
};
return that;
}
var dog = function(spec) {
var that = mammal(spec);
that.bark = function() {
return that.get_name() + ' says bark, bark';
}
return that;
}
/*
* Momoization
*/
var fibonacci = (function ( ) {
var memo = [0, 1];
var fib = function (n) {
var result = memo[n];
if (typeof result !== 'number') {
result = fib(n - 1) + fib(n - 2);
memo[n] = result;
}
return result;
};
return fib;
}( ));