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