javascript中的对象识别

时间:2017-02-22 05:13:52

标签: javascript arrays collision-detection tree-structure

我正在使用JavaScript制作碰撞检测器。我想制作一个树形结构,这样我就可以创建一个复杂的对象。

有一个大数组,包括子元素在内的所有对象都是数组的一部分。 他们中的一些人是孩子,其他人是父母。每个项目都有childrenparentroot属性,如果发生碰撞,它会冒泡到父级。

我想出了两种识别物体的方法。

  1. 提供对象ID

  2. 直接引用对象,如"node1.root = someOtherObject"

  3. 我也想知道哪个更快

    //1
    if(object1.root === root.id){/*code here*/}//id based identification (literally)
            //19253 === 19253
    
    //or 2
    if(object1.root === root){/*code here*/}//object based identification
         //[Object] === [Object]
    

1 个答案:

答案 0 :(得分:1)

两者在性能方面都是一样的。请查看以下链接。

http://jsben.ch/#/Y9jDP