首页 > 解决方案 > 懒类参数设置

问题描述

我正在尝试在 javascript 中创建树结构。这是节点类:

class NodeTree {
constructor(parent, chkAttacked, chkType, coords) {
    var children; 
    this.parent = parent;
    this.chkAttacked = chkAttacked;
    this.chkType = chkType;
    this.coords = coords;
}

setChild(node) {
    children.push(node);
}

getCoords() {
    return coords; 
}

getParent() { 
    return parent;
}

getChildren() { 
    return children;
}

getChkAttacked() { 
    return chkAttacked;
}

getChkType() {
    return chkType;
}
}

子节点不会在创建对象的同时被声明,因此稍后设置。就目前而言,我得到一个“未定义的错误”。如何在构造函数之外设置子节点?

标签: javascriptdata-structurestree

解决方案


推荐阅读