Skip to content

Commit f917596

Browse files
committed
Code style
1 parent 78de558 commit f917596

File tree

5 files changed

+55
-55
lines changed

5 files changed

+55
-55
lines changed

JavaScript/1-mixin-function.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
let g1 = {};
4-
let g2 = {};
5-
let g3 = { area: 300 };
3+
const g1 = {};
4+
const g2 = {};
5+
const g3 = { area: 300 };
66

77
// Add property area to g2
88
g2.area = 200;
@@ -26,7 +26,7 @@ console.log(g1.calculateCost(5));
2626
console.log(g2.calculateCost(5));
2727
console.log(g3.calculateCost(5));
2828

29-
let t1 = setTimeout(function() {
29+
const t1 = setTimeout(() => {
3030
console.log('Hello from timer');
3131
}, 1000);
3232

JavaScript/2-mixin-extend.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ function extend(obj, mixin) {
1111
}
1212
*/
1313

14-
let extend = (obj, mixin) => (
14+
const extend = (obj, mixin) => (
1515
Object.keys(mixin).forEach(key => obj[key] = mixin[key]), obj
1616
);
1717

18-
let obj1 = { name: 'Marcus Aurelius', city: 'Rome', born: 121 };
18+
const obj1 = { name: 'Marcus Aurelius', city: 'Rome', born: 121 };
1919

20-
let mix1 = {
21-
toString: function() {
20+
const mix1 = {
21+
toString() {
2222
return this.name + ' was born in ' + this.city + ' in ' + this.born;
2323
},
24-
age: function() {
24+
age() {
2525
return new Date().getFullYear() - new Date(this.born + '').getFullYear();
2626
}
2727
};

JavaScript/3-mixin-behavior.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ function scalable(image) {
55
}
66

77
function lazy(image) {
8-
let scale = image.scale;
8+
const scale = image.scale;
99
image.scale = () => setImmediate(() => scale());
1010
}
1111

12-
let image = {};
12+
const image = {};
1313

1414
console.log('Mixin scalable() adds method: scale');
1515
scalable(image);

JavaScript/4-mixin-events.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
'use strict';
22

3-
let extend = (obj, mixin) => (
3+
const extend = (obj, mixin) => (
44
Object.keys(mixin).forEach(key => obj[key] = mixin[key]), obj
55
);
66

7-
let obj1 = { name: 'Marcus Aurelius', city: 'Rome', born: 121 };
7+
const obj1 = { name: 'Marcus Aurelius', city: 'Rome', born: 121 };
88

9-
let mexinEvents = {
10-
toString: function() {
9+
const mexinEvents = {
10+
toString() {
1111
return this.name + ' was born in ' + this.city + ' in ' + this.born;
1212
},
13-
age: function() {
13+
age() {
1414
return new Date().getFullYear() - new Date(this.born + '').getFullYear();
1515
}
1616
};
1717

18-
extend(obj1, mix1);
18+
extend(obj1, mexinEvents);
1919
console.log(obj1);
2020
console.log(obj1.toString());
2121
console.log('His age is ' + obj1.age() + ' as of today');

JavaScript/5-mixin-prototype.js

+38-38
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,42 @@
11
'use strict';
22

3+
const accessors = {
4+
5+
string(proto, name, index) {
6+
Object.defineProperty(proto.prototype, name, {
7+
get() {
8+
return this[index];
9+
},
10+
set(value) {
11+
this[index] = value;
12+
}
13+
});
14+
},
15+
16+
Date(proto, name, index) {
17+
Object.defineProperty(proto.prototype, name, {
18+
get() {
19+
return new Date(this[index]);
20+
},
21+
set(value) {
22+
this[index] = value instanceof Date ? value.toISOString() : value;
23+
}
24+
});
25+
},
26+
27+
function(proto, name, index, fieldDef) {
28+
Object.defineProperty(proto.prototype, name, { get: fieldDef });
29+
}
30+
31+
};
32+
333
// Assign metadata to array elements
434
// data - array of objects
535
// metadata - data describes PrototypeClass structure
636
// Returns: built PrototypeClass
737
//
838
function assignMetadata(data, metadata) {
9-
let proto = buildPrototype(metadata);
39+
const proto = buildPrototype(metadata);
1040
assignPrototype(data, proto);
1141
return proto;
1242
}
@@ -24,9 +54,9 @@ function assignPrototype(data, proto) {
2454
// Build Prototype from Metadata
2555
//
2656
function buildPrototype(metadata) {
27-
let protoClass = function ProtoClass() {};
57+
const protoClass = function ProtoClass() {};
2858
let index = 0, fieldDef, buildGetter, fieldType;
29-
for (let name in metadata) {
59+
for (const name in metadata) {
3060
fieldDef = metadata[name];
3161
fieldType = typeof(fieldDef);
3262
if (fieldType !== 'function') fieldType = fieldDef;
@@ -36,40 +66,10 @@ function buildPrototype(metadata) {
3666
return protoClass;
3767
}
3868

39-
let accessors = {
40-
41-
string: function(proto, name, index) {
42-
Object.defineProperty(proto.prototype, name, {
43-
get: function() {
44-
return this[index];
45-
},
46-
set: function(value) {
47-
this[index] = value;
48-
}
49-
});
50-
},
51-
52-
Date: function(proto, name, index) {
53-
Object.defineProperty(proto.prototype, name, {
54-
get: function() {
55-
return new Date(this[index]);
56-
},
57-
set: function(value) {
58-
this[index] = value instanceof Date ? value.toISOString() : value;
59-
}
60-
});
61-
},
62-
63-
function: function(proto, name, index, fieldDef) {
64-
//console.log({proto, name, index, fieldDef});
65-
Object.defineProperty(proto.prototype, name, { get: fieldDef });
66-
}
67-
68-
};
6969

7070
// Define Data Source
7171
//
72-
let data = [
72+
const data = [
7373
['Marcus Aurelius', 'Rome', '212-04-26'],
7474
['Victor Glushkov', 'Rostov on Don', '1923-08-24'],
7575
['Ibn Arabi', 'Murcia', '1165-11-16'],
@@ -79,11 +79,11 @@ let data = [
7979

8080
// Define metadata to build prototype dynamically
8181
//
82-
let metadata = {
82+
const metadata = {
8383
name: 'string',
8484
city: 'string',
8585
born: 'Date',
86-
age: function() {
86+
age() {
8787
return (
8888
new Date().getFullYear() -
8989
new Date(this.born + '').getFullYear()
@@ -93,7 +93,7 @@ let metadata = {
9393

9494
// Define query using regular JavaScript syntax
9595
//
96-
let query = person => (
96+
const query = person => (
9797
person.name !== '' &&
9898
person.age > 25 &&
9999
person.city === 'Rome'
@@ -103,5 +103,5 @@ let query = person => (
103103
assignMetadata(data, metadata);
104104

105105
// Apply query to dataset
106-
let res = data.filter(query);
106+
const res = data.filter(query);
107107
console.dir(res);

0 commit comments

Comments
 (0)