首页 > javascript > 如何更改堆栈push()第二个结果的下一个是[object]?

如何更改堆栈push()第二个结果的下一个是[object]? (How to change stack push() the second result's next is [object]?)

2019-03-07 javascriptstack

问题

我正在构建一个Stack数据结构

我遇到了一个问题

结果不是我想要的

原来的结果是

Stack {top:Node {value:'Youtube',next:Node {value:'Google',next:[Node]}},bottom:Node {value:'Twitter',next:null},length:3}

我想要的结果是

Stack {top:Node {value:'Youtube',next:Node {value:'Google',next:[Object]}},bottom:Node {value:'Twitter',next:null},length:3}

我认为问题出在我的push()方法中。

如何将下一个[Node]转换为下一个[Object]?

这是我的JS:

class Node {
  constructor(value){
    this.value = value;
    this.next = null;
  }
}

class Stack {
  constructor(){
  this.top = null;
  this.bottom = null;
  this.length = 0;
  }
  peek() {
    if(this.top){
      return this.top;
    }else{
      return 'Empty';
    }
  }
  push(value){
     let node = new Node(value);
     if(this.top){
      node.next=this.top;
      this.top=node;
     }else{
       this.top=node;
        this.bottom=node;
      }
        this.length++;
        return this;
      }
     pop(){
        if(this.top===this.bottom){
        this.bottom=null;
       }
     if(this.top){
         let itemTop=this.top;
         this.top=this.top.next;
         this.length--;
          return this;
      }else{
         console.log('Stack is empty')
         return false;
       }
      }
     isEmpty(){
         return this.length>=1
     }
    }

    const myStack = new Stack();
    myStack.push("Twitter");
    myStack.push('Google');
    myStack.push('Youtube');
    //should return Stack { top: Node { value: 'Youtube', next: Node { value: 'Google', next: [Object] } }, bottom: Node { value: 'Twitter', next: null }, length: 3 }


解决方法

这已经是正确的了。[Object]是javascript中对象的字符串表示,以避免嵌套的地狱,这可能会混淆用户。这就是javascript的方式。

您可以使用JSON.stringify()在字符串中查看对象的确切表示。

或者您可以在浏览器中尝试console.log。

这是我在浏览器中控制对象时控制对象的结果。

在此输入图像描述

问题

I was building a Stack data structure

and i meet a problem

the result is not what i wanted

Original result is

Stack { top: Node { value: 'Youtube', next: Node { value: 'Google', next: [Node] } }, bottom: Node { value: 'Twitter', next: null }, length: 3 }

What i want in result is

Stack { top: Node { value: 'Youtube', next: Node { value: 'Google', next: [Object] } }, bottom: Node { value: 'Twitter', next: null }, length: 3 }

I think the problem is in my push() method.

How to turn next[Node] into next[Object]?

Here is my JS:

class Node {
  constructor(value){
    this.value = value;
    this.next = null;
  }
}

class Stack {
  constructor(){
  this.top = null;
  this.bottom = null;
  this.length = 0;
  }
  peek() {
    if(this.top){
      return this.top;
    }else{
      return 'Empty';
    }
  }
  push(value){
     let node = new Node(value);
     if(this.top){
      node.next=this.top;
      this.top=node;
     }else{
       this.top=node;
        this.bottom=node;
      }
        this.length++;
        return this;
      }
     pop(){
        if(this.top===this.bottom){
        this.bottom=null;
       }
     if(this.top){
         let itemTop=this.top;
         this.top=this.top.next;
         this.length--;
          return this;
      }else{
         console.log('Stack is empty')
         return false;
       }
      }
     isEmpty(){
         return this.length>=1
     }
    }

    const myStack = new Stack();
    myStack.push("Twitter");
    myStack.push('Google');
    myStack.push('Youtube');
    //should return Stack { top: Node { value: 'Youtube', next: Node { value: 'Google', next: [Object] } }, bottom: Node { value: 'Twitter', next: null }, length: 3 }

解决方法

It is already correct. [Object] is a string representation of an object in javascript to avoid nested hell which can confuse user. That's just the way javascript is.

You can use JSON.stringify() to see the exact representation of your object in string.

Or you can try console.log in the browser.

Here is the result when i console.log your object in browser.

enter image description here

相似信息