-
Notifications
You must be signed in to change notification settings - Fork 29
/
object_literals.html
67 lines (62 loc) · 2.21 KB
/
object_literals.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Object Literals Example</title>
</head>
<body>
<h1>Interactive Object Literals Example</h1>
<button id="say-something-btn">Say Something</button>
<button id="report-config-btn">Report Config</button>
<button id="update-config-btn">Update Config</button>
<div id="output"></div>
<script>
const myModule = {
myProperty: 'someValue',
myConfig: {
useCaching: true,
language: 'en',
},
saySomething() {
console.log('Where is Paul Irish debugging today?');
},
reportMyConfig() {
console.log(`Caching is: ${this.myConfig.useCaching ? 'enabled' : 'disabled'}`);
},
updateMyConfig(newConfig) {
if (typeof newConfig === 'object') {
this.myConfig = newConfig;
console.log(this.myConfig.language);
}
},
};
const saySomethingBtn = document.getElementById('say-something-btn');
const reportConfigBtn = document.getElementById('report-config-btn');
const updateConfigBtn = document.getElementById('update-config-btn');
const output = document.getElementById('output');
saySomethingBtn.addEventListener('click', () => {
myModule.saySomething();
const messageElement = document.createElement('p');
messageElement.textContent = 'Where is Paul Irish debugging today?';
output.appendChild(messageElement);
});
reportConfigBtn.addEventListener('click', () => {
myModule.reportMyConfig();
const configElement = document.createElement('p');
configElement.textContent = `Caching is: ${myModule.myConfig.useCaching ? 'enabled' : 'disabled'}`;
output.appendChild(configElement);
});
updateConfigBtn.addEventListener('click', () => {
myModule.updateMyConfig({
language: 'fr',
useCaching: false,
});
myModule.reportMyConfig();
const configElement = document.createElement('p');
configElement.textContent = `Caching is: ${myModule.myConfig.useCaching ? 'enabled' : 'disabled'}`;
output.appendChild(configElement);
});
</script>
</body>
</html>