-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
85 lines (63 loc) · 1.7 KB
/
main.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
'use strict';
// First way using local variable as the private list
let _list = [];
class Stack {
get count() {
return _list.length;
}
peek() {
if (_list.length == 0) throw new Error('Stack is empty.');
return _list[_list.length - 1];
}
push(value) {
_list.push(value);
}
pop() {
if (_list.length == 0) throw new Error('Stack is empty.');
return _list.pop();
}
}
// // Second way using WeakMap as a variable to hold the private list
// let _list = new WeakMap();
// class Stack {
// constructor() {
// _list.set(this, []);
// }
// get count() {
// return _list.get(this).length;
// }
// peek() {
// const _items = _list.get(this);
// if (_items.length == 0) throw new Error('Stack is empty.');
// return _items[_items.length - 1];
// }
// push(value) {
// _list.get(this).push(value);
// }
// pop() {
// const _items = _list.get(this);
// if (_items.length == 0) throw new Error('Stack is empty.');
// return _items.pop();
// }
// }
// // Third way using Symbol variable as the private list
// let _list = Symbol();
// class Stack {
// constructor() {
// this[_list] = [];
// }
// get count() {
// return this[_list].length;
// }
// peek() {
// if (this[_list].length == 0) throw new Error('Stack is empty.');
// return this[_list][this[_list].length - 1];
// }
// push(value) {
// this[_list].push(value);
// }
// pop() {
// if (this[_list].length == 0) throw new Error('Stack is empty.');
// return this[_list].pop();
// }
// }