在运行下面的代码时,我将得到一个引用错误,它声明在使用LinkedList ()时没有定义ShelterQueue.enqueue()。但是,我在与LinkedList相同的文件中定义了ShelterQueue,我在那里引用了它的名称,这让我觉得它应该是可用的。
有没有人能解释一下这段片段到底出了什么问题?
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());发布于 2016-03-03 15:37:11
我最终还是把它修好了,但我不太清楚是怎么解决的。我认为这与最初将我的类定义为函数有关。
这是固定的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;https://stackoverflow.com/questions/35774882
复制相似问题