-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjooshe.tests.htm
316 lines (223 loc) · 7.46 KB
/
jooshe.tests.htm
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jooshe Tests</title>
</head>
<body>
<script src="jooshe.min.js"></script>
<script>
(function(){
test00(); // change the function name to the test you want to run (do one at a time)
})();
function test00(){
function myClass(){return undefined;}
myClass.prototype.sayHi = function(){ console.log("Hi!"); }
console.log(new myClass); // myClass {sayHi: function}
}
function test01(){
function myClass(){return {a: "a", b: "b"};}
myClass.prototype.sayHi = function(){ console.log("Hi!"); }
console.log(new myClass); // Object {a: "a", b: "b"}
(new myClass).sayHi(); // Uncaught TypeError: Object # has no method 'sayHi'
}
function test02(){
function myClass(){
var i, me = {a: "a", b: "b"};
for(i in this) me[i] = this[i];
return me;
}
myClass.prototype.sayHi = function(){ console.log("Hi!"); };
(new myClass).sayHi(); // Hi!
console.log((new myClass) instanceof myClass); // false
}
function test03(){
function myClass(){
var i, me = document.createElement("div");
for(i in this) me[i] = this[i];
return me;
}
myClass.prototype.sayHi = function(){ console.log("Hi!"); };
(new myClass).sayHi(); // Hi!
}
function test04(){
function myClass(){
var i, me = document.createElement("input");
me.type = "text";
for(i in this) me[i] = this[i];
return me;
}
myClass.prototype.onkeyup = function(){ console.log(this.value); };
document.body.appendChild(new myClass).focus(); // type into the input and watch the console log
}
function test05(){
function myClass(){
var me = document.createElement("input");
me.type = "text";
me.addEventListener("keyup", this.keyupHandler);
return me;
}
myClass.prototype.keyupHandler = function(){ console.log(this.value); };
document.body.appendChild(new myClass).focus();
}
function test06(){
function myClass(){
var i, me = document.createElement("input");
me.type = "text";
for(i in this.listeners) me.addEventListener(i, this.listeners[i]);
return me;
}
myClass.prototype.listeners = {
keydown: function(){ console.log("down", this.value); },
keyup: function(){ console.log("up", this.value); }
};
document.body.appendChild(new myClass).focus();
}
function test07(){
function myClass(){
var i, j, me = document.createElement("input");
me.type = "text";
for(i in this)
if(i == "listeners") for(j in this[i]) me.addEventListener(j, this[i][j]);
else me[i] = this[i];
return me;
}
myClass.prototype.onkeyup = function(){ console.log("handle up", this.value); };
myClass.prototype.listeners = {
keydown: function(){ console.log("listen down", this.value); },
keyup: function(){ console.log("listen up", this.value); }
};
document.body.appendChild(new myClass).focus();
}
function test08(){
function myClass(){
var i, j, me = document.createElement("input");
me.type = "text";
for(i in this)
if(i == "listeners") for(j in this[i]) me.addEventListener(j, this[i][j]);
else if(i == "style") for(j in this[i]) me[i][j] = this[i][j];
else me[i] = this[i];
return me;
}
myClass.prototype.onkeyup = function(){ console.log("handle up", this.value); };
myClass.prototype.listeners = {
keydown: function(){ console.log("listen down", this.value); },
keyup: function(){ console.log("listen up", this.value); }
};
myClass.prototype.style = { border: "2px solid black", borderRadius: "8px", padding: "4px" };
document.body.appendChild(new myClass).focus();
}
function test09(){
function myClass(){
var i, j, me = document.createElement("input");
me.type = "text";
for(i in this)
if(i == "listeners") for(j in this[i]) me.addEventListener(j, this[i][j]);
else if(i == "style") for(j in this[i]) me[i][j] = this[i][j];
else if(i == "$") { me.$ = this.$; me.$.el = me; }
else me[i] = this[i];
return me;
}
myClass.prototype.onkeyup = function(){ this.$.logIt("handle up"); };
myClass.prototype.listeners = {
keydown: function(){ this.$.logIt("listen down"); },
keyup: function(){ this.$.logIt("listen up"); }
};
myClass.prototype.style = { border: "2px solid black", borderRadius: "8px", padding: "4px" };
myClass.prototype.$ = {
logIt: function(type){ console.log(type, this.el.value); }
};
document.body.appendChild(new myClass).focus();
}
function test10(){
createClass("myClass",
function(){ return element("input", null, this); },
{
onkeyup: function(){ this.$.logIt("handle up"); },
listeners: {
keydown: function(){ this.$.logIt("listen down"); },
keyup: function(){ this.$.logIt("listen up"); }
},
style: { border: "2px solid black", borderRadius: "8px", padding: "4px" },
type: "text",
$: { logIt: function(type){ console.log(type, this.el.value); } }
}
);
document.body.appendChild(new myClass).focus();
}
function test11(){
createClass("myClass",
function(){ return element("input", { border: "2px solid black", borderRadius: "8px", padding: "4px" }, this, null, {type: "text"}); },
{
onkeyup: function(){ this.$.logIt("handle up"); },
listeners: {
keydown: function(){ this.$.logIt("listen down"); },
keyup: function(){ this.$.logIt("listen up"); }
},
$: { logIt: function(type){ console.log(type, this.el.value); } }
}
);
document.body.appendChild(new myClass).focus();
}
function test12(){
createClass("myClass",
function(i){ return element("input", null, this, {index: i}); },
{
onkeyup: function(){ this.$.logIt("handle up"); },
listeners: {
keydown: function(){ this.$.logIt("listen down"); },
keyup: function(){ this.$.logIt("listen up"); }
},
style: { border: "2px solid black", borderRadius: "8px", padding: "4px" },
type: "text",
$: { logIt: function(type){ console.log(type, this.index, this.el.value); } }
}
);
for(var i=0;i<10;i++) document.body.appendChild(new myClass(i)).focus();
}
function test13(){
createClass("myClass", function(){ return element("input", null, this); },
{
style: { border: "2px solid black", borderRadius: "8px", padding: "4px" },
type: "text"
}
);
createClass("mySubClass", function(){ return element("input", null, this); }, myClass,
{
style: { background: "rgba(209,42,42,.1)", borderColor: "red" },
type: "text"
}
);
document.body.appendChild(new myClass);
document.body.appendChild(new mySubClass).focus();
}
function test14(){
createClass("momClass", null,
{ $: { x: function(){ console.log("I'm x"); } } }
);
createClass("dadClass", null,
{ $: { y: function(){ console.log("I'm y"); } } }
);
createClass("childClass", null,
{
$: {
x: momClass.prototype.$.x,
y: dadClass.prototype.$.y,
xy: function(){ console.log("I'm xy!"); }
}
}
);
var myChild = new childClass;
myChild.$.x(); // I'm x!
myChild.$.y(); // I'm y!
myChild.$.xy(); // I'm xy!
}
function test15(){
createClass("myClass",function(){ return element("div", null, this); });
var o = new myClass;
console.log(o instanceof myClass); // false - as mentioned earlier, instanceof is broken
console.log(o.$.__class__ == myClass); // true - effective workaround!</pre>
}
</script>
</body>
</html>