-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
155 lines (123 loc) · 4.7 KB
/
test.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="oojs.js"></script>
<script>
var TestClosure = function() {
// declare Lifeform class //
var Lifeform = Class( "Lifeform",
function() {
this.isAlive = true;
}
);
// declare Mammal class and extend Lifeform //
var Mammal = Class( "Mammal", Extend, Lifeform,
function() {
this.legs;
// define a function that's later overridden in an extended class //
this.checkSuper = function(x) {
var x = x || 0;
return "mammal super: " + this.legs + ": " + x;
}
}
);
// declare Human class and extend Mammal //
var Human = Class( "Human", Extend, Mammal,
function( props ) {
this.legs = 2;
this.name;
var priv = "";
this.setPrivate = function(val) {
priv = val;
};
this.getPrivate = function() {
return priv;
};
// setup and call initializer //
var init = function(p) {
this.name = p.name;
// check for our private variable for initialization //
if( p.priv ) {
priv = p.priv;
}
}.call(this, props);
}
);
// declare Dog class and extend Mammal //
var Dog = Class( "Dog", Extend, Mammal,
function() {
this.legs = 4;
this.checkSuper = function() {
return "overridden";
};
this.getSuper = function() {
return "dog " + this._super().checkSuper(5);
};
}
);
/* ####### END SETUP TEST CLASSES ####### */
/* ########### START TESTING ############ */
var Beagle = new Dog();
var Jessica = new Human({
name: "Jessica",
priv: "waka"
});
var Timothy = new Human({
name: "Tim"
});
var Terrier = new Dog();
var Life = new Lifeform();
// test non extended //
console.log("## test unextended class");
console.log(Life);
// test prototype chain //
Lifeform.prototype.waka = "all lifeforms should have this";
console.log("## test prototyping..");
console.log(Jessica.waka, " :: ", Beagle.waka);
// test instanceof //
console.log("## is beagle a dog, a mammal, and a lifeform?");
console.log(Beagle instanceof Dog && Beagle instanceof Mammal && Beagle instanceof Lifeform);
console.log("## make sure beagle is not a human (should be false)");
console.log(Beagle instanceof Human);
// test constructors //
console.log("## is jessica constructor human?");
console.log(Jessica.constructor === Human);
// test inheritance //
console.log("## are all lifeforms alive?");
console.log( Jessica.isAlive && Beagle.isAlive );
// test private variables //
console.log("## test private variable getter; should output 'waka' for jessica");
console.log( Jessica.getPrivate("priv") );
console.log("## and should be unchanged (blank string) for tim");
console.log( Timothy.getPrivate("priv") );
Timothy.setPrivate("waka waka");
console.log("## but should now be 'waka waka' for tim");
console.log( Timothy.getPrivate("priv") );
console.log("## and still 'waka' for jessica");
console.log( Jessica.getPrivate("priv") );
// test public vars and collisions //
console.log("## timothy's name should be tim... then changed to timothy");
console.log( Timothy.name );
Timothy.name = "Timothy";
console.log( Timothy.name );
console.log("## make sure Jessica's name has not changed");
console.log( Jessica.name );
// test supers.. //
console.log("## check super ('mammal super: 2: 0', 'overridden', 'dog mammal super: 4: 5')");
console.log( Jessica.checkSuper(), ", ", Beagle.checkSuper(), ", ", Beagle.getSuper() );
console.log("## check super collision ('dog mammal super: 4: 5, dog mammal super: 3: 5')");
Terrier.legs = 3; // i guess he's wounded
console.log( Beagle.getSuper(), ", ", Terrier.getSuper() );
// output the objects for manual checking //
console.log("## output Beagle object");
console.log(Beagle);
console.log("## output Jessica object");
console.log(Jessica);
console.log("## output Timothy object");
console.log(Timothy);
}();
</script>
</head>
<body>
</body>
</html>