Skip to content

Commit 1c82731

Browse files
committed
Add OOP composition
1 parent 987ae1d commit 1c82731

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

JavaScript/6-compose-proto.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const { EventEmitter } = require('events');
4+
5+
function AddOnlySet(it) {
6+
this.set = new Set(it);
7+
this.emitter = new EventEmitter();
8+
this.size = it.length;
9+
}
10+
11+
AddOnlySet.prototype.add = function(value) {
12+
this.set.add(value);
13+
this.size = this.set.size;
14+
this.emitter.emit('add', value);
15+
};
16+
17+
AddOnlySet.prototype.on = function(name, listener) {
18+
this.emitter.on(name, listener);
19+
};
20+
21+
AddOnlySet.prototype.toString = function() {
22+
return [...this.set.values()].join();
23+
};
24+
25+
const s1 = new AddOnlySet(['uno', 'due']);
26+
s1.on('add', value => console.log(`Added "${value}"`));
27+
s1.add('tre');
28+
console.dir({ result: s1.toString() });

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Composition
2-
===============
1+
# Composition

0 commit comments

Comments
 (0)