2017-09-01 38 views
0

我有2個類,生物類將節點數組作爲屬性。但是這些生物也將被列入陣列。我希望能夠從任何節點獲取生物索引。目前我所能做的就是能夠從生物本身獲取生物索引。我一直在嘗試着使用生物類中的集合函數來設置生物的生物索引號,並將所有節點的生物索引號設置爲相同的數字。數組的嵌套類,想抓取外層數組的索引

//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

我讀過你的問題了幾次,我不知道你想TP達到什麼目的。你可以添加一個小例子數組,並基於這個數組解釋你需要得到什麼? – Thijs

+0

爲了解決您的問題並且同時獲得更好的性能,也許您應該考慮將ID明確存儲在Creature對象中(因爲「array.indexOf」效率不高,O(N))。 – Joel

+0

@Thijs數組在片段中。基本上我會有這兩個數組。每個將擴展他們各自的類。每個生物都會擁有一組節點。我想要做的就是隻能取得其中一個節點,並能夠抓住其他生物陣列中生物的索引。希望這是有道理的。 – Chenny

回答

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

+0

非常感謝您的幫助,我想我會稍後使用一些此代碼。但仔細看,我仍然認爲我會遇到同樣的問題,如果我用這個。因爲我試圖基本上將2個節點連接在一起,但是一旦它們連接在一起,它們將在同一個生物ID下。所以我認爲每個節點都必須能夠擁有他們所屬的生物的屬性。所以當我打電話時,我可以刪除1個生物,並將該生物的所有節點和肌肉加入其他生物。 – Chenny

+0

我已經改變了答案......我認爲它現在做你想做的。 – Thijs

+0

工作得很好,我其實只需要使用setParentForNodes函數,但我仍然會採取一些其他建議來清理我的代碼,非常感謝! – Chenny