我正在尝试在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;
}
}
子节点不会在创建对象的同时声明,因此稍后会设置。就目前而言,我得到一个未定义的错误"。如何在构造函数外部设置子节点?