问题
So I'm trying to write a Data Structures Visualizer with JS (so that I can host it online). It seems as though my JS ignoring my variables (and claiming some functions don't exist) and I can't figure out why. I'd appreciate the help.
var stack = new Stack();
var defaultValueCounter = 0;
function push() {
var value = document.getElementById("add").value;
if (value === "") {
defaultValueCounter++;
value = defaultValueCounter;
}
//console.log(stack + ", " + value)
stack.push(value);
addCol(value);
stack.print();
document.getElementById("add").value = "";
}
In that code, it seems to ignore stack for some reason (initializes to undefined). I've tested this hypothesis by moving the declaration inside the push() function, and it works (although for obvious reasons, my Stack can only contain 1 element). What can I do to fix it
Edit: Sharing my Stack implementation
function Node() {
this.value;
this.next ;
}
var Stack= function(){
this.head;
}
Node.prototype.insert=function(value) {
var current = this;
if (current.value === undefined) { //has nothing yet
current.value = value; //insert here
return;
}
if(current.next === undefined) { //completely null
current.next = new Node();//want new node
}
var c = current.next;
c.insert(value);
}
Stack.prototype.push= function(value) {
if(value==undefined || value==""){
throw "Please input proper value (number)"
}
if(this.head==undefined){//nothing exists yet
this.head=new Node();
this.head.value=value;
}else{//nonempty stack
var c=this.head;
c.next=new Node();
c.next=this.head;
c.value=value;
this.head=c;
}
}
Stack.prototype.top= function() {
if(this.head==undefined){//nothing exists yet
throw "Trying to get top of null"
}else{//nonempty stack
return this.head.value;
}
}
Stack.prototype.pop= function() {
if(this.head==undefined){//nothing exists yet
throw "Trying to get top of null"
}else{//nonempty stack
var val=this.head.value;
this.head=this.head.next;
return val;
}
}
Stack.prototype.print= function(){
//debugging purposes
var c=new Node();
c.value=this.head.value
c.next=this.head.next
while(c.value!=undefined){
console.log(c.value)
c=c.next
}
console.log("Head: "+ this.value)
}
Edit: It seems as though the code is not initializing the stack at the beginning. What can I do to resolve this?
回答1:
For some reason, I resolved all issues by deleting top(). Not sure why. Will investigate my code futher
回答2:
You perhaps need to use
function Stack() {
instead of
var Stack = function() {
as function definitions are hoisted, where as only var declarations, not var definitions are hoisted
This explains why Stack
is undefined
Also, your nonempty stack code should be
}else{//nonempty stack
var c=new Node;
c.next = this.head;
c.value=value;
this.head=c;
}
回答3:
You have a problem with scope. stack
is declared outside of the function and not passed to the function. Hence, it is undefined
when you try to access it.
To fix this, ensure that the method push()
operates on the stack
variable by making variable visible to the function.
来源:https://stackoverflow.com/questions/61857251/javascript-variable-being-ignored