Skip to content

Commit 66aed17

Browse files
committed
Added more JavaScript examples and knowledge.map dependencies
1 parent 83135f7 commit 66aed17

File tree

7 files changed

+82
-0
lines changed

7 files changed

+82
-0
lines changed

.jshintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"esversion": 6,
3+
"node": true
4+
}

JavaScript/3-closure.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
function add(x) {
4+
return function(y) {
5+
let z = x + y;
6+
console.log(x + '+' + y + '=' + z);
7+
return z;
8+
};
9+
}
10+
11+
add(3)(6);

JavaScript/4-closure-recursive.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
function add(x) {
4+
return function(y) {
5+
let z = x + y;
6+
console.log(x + '+' + y + '=' + z);
7+
return add(z);
8+
};
9+
}
10+
11+
let a1 = add(5);
12+
let a2 = a1(2);
13+
let a3 = a2(3);
14+
let a4 = a1(1);
15+
let a5 = a2(10);
16+
17+
console.log('--------');
18+
19+
add(1)(4)(8)(8);

JavaScript/5-logger.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
function logger(level) {
4+
let color = logger.colors[level] || logger.colors.info;
5+
return function(s) {
6+
let date = new Date().toISOString();
7+
console.log(color + date + '\t' + s);
8+
};
9+
}
10+
11+
logger.colors = {
12+
warning: '\x1b[1;33m',
13+
error: '\x1b[0;31m',
14+
info: '\x1b[1;37m'
15+
};
16+
17+
let warning = logger('warning');
18+
let error = logger('error');
19+
let debug = logger('debug');
20+
21+
warning('Hello');
22+
error('World');
23+
debug('Bye!');

JavaScript/6-functor.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
function add(x) {
4+
let f = function(y) {
5+
let z = x + y;
6+
console.log(x + '+' + y + '=' + z);
7+
return add(z);
8+
};
9+
f.map = function(fn) {
10+
fn(x);
11+
};
12+
return f;
13+
}
14+
15+
let a1 = add(5);
16+
let a2 = a1(2);
17+
let a3 = a2(3);
18+
let a4 = a1(1);
19+
let a5 = a2(10);
20+
a5.map(console.log);
21+
22+
add(2)(7)(1).map(console.log);

knowledge.map

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
dependencies: ['HigherOrderFunction']
3+
}

0 commit comments

Comments
 (0)