Skip to content

Commit 854f595

Browse files
committed
Refresh and refactor metaprogramming examples
1 parent 3271665 commit 854f595

File tree

10 files changed

+41
-89
lines changed

10 files changed

+41
-89
lines changed

2-level-up/2-metadata.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

2-level-up/2-speedtest.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

2-level-up/file1.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"statusCode":404}

2-level-up/file2.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"empty":"file"}
1+
{"statusCode":404}

2-level-up/file3.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"statusCode":404}

2-level-up/metadata.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
counters: { success: 0, error: 0, wrong: 0 },
3+
tasks: [
4+
{ interval: '5s', get: 'http://127.0.0.1/api/method1.json', expect: 'OK', save: 'file1.json' },
5+
{ interval: '8s', get: 'http://127.0.0.1/api/method2.json', put: 'http://127.0.0.1/api/method4.json', save: 'file2.json' },
6+
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', expect: 'Done', post: 'http://127.0.0.1/api/method5.json' },
7+
{ interval: '4s', load: 'file1.json', expect: 'OK', put: 'http://127.0.0.1/api/method6.json' },
8+
{ interval: '9s', load: 'file2.json', post: 'http://127.0.0.1/api/method7.json', save: 'file1.json' },
9+
{ interval: '3s', load: 'file1.json', save: 'file3.json' },
10+
],
11+
error: (err, request) => console.log('Error'),
12+
success: (err, request) => test.counters.success++
13+
}

2-level-up/metametaprogramming.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ function duration(s) {
2626
// Metadata
2727
//
2828
let tasks = [
29-
{ interval: 5000, get: 'http://127.0.0.1/api/method1.json', expect: 'OK', save: 'file1.json' },
29+
{ interval: 5000, get: 'http://127.0.0.1/api/method1.json', save: 'file1.json' },
3030
{ interval: '8s', get: 'http://127.0.0.1/api/method2.json', put: 'http://127.0.0.1/api/method4.json', save: 'file2.json' },
31-
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', expect: 'Done', post: 'http://127.0.0.1/api/method5.json' },
32-
{ interval: '4s', load: 'file1.json', expect: 'OK', put: 'http://127.0.0.1/api/method6.json' },
31+
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', post: 'http://127.0.0.1/api/method5.json' },
32+
{ interval: '4s', load: 'file1.json', put: 'http://127.0.0.1/api/method6.json' },
3333
{ interval: '9s', load: 'file2.json', post: 'http://127.0.0.1/api/method7.json', save: 'file1.json' },
3434
{ interval: '3s', load: 'file1.json', save: 'file3.json' },
3535
];

2-level-up/metaprogramming.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ function duration(s) {
2525
// Metadata
2626
//
2727
var tasks = [
28-
{ interval: 5000, get: 'http://127.0.0.1/api/method1.json', expect: 'OK', save: 'file1.json' },
28+
{ interval: 5000, get: 'http://127.0.0.1/api/method1.json', save: 'file1.json' },
2929
{ interval: '8s', get: 'http://127.0.0.1/api/method2.json', put: 'http://127.0.0.1/api/method4.json', save: 'file2.json' },
30-
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', expect: 'Done', post: 'http://127.0.0.1/api/method5.json' },
31-
{ interval: '4s', load: 'file1.json', expect: 'OK', put: 'http://127.0.0.1/api/method6.json' },
30+
{ interval: '7s', get: 'http://127.0.0.1/api/method3.json', post: 'http://127.0.0.1/api/method5.json' },
31+
{ interval: '4s', load: 'file1.json', put: 'http://127.0.0.1/api/method6.json' },
3232
{ interval: '9s', load: 'file2.json', post: 'http://127.0.0.1/api/method7.json', save: 'file1.json' },
3333
{ interval: '3s', load: 'file1.json', save: 'file3.json' },
3434
];

4-class-factory/class-inheritance-metaprogramming.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
// Base class
44
//
5-
let Ground = (area) => {
5+
function Ground(area) {
66
this.area = area;
7-
};
7+
}
88

99
// Base class prototype method
1010
//
11-
Ground.prototype.calculateCost = (price) => this.area * price;
11+
Ground.prototype.calculateCost = function(price) {
12+
return this.area * price;
13+
};
1214

1315
// MetaFactory to build descendant classes
1416
//
1517
function inheritGround(mixin) {
1618

17-
let DescendantGround = (area) => {
19+
let DescendantGround = function(area) {
1820
this.constructor.apply(this, arguments);
1921
this.isEmpty = parseInt(area) <= 0;
2022
};
@@ -26,7 +28,6 @@ function inheritGround(mixin) {
2628
for (let property in mixin) {
2729
DescendantGround.prototype[property] = mixin[property];
2830
}
29-
3031
return DescendantGround;
3132

3233
}
@@ -37,7 +38,9 @@ let LandOwnership = inheritGround({
3738
category: 'land',
3839
type: 'ownership',
3940
// Add method to descendant class prototype
40-
toString: (price) => this.category + ' ' + this.type + ' / ' +this.area
41+
toString: function(price) {
42+
return this.category + ' ' + this.type + ' / ' +this.area;
43+
}
4144
});
4245

4346
// Create and use instance

4-class-factory/class-inheritance-simple.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@
22

33
// Base class
44
//
5-
let Ground = (area) => {
5+
function Ground(area) {
66
this.area = area;
7-
};
7+
}
88

99
// Base class prototype method
1010
//
11-
Ground.prototype.calculateCost = (price) => this.area * price;
11+
Ground.prototype.calculateCost = function(price) {
12+
return this.area * price
13+
};
1214

1315
// Create descendant class
1416
//
15-
let LandOwnership = (area) => {
17+
function LandOwnership(area) {
1618
this.constructor.apply(this, arguments);
1719
this.isEmpty = parseInt(area) <= 0;
18-
};
20+
}
1921

2022
// Use protorype inheritance from Ground
2123
//
@@ -28,9 +30,9 @@ LandOwnership.prototype.type = 'ownership';
2830

2931
// Add method to descendant class prototype
3032
//
31-
LandOwnership.prototype.toString = (price) => (
32-
this.category + ' ' + this.type + ' / ' + this.area
33-
);
33+
LandOwnership.prototype.toString = function(price) {
34+
return this.category + ' ' + this.type + ' / ' + this.area;
35+
};
3436

3537
// Create and use instance
3638
//

0 commit comments

Comments
 (0)