File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
// Case 2: Active entity
4+
// for example: Collection, Stream, Scheduler
5+
6+
class Collection {
7+
constructor() {
8+
this.keys = [];
9+
this.values = [];
10+
}
11+
12+
set(key, value) {
13+
this.keys.push(key);
14+
this.values.push(value);
15+
}
16+
17+
get(key) {
18+
const index = this.keys.indexOf(key);
19+
if (index === -1) return;
20+
return this.values[index];
21+
}
22+
}
23+
24+
// Usage
25+
26+
const collection = new Collection();
27+
collection.set('name', 'Marcus Aurelius');
28+
const name = collection.get('name');
29+
console.dir({ name });
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<script type="application/javascript">
5+
6+
// Case 3: Visul component
7+
8+
class PageCaption extends HTMLElement {
9+
constructor() {
10+
super();
11+
this.attachShadow({ mode: 'open' });
12+
}
13+
14+
connectedCallback() {
15+
this.shadowRoot.innerHTML = `<h1>${this.getAttribute('name')}</h1>`;
16+
}
17+
}
18+
19+
window.customElements.define('page-caption', PageCaption);
20+
21+
</script>
22+
</head>
23+
<body>
24+
<page-caption name="Marcus">Marcus Aurelius</page-caption>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)