Skip to content

Commit c7c0091

Browse files
committed
More examples
1 parent e8612ea commit c7c0091

6 files changed

Lines changed: 56 additions & 8 deletions

File tree

JavaScript/6-logger.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const logger = level => {
3+
const logger = (level = 'info') => {
44
const color = logger.colors[level] || logger.colors.info;
55
return s => {
66
const date = new Date().toISOString();

JavaScript/7-prototype.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
'use strict';
22

3-
function Logger(level) {
4-
this.color = this.colors[level] || this.colors.info;
3+
function Logger(level = 'info') {
4+
this.color = Logger.colors[level] || Logger.colors.info;
55
}
66

7-
Logger.prototype.colors = {
7+
Logger.colors = {
88
warning: '\x1b[1;33m',
99
error: '\x1b[0;31m',
1010
info: '\x1b[1;37m'

JavaScript/8-class.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

33
class Logger {
4-
constructor(level) {
5-
this.color = this.colors[level] || this.colors.info;
4+
constructor(level = 'info') {
5+
this.color = Logger.colors[level] || Logger.colors.info;
66
}
77

88
log(s) {
@@ -11,7 +11,7 @@ class Logger {
1111
}
1212
}
1313

14-
Logger.prototype.colors = {
14+
Logger.colors = {
1515
warning: '\x1b[1;33m',
1616
error: '\x1b[0;31m',
1717
info: '\x1b[1;37m'

JavaScript/b-set.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ class Rect {
88

99
const p1 = { x: 10, y: 20, width: 50, height: 50 };
1010
Object.setPrototypeOf(p1, Rect.prototype);
11-
// p1.__proto__ = Rect.prototype;
11+
//p1.__proto__ = Rect.prototype;
1212

1313
console.log(p1.toString());

JavaScript/d-get-set.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ class Rect {
1616
this.width = l;
1717
this.height = l;
1818
}
19+
20+
get side() {
21+
if (this.width !== this.height) throw new Error('not a Square');
22+
return this.width;
23+
}
1924
}
2025

2126
const p1 = new Rect(10, 20, 50, 100);
27+
console.log(p1.side);
2228
console.log(p1.area);
2329
p1.side = 150;
2430
console.log(p1.area);

JavaScript/e-array.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
3+
const data = [
4+
['Marcus Aurelius', '212-04-26', 'Rome'],
5+
['Commodus Antoninus', '312-04-26', 'Rome'],
6+
['Victor Glushkov', '1923-08-24', 'Rostov on Don'],
7+
['Ibn Arabi', '1165-11-16', 'Murcia'],
8+
['Mao Zedong', '1893-12-26', 'Shaoshan'],
9+
['Rene Descartes', '1596-03-31', 'La Haye en Touraine']
10+
];
11+
12+
class Person {
13+
get name() {
14+
return this[0];
15+
}
16+
get birth() {
17+
return this[1];
18+
}
19+
get city() {
20+
return this[2];
21+
}
22+
get age() {
23+
const difference = new Date() - new Date(this.birth);
24+
return Math.floor(difference / 31536000000);
25+
}
26+
}
27+
28+
const query = (person) => (
29+
person.name !== '' &&
30+
person.age > 18 &&
31+
person.city === 'Rome'
32+
);
33+
34+
console.dir(data);
35+
36+
data.forEach(person => {
37+
Object.setPrototypeOf(person, Person.prototype);
38+
// person.__proto__ = Person.prototype
39+
});
40+
41+
const res = data.filter(query);
42+
console.dir(res);

0 commit comments

Comments
 (0)