为什么我的 JS 只排队 return 第一个单词而不是整个字符串?

Why does my JS queue only return first word instead of whole string?

为什么它只出队 'first' 而不是 'first in line'? 正如您在下面的测试结果中看到的,期望值是“first in line”,但接收到的值只是“first”,这是怎么回事?

这是我的队列class:

function Queue() {}

Queue.prototype.arr = [];
Queue.prototype.sz = 0;

Queue.prototype.enqueue = function(n) {
  this.sz++;
  this.arr.push(n);
}

Queue.prototype.dequeue = function() {
  if(this.sz === 0) {
  return undefined;
  }
  else if(this.sz > 0){
  this.sz--;
  return this.arr.shift();
  }
}

Queue.prototype.size = function() {
  return this.sz;
}

这是我的测试:

it('returns queued string', function() {
    queue.enqueue('first in line');
    expect(queue.size()).toBe(1);
    expect(queue.dequeue()).toBe('first in line');
  });

这是测试结果:

● returns queued string

    expect(received).toBe(expected) // Object.is equality

    Expected: "first in line"
    Received: "first"

      76 |     queue.enqueue('first in line');
      77 |     expect(queue.size()).toBe(1);
    > 78 |     expect(queue.dequeue()).toBe('first in line');
         |                             ^

好的,我是这样解决的,我是这样重写的:

class Queue {
  constructor() {
  this.arr = [];
  this.sz = 0;
 }

enqueue(n) {
  this.sz++;
  this.arr.push(n);
}

dequeue() {
  if(this.sz === 0) {
  return undefined;
  }
  else if(this.sz > 0){
  this.sz--;
  return this.arr.shift();
  }
}

size() {
  return this.sz;
}
}