-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
253 lines (175 loc) · 5.75 KB
/
index.js
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
/******************************************************************************
Section 2: Stop-watch Exercise
******************************************************************************/
// function SW() {
// let startTime, endTime, running, duration = 0;
// this.start = function() {
// if (running)
// throw new Error('Stopwatch already started.');
// running = true;
// startTime = new Date();
// };
// this.stop = function() {
// if (!running)
// throw new Error('Stopwatch already stopped.');
// running = false;
// endTime = new Date();
// const seconds = (endTime.getTime() - startTime.getTime())/1000;
// duration += seconds;
// };
// this.Reset = function() {
// duration = 0;
// startTime = null;
// endTime = null;
// running = null;
// };
// Object.defineProperty(this, 'duration', {
// get: function() { return duration; }
// });
// }
/******************************************************************************
Section 3: Example of "Premature Optimizatiion is the root of all evils"
******************************************************************************/
// function Stopwatch() {
// let startTime, endTime, running = false, duration = 0;
// Object.defineProperty(this, 'startTime', {
// get: function() { return startTime; },
// set: function(value) { startTime = value; }
// });
// Object.defineProperty(this, 'endTime', {
// get: function() { return endTime; },
// set: function(value) { endTime = value; }
// });
// Object.defineProperty(this, 'running', {
// get: function() { return running; },
// set: function(value) { running = value; }
// });
// Object.defineProperty(this, 'duration', {
// get: function() { return duration; },
// set: function(value) { duration = value; }
// });
// }
// Stopwatch.prototype.start = function() {
// if (this.running)
// throw new Error('Stopwatch already started.');
// this.running = true;
// this.startTime = new Date();
// };
// Stopwatch.prototype.stop = function() {
// if (!this.running)
// throw new Error('Stopwatch already stopped.');
// this.running = false;
// this.endTime = new Date();
// const seconds = (this.endTime.getTime() - this.startTime.getTime())/1000;
// this.duration += seconds;
// };
// Stopwatch.prototype.Reset = function() {
// this.duration = 0;
// this.startTime = null;
// this.endTime = null;
// this.running = null;
// };
/******************************************************************************
Section 4 - Prototypical Inheritance
******************************************************************************/
// function extend(Child, Parent)
// {
// Child.prototype = Object.create(Parent.prototype);
// Child.prototype.constructor = Child;
// }
// function Shape(color) {
// this.color = color;
// }
// Shape.prototype.duplicate = function() {
// console.log('duplicate');
// }
// function Circle(radius, color) {
// this.raidus = radius;
// Shape.call(this, color);
// }
// extend(Circle, Shape);
// Circle.prototype.draw = function () {
// console.log('draw');
// }
// Circle.prototype.duplicate = function() {
// Shape.prototype.duplicate.call(this);
// console.log('duplicate circle');
// }
// function Square(size) {
// this.size = size;
// }
// extend(Square, Shape);
// const s = new Shape();
// const c = new Circle(1, "red");
/******************************************************************************
Section 4 - Exercise 1
******************************************************************************/
// function HTMLElement () {
// this.click = function () {
// console.log('clicked');
// };
// }
// HTMLElement.prototype.focus = function () {
// console.log('focused');
// }
// function HTMLSelectElement (items = []) {
// this.items = items;
// this.addItem = function(item) {
// items.addItem(item);
// }
// this.removeItem = function(item) {
// items.splice(this.items.indexof(item), 1);
// }
// this.render = function() {
// return `
// <select>${this.items.map(item => `
// <option>${item}</option>`).join('')}
// </select>
// `;
// }
// }
// HTMLSelectElement.prototype = new HTMLElement();
// HTMLSelectElement.prototype.constructor = HTMLSelectElement;
// function HTMLImageElement(src) {
// this.src = src;
// this.render = function() {
// return `<img src=${src} />`;
// }
// }
// HTMLImageElement.prototype = new HTMLElement();
// HTMLImageElement.prototype.constructor = HTMLImageElement;
// const elements = [
// new HTMLSelectElement([1,2,3]),
// new HTMLImageElement('http://')
// ];
// for (let element of elements)
// {
// console.log(element.render());
// }
/******************************************************************************
Section 5 - Exercise 1
******************************************************************************/
const _items = new WeakMap();
class Stack {
constructor() {
_items.set(this, []);
}
push(obj){
_items.get(this).push(obj);
}
pop() {
const items = _items.get(this);
if (items.length === 0)
throw new Error('Stack is empty.');
return items.pop();
}
peek() {
const items = _items.get(this);
if (items.length === 0)
throw new Error('Stack is empty.');
return items[items.length-1];
}
count() {
return _items.get(this).length;
}
}