忽略JavaScript变量

忽略JavaScript变量,javascript,data-structures,linked-list,stack,Javascript,Data Structures,Linked List,Stack,所以我尝试用JS编写一个数据结构可视化工具(这样我就可以在线托管它)。似乎我的JS忽略了我的变量(并声称一些函数不存在),我不知道为什么。我很感激你的帮助 var stack = new Stack(); var defaultValueCounter = 0; function push() { var value = document.getElementById("add").value; if (value === "") { defaultValueCo

所以我尝试用JS编写一个数据结构可视化工具(这样我就可以在线托管它)。似乎我的JS忽略了我的变量(并声称一些函数不存在),我不知道为什么。我很感激你的帮助

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 = "";
}
在该代码中,它似乎出于某种原因忽略了堆栈(初始化为未定义)。我已经通过在push()函数中移动声明来测试这个假设,它是有效的(尽管出于明显的原因,我的堆栈只能包含1个元素)。我能做些什么来修复它

编辑: 共享我的堆栈实现

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)
}

编辑:代码似乎没有在开始时初始化堆栈。我能做些什么来解决这个问题

范围有问题<代码>堆栈在函数外部声明,不传递给函数。因此,当您试图访问它时,它是未定义的


要解决此问题,请确保方法
push()
通过使变量对函数可见来对
堆栈
变量进行操作

您可能需要使用

function Stack() {
而不是

var Stack = function() {
当函数定义被提升时,其中仅提升变量声明,而不提升变量定义

这解释了为什么
堆栈
未定义的

另外,您的非空堆栈代码应该是

}else{//nonempty stack
    var c=new Node;
    c.next = this.head;
    c.value=value;
    this.head=c;
}   

出于某种原因,我通过删除top()解决了所有问题。不知道为什么。将进一步研究我的代码

函数实际上有权访问堆栈变量,因为它附加了全局作用域,即Windows,难道它没有访问权限吗?我了解到,函数外部声明的任何变量都是全局变量。这不是真正的作用域问题,而是JavaScription中函数和变量之间的区别。如果它附加到窗口,则必须定义它,只要被调用方具有相同的作用域@pasha如何调用
push
函数?单击一个按钮。如前所述,我不知道为什么,但删除顶部固定的一切你能分享你的堆栈定义吗?从>“创建用户定义的对象需要两个步骤:>>通过编写函数定义对象类型。使用new创建>对象的实例。若要定义对象类型,请为>对象类型创建一个函数,指定其名称和属性。对象可以>具有本身是另一个对象的属性。”你能分享你的堆栈实现吗。我仍然不确定我做错了什么你的
//非空堆栈
代码正在覆盖这个头上的值<代码>c.next=新节点();c、 next=this.head基本上只是创建一个新节点,然后在下一个语句中完全丢弃它设置c=this.head是否将它们指向相同的位置?我以为它会把它们复制在一起。如果是这样的话,我的打印代码是否更适合复制该值?同样,它仍然说Stack DNEif
var Stack=new Stack()失败,是在此代码之前定义
堆栈的代码吗。。。不要使用
var Stack=function(){
try
function Stack(){
,因为函数定义被提升,而只有var声明被提升