节点引用包含的帮助程序类上的错误

Node ReferenceError on included helper class

本文关键字:错误 帮助程序 包含 节点 引用      更新时间:2023-09-26

运行以下代码时,我收到一个引用错误,指出在使用ShelterQueue.enqueue()时未定义LinkedList。但是,我在与ShelterQueue相同的文件中定义了LinkedList,并且我通过它的名称引用了它,这让我觉得它应该可用。

有人碰巧能够解释这个片段中出了什么问题吗?

开裂.js

LinkedList = function(data){
  this.data = data;
  this.next = null;
  this.addToEnd = function(data){
    n = this;
    while (n.next !== null){
      n = n.next;
    }
    var b = new LinkedList(data);
    n.next = b;
    return b;
  };
};
Dog = function(){};
Cat = function(){};
ShelterQueue = function(){
  this.cat = null;
  this.dog = null;
  this.all = null;
  this.enqueue = function(item){
    var node = null;
    if (this.all === null){
      node = new LinkedList(item);
      this.all = node;
    }
    else{
      node = this.all.addToEnd(item);
    }
    if (item instanceof Dog){
      if (this.dog === null){
        this.dog = node;
      }
    }
    else if (this.cat === null){
      this.cat = node;
    }
  };
  this.dequeueAny = function(){
    while (this.all !== this.cat && this.all == this.dog){
      this.all = this.all.next;
    }
    item = this.all;
    this.all = this.all.next;
    if (this.cat === item){
      this.cat = this.cat.next;
      while (!(this.cat instanceof Cat)){
        this.cat = this.cat.next;
      }
    }
    else{
      this.dog = this.dog.next;
      while(!(this.dog instanceof Dog)){
        this.dog = this.dog.next;
      }
    }
    return item;
  };
  this.dequeueType = function(name, type){
    var item = this[name];
    var next = item.next;
    while(!(next instanceof type) && next !== null){
      next = next.next;
    }
    this[name] = next;
    return item;
  };
  this.dequeueCat = function(){
    return this.dequeueType('cat', Cat);
  };
  this.dequeueDog = function(){
    return this.dequeueType('dog', Dog);
  };
};
exports.ShelterQueue = ShelterQueue;
exports.LinkedList = LinkedList;
exports.Cat = Cat;
exports.Dog = Dog;

测试.js

var cracking = require('./cracking.js');
var shelter = new cracking.ShelterQueue();
shelter.enqueue(new cracking.Cat());

我最终修复了它,但我不确定如何修复它。我认为这与最初将我的类定义为函数有关。

这是固定的裂缝.js

function LinkedList(data){
  this.data = data;
  this.next = null;
}
LinkedList.prototype.addToEnd = function(data){
  n = this;
  while (n.next !== null){
    n = n.next;
  }
  var b = new LinkedList(data);
  n.next = b;
  return b;
};
function Dog(){}
function Cat(){}
function ShelterQueue(){
  this.cat = null;
  this.dog = null;
  this.all = null;
}
ShelterQueue.prototype.enqueue = function(item){
  var node = null;
  if (this.all === null){
    node = new LinkedList(item);
    this.all = node;
  }
  else{
    node = this.all.addToEnd(item);
  }
  if (item instanceof Dog){
    if (this.dog === null){
      this.dog = node;
    }
  }
  else if (this.cat === null){
    this.cat = node;
  }
};
ShelterQueue.prototype.dequeueAny = function(){
  while (this.all !== this.cat && this.all == this.dog){
    this.all = this.all.next;
  }
  item = this.all;
  this.all = this.all.next;
  if (this.cat === item){
    this.cat = this.cat.next;
    while (!(this.cat instanceof Cat)){
      this.cat = this.cat.next;
    }
  }
  else{
    this.dog = this.dog.next;
    while(!(this.dog instanceof Dog)){
      this.dog = this.dog.next;
    }
  }
  return item;
};
ShelterQueue.prototype.dequeueType = function(name, type){
  var item = this[name];
  var next = item.next;
  while(!(next instanceof type) && next !== null){
    next = next.next;
  }
  this[name] = next;
  return item;
};
ShelterQueue.prototype.dequeueCat = function(){
  return this.dequeueType('cat', Cat);
};
ShelterQueue.prototype.dequeueDog = function(){
  return this.dequeueType('dog', Dog);
};
exports.ShelterQueue = ShelterQueue;
exports.LinkedList = LinkedList;
exports.Cat = Cat;
exports.Dog = Dog;