
上QQ阅读APP看书,第一时间看更新
Testing the stack
To test the Stack we have just created, let's instantiate a new stack and call out each of the methods and take a look at how they present us with data:
var stack = new Stack();
stack.push(10);
stack.push(20);
console.log(stack.items); // prints undefined -> cannot be accessed directly
console.log(stack.size()); // prints 2
console.log(stack.peek()); // prints 20
console.log(stack.pop()); // prints 20
console.log(stack.size()); // prints 1
stack.clear();
console.log(stack.size()); // prints 0
When we run the above script we see the logs as specified in the comments above. As expected, the stack provides what appears to be the expected output at each stage of the operations.