首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >数组的嵌套类,希望获取外部数组的索引。

数组的嵌套类,希望获取外部数组的索引。
EN

Stack Overflow用户
提问于 2017-09-01 07:59:01
回答 1查看 79关注 0票数 0

我有两个类,生物类将节点数组作为属性。但这些生物也将被保存在一个阵列中。我希望能够从任何节点获取生物索引。目前,我所能做的就是能够从生物本身抓取生物索引。我一直在研究如何在生物类中使用set函数,来设置生物的生物索引号,以及将所有节点的生物索引数设置为相同的数目。

代码语言:javascript
运行
复制
//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)
];

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-01 08:30:18

我已经稍微改变了你的类(甚至不确定你的类是否像你想的那样工作),并增加了一个额外的类来管理这些生物。我不认为你和数组的设置会起作用。

代码语言:javascript
运行
复制
//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);

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45995669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档