节点引用包含的帮助程序类上的错误
Node ReferenceError on included helper class
运行以下代码时,我收到一个引用错误,指出在使用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;
相关文章:
- AngularJS错误:提供程序必须从$get工厂方法返回值
- {{#each}}内部的Handlebar帮助程序
- 修复AngularJS错误:提供程序必须从$get工厂方法返回值
- Handlebars帮助程序未获取变量的值
- 为什么没有调用我的流错误处理程序
- 如果文件不存在,fs.watch是否有错误处理程序
- Node Express Handlebars帮助程序未返回函数的结果
- 如何让应用程序帮助程序方法在发送的请求为 JS 格式时工作
- 从帮助程序异步返回值
- 无法使用模板帮助程序设置正文类
- 在呈现模板之前调用帮助程序
- Javascript 运行时错误:“应用程序未定义”
- 花式框 V2 按钮帮助程序冲突
- BreezeJS中央错误处理程序和自动重试
- 在帮助程序处获取对象值的车把,无需使用数据进行编译
- 节点引用包含的帮助程序类上的错误
- 车把自定义帮助程序错误:“options.fn 不是函数”
- 嵌套车把帮助程序引发错误
- 模板帮助程序中出现异常:错误:Can'不要在没有DOM的组件上使用$
- 为什么这个模板帮助程序会产生错误,但仍然有效