Skip to content

Commit a720bf8

Browse files
committed
Improve examples
1 parent 33e3891 commit a720bf8

6 files changed

Lines changed: 44 additions & 41 deletions

File tree

JavaScript/1-decorator.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ const g3 = { area: 300 };
88
g2.area = 200;
99

1010
// Mixin as a function
11-
function mixinCalculateCost(obj) {
11+
const mixinCalculateCost = (obj) => {
1212
obj.area = obj.area || 0;
1313
obj.calculateCost = function(price) {
1414
return this.area * price;
1515
};
16-
}
16+
};
1717

1818
// Mixin to single object
1919
mixinCalculateCost(g1);

JavaScript/2-extend.js

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

3-
/*function extend(obj, mixin) {
4-
for (let key in mixin) {
5-
if (mixin.hasOwnProperty(key)) {
6-
obj[key] = mixin[key];
7-
}
8-
}
9-
return obj;
10-
}*/
3+
// const extend = (obj, mixin) => {
4+
// for (let key in mixin) {
5+
// if (mixin.hasOwnProperty(key)) {
6+
// obj[key] = mixin[key];
7+
// }
8+
// }
9+
// return obj;
10+
// };
1111

1212
const extend = (obj, mixin) => {
1313
Object.keys(mixin).forEach(key => obj[key] = mixin[key]);
1414
return obj;
1515
};
1616

17+
// Usage
18+
1719
const obj1 = {
1820
name: 'Marcus Aurelius',
1921
city: 'Rome',

JavaScript/3-assign.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const mix2 = {
2323
}
2424
};
2525

26-
Object.assign(obj1, mix1, mix2);
26+
const res = Object.assign(obj1, mix1, mix2);
2727
console.log(obj1);
28-
console.log(obj1.toString());
28+
console.log(res.toString());
2929
console.log(`His age is ${obj1.age()} as of today`);

JavaScript/4-behavior.js

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

3-
function scalable(image) {
3+
const scalable = (image) => {
44
image.scale = () => console.log('Image scaled');
5-
}
5+
};
66

7-
function lazy(image) {
7+
const lazy = (image) => {
88
const scale = image.scale;
99
image.scale = () => setImmediate(() => scale());
10-
}
10+
};
1111

1212
const image = {};
1313

JavaScript/6-events.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
'use strict';
22

3-
const emitable = obj => Object.assign(obj, {
4-
events: {},
3+
const emitable = (obj, events = {}) => Object.assign(obj, {
54
on(name, fn) {
6-
const event = this.events[name] || [];
7-
this.events[name] = event;
5+
const event = events[name] || [];
6+
events[name] = event;
87
event.push(fn);
98
},
109
emit(name, ...data) {
11-
const event = this.events[name];
10+
const event = events[name];
1211
if (event) event.forEach(fn => fn(...data));
1312
}
1413
});

JavaScript/7-build.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -30,32 +30,21 @@ const accessors = {
3030

3131
};
3232

33-
// Assign metadata to array elements
34-
// data - array of objects
35-
// metadata - data describes PrototypeClass structure
36-
// Returns: built PrototypeClass
37-
//
38-
function assignMetadata(data, metadata) {
39-
const proto = buildPrototype(metadata);
40-
assignPrototype(data, proto);
41-
return proto;
42-
}
43-
4433
// Assign prototype to records array or single record
4534
// data - array of objects
4635
// proto - dynamically built prototipe to be assigned
47-
//
48-
function assignPrototype(data, proto) {
36+
37+
const assignPrototype = (data, proto) => {
4938
if (Array.isArray(data)) {
5039
data.forEach(item => item.__proto__ = proto.prototype);
5140
} else {
5241
data.__proto__ = proto.prototype;
5342
}
54-
}
43+
};
5544

5645
// Build Prototype from Metadata
57-
//
58-
function buildPrototype(metadata) {
46+
47+
const buildPrototype = (metadata) => {
5948
const protoClass = function ProtoClass() {};
6049
let index = 0, fieldDef, buildGetter, fieldType;
6150
for (const name in metadata) {
@@ -66,10 +55,23 @@ function buildPrototype(metadata) {
6655
if (buildGetter) buildGetter(protoClass, name, index++, fieldDef);
6756
}
6857
return protoClass;
69-
}
58+
};
59+
60+
// Assign metadata to array elements
61+
// data - array of objects
62+
// metadata - data describes PrototypeClass structure
63+
// Returns: built PrototypeClass
64+
65+
const assignMetadata = (data, metadata) => {
66+
const proto = buildPrototype(metadata);
67+
assignPrototype(data, proto);
68+
return proto;
69+
};
70+
71+
// Usage
7072

7173
// Define Data Source
72-
//
74+
7375
const data = [
7476
['Marcus Aurelius', 'Rome', '212-04-26'],
7577
['Victor Glushkov', 'Rostov on Don', '1923-08-24'],
@@ -79,7 +81,7 @@ const data = [
7981
];
8082

8183
// Define metadata to build prototype dynamically
82-
//
84+
8385
const metadata = {
8486
name: 'string',
8587
city: 'string',
@@ -93,7 +95,7 @@ const metadata = {
9395
};
9496

9597
// Define query using regular JavaScript syntax
96-
//
98+
9799
const query = person => (
98100
person.name !== '' &&
99101
person.age > 25 &&

0 commit comments

Comments
 (0)