Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中引用对象的层次结构_Javascript_Object_Hierarchy - Fatal编程技术网

在Javascript中引用对象的层次结构

在Javascript中引用对象的层次结构,javascript,object,hierarchy,Javascript,Object,Hierarchy,我来自Java背景,希望以类似的方式引用事物。 我不知道该去哪里找。 伪代码: var elements = []; class Element { constructor() { this.isClicked; } mouseOver() { ///returns true if mouse is over }

我来自Java背景,希望以类似的方式引用事物。 我不知道该去哪里找。 伪代码:

var elements = [];

class Element
{
   constructor()
              {
              this.isClicked;
              }
   mouseOver()
              {
              ///returns true if mouse is over
              }
   click()
              {
              this.isClicked = true;
              }
   unclick()
              {
              this.isClicked = false;
              }

}

class Parent extends Element 
{

   constructor()
              {
              this.children = [];
              this.x = 0;
              this.y = 0;
              elements.push(this)
              }
   addChild()
              {
              children.push(new Child(this));
              }
}

class Child extends Element
{
   constructor(Parent p)
              {
              this.parent = p;
              this.x = 0;
              this.y = 0;
              elements.push(this)
              }

   move()
              {
              this.x = parent.x;
              this.y = parent.y;
              }
}

var liveElement;

function mousePressed()
{
    for(Element e : elements)
              {
              if(e.mouseOver)
                        {
                        liveElement = e;
                        liveElement.click();
                        }
              }
}

function mouseReleased()
{
    liveElement.unclick();
    liveElement = null;
}

有可能在香草JavaScript中实现这种功能吗?我还想水平引用
元素中的所有对象。子对象应该能够在需要时引用到父对象。

是的,自ES2015()以来,可以在JS中执行类似的操作。如果应用程序应该在不同的浏览器和版本中运行,我建议您使用Babel()之类的编译器,因为last ES功能与旧浏览器不兼容。可以在mdn引用()中找到子类和父类之间的关系。希望能有所帮助。

查看typescript:删除类型注释,您大部分都会得到有效的JS。您的层次结构确实存在问题。子级本身不能是父级,因此这不适合于树。@Bergi您是否建议我将它们打包到
元素
类中,并删除
父级
子级
?@jackoverflow,尽管我不知道您想做什么。