Skip to content

Commit 76169e6

Browse files
committed
JavaScript examples added
1 parent 0507b82 commit 76169e6

File tree

7 files changed

+117
-0
lines changed

7 files changed

+117
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules
2+
*.log
3+
.DS_Store

JavaScript/1-simple.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
function inc(a) {
4+
return ++a;
5+
}
6+
7+
let sum = function(a, b) {
8+
return a + b;
9+
};
10+
11+
let max = (a, b) => {
12+
return a > b ? a : b;
13+
};
14+
15+
console.log('inc(5) = ' + inc(5));
16+
console.log('sum(1, 3) = ' + sum(1, 3));
17+
console.log('max(8, 6) = ' + max(5, 6));

JavaScript/2-context.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
let cities = ['Athens', 'Roma', 'London', 'Beijing', 'Kiev', 'Riga'];
4+
let f = s => s.length;
5+
6+
function f1() {
7+
let cities = ['Athens', 'Roma'];
8+
let f = s => s.toUpperCase();
9+
console.dir({ cities });
10+
console.dir(cities.map(f));
11+
12+
{
13+
let f = s => s.toLowerCase();
14+
console.dir({ cities });
15+
console.dir(cities.map(f));
16+
}
17+
18+
{
19+
let cities = ['London', 'Beijing', 'Kiev'];
20+
console.dir({ cities });
21+
console.dir(cities.map(f));
22+
}
23+
24+
}
25+
26+
f1();
27+
28+
console.dir({ cities });
29+
console.dir(cities.map(f));

JavaScript/3-abstraction.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
let power = Math.pow;
4+
let square = x => power(x, 2);
5+
let cube = x => power(x, 3);
6+
7+
console.log(power(10, 2));
8+
console.log(square(10));
9+
10+
console.log(power(10, 3));
11+
console.log(cube(10));

JavaScript/4-introspection.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
function inc(a) {
4+
return ++a;
5+
}
6+
7+
let sum = function(a, b) {
8+
return a + b;
9+
};
10+
11+
let max = (a, b) => {
12+
return a > b ? a : b;
13+
};
14+
15+
console.log('Names: ');
16+
console.dir({
17+
inc: inc.name,
18+
sum: sum.name,
19+
max: max.name,
20+
});
21+
22+
console.log('Argumants: ');
23+
console.dir({
24+
inc: inc.length,
25+
sum: sum.length,
26+
max: max.length
27+
});
28+
29+
console.log('Anonymous function: ' + function(x) { return x }.name);
30+
console.log('Anonymous lambda' + (x => x).name);
31+
32+
console.log('toString: ');
33+
console.dir({
34+
inc: inc.toString(),
35+
sum: sum.toString(),
36+
max: max.toString()
37+
});

JavaScript/5-default.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
function fn(a, b = 'Hello', c = 5) {
4+
console.dir({ a, b, c });
5+
}
6+
7+
fn(1, 2, 3);
8+
fn(1, 2);
9+
fn(1);

JavaScript/6-spread.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
let f1 = (...args) => console.log(args);
4+
5+
f1(1, 2, 3);
6+
7+
let f2 = (...args) => {
8+
args.forEach(arg => console.log(typeof(arg) + ': ' + arg));
9+
};
10+
11+
f2(1, 'Marcus', { field: 'value' });

0 commit comments

Comments
 (0)