包含的辅助类的节点ReferenceError

时间:2016-03-03 14:27:56

标签: javascript node.js

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

是否有人能够解释此代码段中出现的问题?

cracking.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;

test.js

var cracking = require('./cracking.js');

var shelter = new cracking.ShelterQueue();
shelter.enqueue(new cracking.Cat());

1 个答案:

答案 0 :(得分:-1)

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

这是固定的cracking.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;