Skip to content

Commit 48108dd

Browse files
committed
code refactor
1 parent 14cb02e commit 48108dd

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

chapter03/01-Stack.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
11
function Stack() {
22

3-
this.items = [];
3+
var items = [];
44

55
this.push = function(element){
6-
this.items.push(element);
6+
items.push(element);
77
};
88

99
this.pop = function(){
10-
return this.items.pop();
10+
return items.pop();
1111
};
1212

1313
this.peek = function(){
14-
return this.items[this.items.length-1];
14+
return items[items.length-1];
1515
};
1616

1717
this.isEmpty = function(){
18-
return this.items.length == 0;
18+
return items.length == 0;
1919
};
2020

2121
this.size = function(){
22-
return this.items.length;
22+
return items.length;
23+
};
24+
25+
this.clear = function(){
26+
items = [];
2327
};
2428

2529
this.print = function(){
26-
console.log(this.items.toString());
30+
console.log(items.toString());
31+
};
32+
33+
this.toString = function(){
34+
return items.toString();
2735
};
2836
}

chapter04/01-Queue.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
11
function Queue() {
22

3-
this.items = [];
3+
var items = [];
44

55
this.enqueue = function(element){
6-
this.items.push(element);
6+
items.push(element);
77
};
88

99
this.dequeue = function(){
10-
return this.items.shift();
10+
return items.shift();
1111
};
1212

1313
this.front = function(){
14-
return this.items[0];
14+
return items[0];
1515
};
1616

1717
this.isEmpty = function(){
18-
return this.items.length == 0;
18+
return items.length == 0;
19+
};
20+
21+
this.clear = function(){
22+
items = [];
1923
};
2024

2125
this.size = function(){
22-
return this.items.length;
26+
return items.length;
2327
};
2428

2529
this.print = function(){
26-
console.log(this.items.toString());
30+
console.log(items.toString());
2731
};
2832
}

0 commit comments

Comments
 (0)