Javascript Data Structure Stack via class and Symbol

Description

Javascript Data Structure Stack via class and Symbol

let _items = Symbol();

class Stack2 {/* w w  w  .ja va2  s  .  c  o m*/

    constructor () {
        this[_items] = [];
    }

    push(element){
        this[_items].push(element);
    }

    pop(){
        return this[_items].pop();
    }

    peek(){
        return this[_items][this[_items].length-1];
    }

    isEmpty(){
        return this[_items].length == 0;
    }

    size(){
        return this[_items].length;
    }

    clear(){
        this[_items] = [];
    }

    print(){
        console.log(this.toString());
    }

    toString(){
        return this[_items].toString();
    }
}

let s = new Stack2();
s.push("a");
s.push("b");
s.push("c");
s.print();



PreviousNext

Related