Javascript Recursion inside class method(此行为)

问题在于 toString 函数内部,这并没有实现函数调用自己的范围:我需要一些东西来解决问题,'这'是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);



})()


开满天机
浏览 69回答 2
2回答

陪伴而非守候

在循环中分配父项的父项。而是使用变量来循环父项。class Node {&nbsp; &nbsp; constructor(data) {&nbsp; &nbsp; &nbsp; &nbsp; this.data = data;&nbsp; &nbsp; &nbsp; &nbsp; this.parent = null;&nbsp; &nbsp; &nbsp; &nbsp; this.children = [];&nbsp; &nbsp; &nbsp; &nbsp; if (!Node.nodes) Node.nodes = [];&nbsp; &nbsp; &nbsp; &nbsp; Node.nodes.push(this);&nbsp; &nbsp; &nbsp; &nbsp; this.index = Node.nodes.length;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; addChild(node) {&nbsp; &nbsp; &nbsp; &nbsp; node.parent = this;&nbsp; &nbsp; &nbsp; &nbsp; this.children.push(node);&nbsp; &nbsp; }&nbsp; &nbsp; getLevel() {&nbsp; &nbsp; &nbsp; &nbsp; let level = 0;&nbsp; &nbsp; &nbsp; &nbsp; let parent = this.parent;&nbsp; &nbsp;// start with parent&nbsp; &nbsp; &nbsp; &nbsp; while (parent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // check value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; level += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parent = parent.parent; // assign parent&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //console.log('lvl',level);&nbsp; &nbsp; &nbsp; &nbsp; return level;&nbsp; &nbsp; }&nbsp; &nbsp; toString() {&nbsp; &nbsp; &nbsp; &nbsp; // console.log('lvl',this.getLevel());&nbsp; &nbsp; &nbsp; &nbsp; let prefix = " ".repeat(this.getLevel() * 3);&nbsp; &nbsp; &nbsp; &nbsp; prefix += this.getLevel() === 0 ? "" : "|__";&nbsp; &nbsp; &nbsp; &nbsp; console.log(prefix + this.index);&nbsp; &nbsp; &nbsp; &nbsp; if (this.children) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (let i = 0; i < this.children.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.children[i].toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; pathToRoot() {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }}const main = (() => {&nbsp; let root = new Node('root');&nbsp; let kid1 = new Node('kid1');&nbsp; let kid2 = new Node('kid2');&nbsp; let kid3 = new Node('kid3');&nbsp; let kid4 = new Node('kid4');&nbsp; root.addChild(kid1);&nbsp; kid1.addChild(kid2);&nbsp; kid2.addChild(kid3);&nbsp; kid3.addChild(kid4);&nbsp; console.log('kid4 lvl :', kid4.getLevel())&nbsp; root.toString(root);console.log(root);})();.as-console-wrapper { max-height: 100% !important; top: 0; }

郎朗坤

这似乎与机制无关。你缺少一个基本情况(所以递归函数将永远保持运行)。您需要考虑递归应该在什么情况下结束。thistoString (){&nbsp; &nbsp; &nbsp; &nbsp; // think of your base case here. Examples are as below&nbsp; &nbsp; &nbsp; &nbsp; // should it be prefix === ""?&nbsp; &nbsp; &nbsp; &nbsp; // should it be when length of prefix smaller or greater than specific length&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; let prefix = " ".repeat(this.getLevel()*3);&nbsp; &nbsp; &nbsp; &nbsp; prefix += this.getLevel()===0 ? "" :"|__";&nbsp; &nbsp; &nbsp; &nbsp; console.log(prefix + this.index);&nbsp; &nbsp; &nbsp; &nbsp; if(this.children){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(let i = 0; i < this.children.length; i++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.children[i].toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript