首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >包含帮助器类的节点ReferenceError

包含帮助器类的节点ReferenceError
EN

Stack Overflow用户
提问于 2016-03-03 14:27:56
回答 1查看 152关注 0票数 1

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

有没有人能解释一下这段片段到底出了什么问题?

cracking.js

代码语言:javascript
复制
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

代码语言:javascript
复制
var cracking = require('./cracking.js');

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

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-03 15:37:11

我最终还是把它修好了,但我不太清楚是怎么解决的。我认为这与最初将我的类定义为函数有关。

这是固定的cracking.js

代码语言:javascript
复制
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;
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35774882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档