-
Notifications
You must be signed in to change notification settings - Fork 32
/
decorators_interface.html
97 lines (86 loc) · 3.26 KB
/
decorators_interface.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Interface Implementation Example</title>
</head>
<body>
<h1>Interface Implementation Example</h1>
<form id="todoForm">
<label for="nameInput">Todo name:</label>
<input type="text" id="nameInput" />
<br />
<label for="summaryInput">Summary:</label>
<input type="text" id="summaryInput" />
<br />
<button type="submit">Create Todo</button>
</form>
<div id="todoOutput"></div>
<script>
// Interface constructor
function Interface(name, methods) {
if (arguments.length !== 2) {
throw new Error('Interface constructor called with ' + arguments.length + ' arguments, but expected exactly 2.');
}
this.name = name;
this.methods = [];
for (let i = 0; i < methods.length; i++) {
if (typeof methods[i] !== 'string') {
throw new Error('Interface constructor expects method names to be passed in as strings.');
}
this.methods.push(methods[i]);
}
}
// Ensure implementation of an interface
Interface.ensureImplements = function (object, ...interfaces) {
if (arguments.length < 2) {
throw new Error('Function Interface.ensureImplements called with ' + arguments.length + ' arguments, but expected at least 2.');
}
for (let i = 0; i < interfaces.length; i++) {
const interfaceInstance = interfaces[i];
if (interfaceInstance.constructor !== Interface) {
throw new Error('Function Interface.ensureImplements expects arguments two and above to be instances of Interface.');
}
for (let j = 0; j < interfaceInstance.methods.length; j++) {
const method = interfaceInstance.methods[j];
if (!object[method] || typeof object[method] !== 'function') {
throw new Error('Function Interface.ensureImplements: object does not implement the ' + interfaceInstance.name + ' interface. Method ' + method + ' was not found.');
}
}
}
};
// Create interfaces using the Interface constructor
const reminder = new Interface('List', ['summary']);
// Todo constructor
class Todo {
constructor({ name, summary }) {
this.name = name;
this.methods = {
summary() {
return summary;
}
};
}
}
// Todo form
const todoForm = document.getElementById('todoForm');
todoForm.addEventListener('submit', function (event) {
event.preventDefault();
const nameInput = document.getElementById('nameInput');
const summaryInput = document.getElementById('summaryInput');
const name = nameInput.value;
const summary = summaryInput.value;
const todoItem = new Todo({ name, summary });
displayTodoItem(todoItem);
});
// Display Todo details
function displayTodoItem(todoItem) {
const output = document.getElementById('todoOutput');
output.innerHTML = `
<p><strong>Todo name:</strong> ${todoItem.name}</p>
<p><strong>Summary:</strong> ${todoItem.methods.summary()}</p>
`;
}
</script>
</body>
</html>