-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelegation.js
46 lines (41 loc) · 956 Bytes
/
delegation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* @title: Behavioral Delegation Pattern
* @description: Generic Behavioral Delegation Pattern
* @author: Thorsten Kober
* @email: [email protected]
*/
if (!Object.create) {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
const Base = {
init: function (id) {
this.id = id;
},
getId: function () {
return this.id;
},
};
const ExtendedBase = Object.create(Base);
ExtendedBase.setName = function (name) {
this.name = name;
};
ExtendedBase.getName = function () {
return this.name;
};
// npx jest patterns/delegation.js
describe('patterns/delegation', () => {
it('should extend another object', () => {
const b = Object.create(Base);
b.init('foo');
expect(b.getId()).toEqual('foo');
const c = Object.create(ExtendedBase);
c.init('baba');
expect(c.getId()).toEqual('baba');
c.setName('bo');
expect(c.getName()).toEqual('bo');
});
});