forked from WebReflection/classtrophobic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.html
87 lines (73 loc) · 2.53 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Classtrophobic Test Page</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<style>
my-div { cursor: pointer; }
my-element, my-div, button[is=my-button] { margin: 8px; }
</style>
<script defer src="//cdnjs.cloudflare.com/ajax/libs/document-register-element/1.3.0/document-register-element.js"></script>
<script defer src="classtrophobic.min.js"></script>
<script>
this.onload = function () {
// Native Subclass
const List = Class({
extends: Array,
constructor(...args) {
this.super().push(...args);
}
});
const ml = new List(1, 2, 3);
console.log(
ml instanceof Array, // true
ml instanceof List, // true
ml // [1, 2, 3]
);
// - - - - - - - - - - - - - - - - - - - - - -
// Custom Element
const MyElement = Class({
extends: HTMLElement
});
customElements.define('my-element', MyElement);
document.body.appendChild(new MyElement).textContent = 'my element';
document.body.appendChild(
document.createElement('my-element')
).textContent = 'my element';
// - - - - - - - - - - - - - - - - - - - - - -
// Custom Element with super()
const MyDiv = Class({
extends: HTMLElement,
constructor(...args) {
this.super(...args);
this.addEventListener('click', console.log);
}
});
customElements.define('my-div', MyDiv);
document.body.appendChild(new MyDiv).textContent = 'my div';
document.body.appendChild(
document.createElement('my-div')
).textContent = 'my div';
// - - - - - - - - - - - - - - - - - - - - - -
// Custom Element native extend with super()
// requires Custom Elements V1 polyfill
// https://github.com/WebReflection/document-register-element
const MyButton = Class({
extends: HTMLButtonElement,
constructor(...args) {
this.super(...args);
this.addEventListener('click', console.log);
}
});
customElements.define('my-button', MyButton, {extends: 'button'});
document.body.appendChild(new MyButton).textContent = 'my button';
document.body.appendChild(
document.createElement('button', {is:'my-button'})
).textContent = 'my button';
document.body.style.backgroundColor = '#0FA';
};
</script>
</head>
<body></body>
</html>