我有2个类,生物类将node数组作为属性。但这些生物也会被放在一个阵列中。我希望能够从任何节点中获取生物索引。目前我所能做的就是能够从生物本身抓住生物索引。我一直试图在生物类中使用一个set函数,设置生物的生物索引号以及设置所有节点'生物索引号作为相同的数字。
//Node class
class Node {
constructor(x, y, r, color, highlight, highlightColor) {
this.x = x;
this.y = y;
this.r = r || 20;
this.color = color || "#ff0";
this.highlight = highlight || false;
this.highlightColor = highlightColor || "#0000FF";
}
}
// Creature class
class Creature {
constructor(nodes, muscles, nodeColors) {
this.nodes = nodes;
this.muscles = muscles;
this.nodeColors = nodeColors || "#ff0";
Object.defineProperties(this, {
nodesArray: {
"get": (i) => this.nodes[i],
"set": nodes => {
this.nodes[i] = newNode;
}
},
musclesArray: {
"get": (i) => this.nodes[i],
"set": muscles => {
this.muscles[i] = newMuscle;
}
},
creatureNumber: {
"get": () => creatures.indexOf(this),
}
});
}
}
var nodes = [
new Node(100, 100),
new Node(200, 200)
];
var creatures = [
new Creature(nodes, muscles)
];

答案 0 :(得分:1)
我已经改变了你的课程(甚至不确定你的工作是否像你认为的那样),并增加了一个额外的课来管理这些生物。我认为您使用该阵列的设置不会起作用。
//Node class
class Node {
constructor(x, y, r, color, highlight, highlightColor) {
this.x = x;
this.y = y;
this.r = r || 20;
this.color = color || "#ff0";
this.highlight = highlight || false;
this.highlightColor = highlightColor || "#0000FF";
}
/* === PROPERTY: parentCreature === */
get parentCreature() {
return this._parentCreature;
}
set parentCreature(creature) {
this._parentCreature = creature;
}
/* === METHODS: removeFromCreature === */
removeFromCreature() {
this._parentCreature = null;
}
}
function setParentForNodes() {
this.nodesArray.forEach(node => {
node.parentCreature = this;
});
}
// Creature class
class Creature {
/* === CONSTRUCTOR === */
constructor(nodes, muscles, nodeColors) {
this.nodes = nodes;
this.muscles = muscles;
this.nodeColors = nodeColors || "#ff0";
setParentForNodes.call(this);
}
/* === PROPERTY: nodesArray === */
get nodesArray() {
return this.nodes;
}
set nodesArray(value) {
this.nodes = value;
setParentForNodes.call(this);
}
/* === PROPERTY: musclesArray === */
get musclesArray() {
return this.musclesArray;
}
set musclesArray(value) {
this.musclesArray = value;
}
/* === METHODS: removeNodes === */
removeNodes() {
this.nodes.forEach(node => {
node.parentCreature = null;
});
this.nodes = null;
}
}
class Creatures {
/* === CONSTRUCTOR === */
constructor(creaturesArray = []) {
this.creatures = new Map();
creaturesArray.forEach(creature => {
this.creatures.set(creature.id, creature.model);
});
}
/* === METHOD: addCreature === */
addCreature(id, model) {
if (this.creatures.has(id)) {
console.log('Creature ID already exists');
return;
}
this.creatures.set(id, model);
}
/* === METHOD: getCreatureById === */
getCreatureById(id) {
if (this.creatures.has(id)) {
return this.creatures.get(id);
}
return null;
}
}
// Create the nodes
var nodes = [
new Node(100, 100),
new Node(200, 200)
];
// Create the Goblin with the nodes.
var creatures = new Creatures([
{
id: 'goblin',
model: new Creature(nodes, 'muscles')
}
]);
// Create the dwarf, it has no nodes
creatures.addCreature('dwarf', new Creature([], 'muscles'));
const
// Get the parent creature for the first node.
parentCreatureForNode = nodes[0].parentCreature,
// Get the creature instance for the dwarf.
dwarf = creatures.getCreatureById('dwarf');
// Remove the nodes from the parent of the first node.
parentCreatureForNode.removeNodes();
// Assign the nodes to the dwarf.
dwarf.nodesArray = nodes;
// The goblin should report it has no nodes.
console.log(creatures.getCreatureById('goblin'));
// The dwarf should log it has 2 nodes.
console.log(creatures.getCreatureById('dwarf'));
// Make sure the node reports its parent is the dwarf.
console.log(nodes[0].parentCreature === dwarf);