首页 > 解决方案 > 我正在尝试从堆栈创建队列,但出现错误“找不到所需的变量”

问题描述

我是编程新手,我有一个练习来为堆栈创建一个队列,但是当我验证它时给我一个错误:

“队列是一个类

TypeError:未定义不是对象(评估“Queue.prototype”)

const Stack = require('./stack'); 

class Queue {
    constructor(){ 
this.first = new Stack();
this.second = new Stack();
    }

    add(record) {
        this.first.push(record);
    }

    remove() {
        while (this.first.peek()) {
            this.second.push(this.first.pop());
        }

        const record = this.second.pop();

        while (this.second.peek()) {
            this.first.push(this.second.pop());

        }
        return(record); 
    }

    peek() {
        while (this.first.peek()) {
            this.second.push(this.first.pop());
        }

        const record = this.second.peek();

        while (this.second.peek()) {
            this.first.push(this.second.pop()); 
        }

        return record; 
    }

}
module.exports = Queue;

标签: javascript

解决方案


一个简单的例子


class Animal{
  species;
  constructor(){}
}

class Dog extends Animal{
  name;
  constructor(){
    super();
  }
}

let max = new Dog();


推荐阅读