/
extend.js
75 lines (61 loc) · 1.89 KB
/
extend.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
var test = require('tape')
, mixly = require('../')
;
/**
* Test function one
*/
function F1()
{
F1.super_.apply(this, arguments);
this.f0 = 'f1';
this.f1 = true;
}
F1.prototype.f1p = true;
F1.prototype.f0p = 'f1';
F1.static1 = true;
F1.static0 = 'f1';
/**
* Test function two
*/
function F2()
{
this.f0 = 'f2';
this.f2 = true;
}
F2.prototype.f2p = true;
F2.prototype.f0p = 'f2';
F2.static2 = true;
F2.static0 = 'f2';
test('extend', function(t)
{
t.plan(21);
var child;
mixly.extend(F1, F2);
F1.prototype.f1p2 = true;
t.true(F1.prototype.f2p, 'should have prototype property from the source object');
t.equal(F1.prototype.f1p, undefined, 'should not have property from the original prototype');
t.equal(F1.prototype.f0p, 'f2', 'prototype property from the extended prototype should prevail');
t.true(F1.static2, 'should have static property from the source object');
t.true(F1.static1, 'should have static property from the target object');
t.equal(F1.static0, 'f1', 'static property from the source object should prevail');
child = new F1();
t.equal(child.constructor, F1, 'should have proper constructor');
t.equal(Object.getPrototypeOf(child), F1.prototype, 'should have proper prototype');
t.true(child instanceof F1, 'should be instance of the proper constructor');
t.equal(child.f0, 'f1', 'property from the target\'s instance should be on top');
t.true(child.f1, 'should have property set on the target\'s instance');
t.true(child.f2, 'should have property set on the source\'s instance');
// it kills original prototype
t.false(child.f1p);
t.true(child.f2p);
// but new one could be modified
t.true(child.f1p2);
t.false(F2.prototype.f1p2);
F1.bla = 'test';
t.notEqual(F2.bla, 'test');
t.equal(F1.static0, 'f1');
t.true(F1.static1);
t.true(F1.static2);
// and proto is available too
t.equal(Object.getPrototypeOf(F1).static0, 'f2');
});