为什么我的 push() 方法不断在我的数组中插入一个“未定义”的元素

Why does my push() method keep inserting an element of 'undefined' into my array?

本文关键字:未定义 我的 一个 元素 数组 方法 push 为什么 插入      更新时间:2023-09-26

为什么我的push()方法每次都不断将'undefined'的元素作为第一个元素插入到我的数组中?

var elements;
//Create a namespace.
var dataStructures = {
//Create a class called myStack which is a property of the dataStructures (object).
myStack: function () {
    var topOfmyStack;
    //Add the push method to the myStack object
    this.push = function (element) {
        //If no value was entered then stay at zero index and insert nothing
        if (typeof (elements) === 'undefined') {
           elements = [];
            topOfmyStack = 0;
        }
        //Get the next empty space in the stack
        topOfmyStack++;
        //Place the new value in that empty space
        elements[topOfmyStack] = element;
    }
}

第一次,您将topOfmyStack设置为 0;对于每个调用(包括第一次调用),您在添加到数组之前将 1 添加到topOfmyStack。所以第一次,这变成了 1,而不是 0。

将其设置为 -1 应该可以解决此问题

var elements;
//Create a namespace.
var dataStructures = {
//Create a class called myStack which is a property of the dataStructures (object).
myStack: function () {
    var topOfmyStack;
    //Add the push method to the myStack object
    this.push = function (element) {
        //If no value was entered then stay at zero index and insert nothing
        if (typeof (elements) === 'undefined') {
           elements = [];
            topOfmyStack = -1;
        }
        //Get the next empty space in the stack
        topOfmyStack++;
        //Place the new value in that empty space
        elements[topOfmyStack] = element;
    }
}

或者更好的是将元素推到数组上:

var elements;
//Create a namespace.
var dataStructures = {
//Create a class called myStack which is a property of the dataStructures (object).
myStack: function () {
    //Add the push method to the myStack object
    this.push = function (element) {
        //If no value was entered then stay at zero index and insert nothing
        if (typeof (elements) === 'undefined') {
           elements = [];
        }
        //Place the new value in that empty space
        elements.push(element);
    }
}

这行代码

var topOfmyStack;

最初是undefined 你只在下面的代码中定义它

if (typeof (elements) === 'undefined') {
    elements = [];
    topOfmyStack = 0;
} // here topOfmyStack is still undefined

所以将代码更改为

var topOfmyStack = 0;

所以topOfmyStack++不会出错。

//Create a namespace.
var dataStructures = {
    myStack: function () {
        var elements = [];
        var topOfMyStack = 0;
    
        //Add the push method to the myStack object
        this.push = function (e) {
            if (typeof (e) !== 'undefined') {
                // Push new value.
                elements[topOfMyStack++] = e;
            }
        };
        
        this.pop = function () {
            if (topOfMyStack > 0) {
                return elements[--topOfMyStack];
            }
        };
        
        return this;
    }
}
var something;
var stack = new dataStructures.myStack();
stack.push(1);
stack.push(something);    // undefined
alert(stack.pop());       // 1