类内部方法中的Javascript递归(此行为)

问题描述 投票:0回答:2

问题出在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);


})()
javascript recursion scope tree this
2个回答
1
投票
您在循环中分配了父母的父母。而是使用一个变量来循环父母。

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; }

-1
投票
似乎与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(); } } }

© www.soinside.com 2019 - 2024. All rights reserved.