JS队列数组调试错误

时间:2017-11-10 16:00:17

标签: javascript arrays debugging

我收到此错误,

Practice.html:格式化:20 Uncaught TypeError: queueArray.push不是函数     在Queue.add(Practice.html:格式:20)     在Practice.html:格式化:30

但推不应该是一个功能。它应该是在数组上执行的方法。那么这意味着什么?

 var tickerArray = ['BP', 'AMZN', 'EARK'];
    //also tried putting queueArray here because that would make it global, so i guess it isnt a scope issue?

    function Queue() {
        this.top = 0; //first item in the stack
        var queueArray = []; //array to hold items
    }

    Queue.prototype.add = function(obj) {
        queueArray.push(obj);
    }

    Queue.prototype.get = function() {
        return queueArray.splice(this.top, 1);
    }


    var queueArray = new Queue();

    for (i = 0; i < tickerArray.length; i++) {
        queueArray.add(tickerArray[i]);
    }

    console.log(q.get());

1 个答案:

答案 0 :(得分:0)

queueArray设为this.queueArray,将return this.queueArray设置为.get()

&#13;
&#13;
var tickerArray = ['BP', 'AMZN', 'EARK'];

function Queue() {
  this.top = 0; //first item in the stack
  this.queueArray = []; //array to hold items
}

Queue.prototype.add = function(obj) {
  this.queueArray.push(obj);
}

Queue.prototype.get = function() {
  this.queueArray.splice(this.top, 1);
  return this.queueArray; // `return` `this.queueArray` from the function
}

var queueArray = new Queue();

for (i = 0; i < tickerArray.length; i++) {
  queueArray.add(tickerArray[i]);
}

console.log(queueArray.get());
&#13;
&#13;
&#13;

相关问题