Creating an array-based Stack class
We are going to create our own class to represent a stack. The source code for this chapter is available inside the src/04-stack
folder on GitHub.
We will start by creating the stack.js
file which will contain our class that represents a stack using an array-based approach.
First, we will declare our Stack
 class:
class Stack {
#items = []; // {1}
// other methods
}
We need a data structure that will store the elements of the stack. We can use an array to do this as we are already familiar with the array data structure ({1}
). Also, note the prefix of the variable items
: we are using a hash #
prefix. This means the #items
property can only be referenced inside the Stack
class. This will allow us to protect this private array as the array data structure allows us to add or remove elements from any position in the data structure. Since the stack follows the LIFO principle, we will limit the functionalities that will be available for the insertion...