/
private-variables.html
137 lines (109 loc) · 3.2 KB
/
private-variables.html
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width"/>
<title>Private class variables tests</title>
<script src="tests.common.js"></script>
</head>
<body>
<script>
Tests.test(function (t)
{
// first, the A
function A ()
{
// private variable priv
var priv
this.setPriv = function (val) { return priv = val }
this.getPriv = function () { return priv }
}
// instantiating
var a1 = new A()
var a2 = new A()
// setting
a1.setPriv('from a1')
a2.setPriv('from a2')
// getting: all works well
t.eq(a1.getPriv(), 'from a1', 'direct var 1')
t.eq(a2.getPriv(), 'from a2', 'direct var 2')
// then a subclass had been exists
function B () { }
B.prototype = new A('from B')
// instantiating
var b1 = new B()
var b2 = new B()
// setting
b1.setPriv('from b1')
b2.setPriv('from b2')
// getting: error b1 and b2 shares the same private variable priv from the B.prototype
t.eq(b1.getPriv(), 'from b2', 'subclass var 1 = var 2')
t.eq(b2.getPriv(), 'from b2', 'subclass var 2')
// all work will be perform in constructor,
// so we can't avoid an unnecessary calculations
function C ()
{
var variable1 = 123
this.method1 = function () { variable1 += 1 }
this.method2 = function () { variable1 += 2 }
this.method3 = function () { variable1 += 3 }
this.method4 = function () { variable1 += 4 }
this.method5 = function () { variable1 += 5 }
this.method6 = function () { variable1 += 6 }
this.method7 = function () { variable1 += 7 }
this.method8 = function () { variable1 += 8 }
}
// nithing will be done in constructor,
// this gives us a chance to gain a performance boost
function D () {}
D.prototype =
{
variable1: 123,
method1: function () { this.variable1 += 1 },
method2: function () { this.variable1 += 2 },
method3: function () { this.variable1 += 3 },
method4: function () { this.variable1 += 4 },
method5: function () { this.variable1 += 5 },
method6: function () { this.variable1 += 6 },
method7: function () { this.variable1 += 7 },
method8: function () { this.variable1 += 8 }
}
var count = t.speed(function () { new C(); new D() }) / 5
t.time('C')
for (var i = 0; i < count; i++)
var c = new C()
var ctime = t.timeEnd('C')
t.time('D')
for (var i = 0; i < count; i++)
var d = new D()
var dtime = t.timeEnd('D')
t.ok(ctime > dtime * 5, 'new C >> new D')
var count = t.speed(function () { c.method8(); d.method8() }) / 5
var c = new C()
t.time('C')
for (var i = 0; i < count; i++)
c.method8()
var ctime = t.timeEnd('C')
var d = new D()
t.time('D')
for (var i = 0; i < count; i++)
d.method8()
var dtime = t.timeEnd('D')
// semi-equal or much different
t.ok((ctime / dtime < 2 || dtime / ctime < 2) || ctime > dtime * 10, 'C#method8() ~= || >> D#method8()')
var count = t.speed(function () { d.variable1++ }) / 5
var c = new C()
t.time('C')
for (var i = 0; i < count; i++)
c.method8()
var ctime = t.timeEnd('C')
var d = new D()
t.time('D')
for (var i = 0; i < count; i++)
d.variable1 += 8
var dtime = t.timeEnd('D')
// semi-equal or much different
t.ok((ctime / dtime < 2 || dtime / ctime < 2) || ctime > dtime * 10, 'C#method8() >> D#variable1')
})
</script>
</body>
</html>