Javascript class 方法内的递归(此行为)

Javascript Recursion inside class method (this behaviour)

问题出在 toString 函数内部,这并没有在函数调用自身时实现作用域: 我需要一些东西来解决问题,'this' 是 javascript 中最糟糕的,我已经在 python 中实现了确切的数据结构和功能并且它有效......,我已经尝试绑定和箭头功能,也许你可以帮我... 以下代码的预期结果:

1
|__2
   |__3
      |__4
         |__5
class Node {

    constructor(data){
        this.data = data;
        this.parent = null;
        this.children = [];
        Node.nodes.push(this);
        this.index = Node.nodes.length;
    }

    addChild(node){
        node.parent = this;
        this.children.push(node);

    }

    getLevel(){
        let level = 0;
        while(this.parent){
            level+=1;
            this.parent = this.parent.parent;
        }
        //console.log('lvl',level);
        return level;
    }

     toString (){
        // console.log('lvl',this.getLevel());
        let prefix = " ".repeat(this.getLevel()*3);
        prefix += this.getLevel()===0 ? "" :"|__";
        console.log(prefix + this.index);

        if(this.children){
            for(let i = 0; i < this.children.length; i++){
                this.children[i].toString();
            }
        }
    }

    pathToRoot(){
        return 0;
    }

}
Node.nodes = [];

const main = (()=>{

let root = new Node('root');
let kid1 = new Node('kid1');
let kid2 = new Node('kid2');
let kid3 = new Node('kid3');
let kid4 = new Node('kid4');

root.addChild(kid1);
kid1.addChild(kid2);
kid2.addChild(kid3);
kid3.addChild(kid4);

console.log('kid4 lvl :',kid4.getLevel())

root.toString(root);


})()

好像与this机制无关。您缺少基本情况(因此递归函数将永远保持 运行 )。你需要考虑递归应该在什么情况下结束。

toString (){
        // think of your base case here. Examples are as below
        // should it be prefix === ""?
        // should it be when length of prefix smaller or greater than specific length 


        let prefix = " ".repeat(this.getLevel()*3);
        prefix += this.getLevel()===0 ? "" :"|__";
        console.log(prefix + this.index);

        if(this.children){
            for(let i = 0; i < this.children.length; i++){
                this.children[i].toString();
            }
        }
    }

您在循环中分配 parent 的 parent。而是使用变量循环 parents.

class Node {

    constructor(data) {
        this.data = data;
        this.parent = null;
        this.children = [];
        if (!Node.nodes) Node.nodes = [];
        Node.nodes.push(this);
        this.index = Node.nodes.length;
    }
   
    addChild(node) {
        node.parent = this;
        this.children.push(node);
    }

    getLevel() {
        let level = 0;
        let parent = this.parent;   // start with parent
        while (parent) {            // check value
            level += 1;
            parent = parent.parent; // assign parent
        }
        //console.log('lvl',level);
        return level;
    }

    toString() {
        // console.log('lvl',this.getLevel());
        let prefix = " ".repeat(this.getLevel() * 3);
        prefix += this.getLevel() === 0 ? "" : "|__";
        console.log(prefix + this.index);

        if (this.children) {
            for (let i = 0; i < this.children.length; i++) {
                this.children[i].toString();
            }
        }
    }

    pathToRoot() {
        return 0;
    }

}

const main = (() => {
  let root = new Node('root');
  let kid1 = new Node('kid1');
  let kid2 = new Node('kid2');
  let kid3 = new Node('kid3');
  let kid4 = new Node('kid4');

  root.addChild(kid1);
  kid1.addChild(kid2);
  kid2.addChild(kid3);
  kid3.addChild(kid4);

  console.log('kid4 lvl :', kid4.getLevel())

  root.toString(root);
console.log(root);
})();
.as-console-wrapper { max-height: 100% !important; top: 0; }