0

I come from a Java background and want to refer to things in a similar way. I’m not sure where to look. pseudo code:

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

Is it possible to do this kind of thing in vanilla JavaScript? I would also like to refer to all the objects horizontally in elements. The Child objects should be able to refer up to parent when needed.

5
  • 1
    Check out typescript:typescriptlang.org Commented Jul 4, 2018 at 12:04
  • Drop the type annotations and you mostly got valid JS. Commented Jul 4, 2018 at 12:15
  • Your hierarchy is really problematic. A child cannot be a parent itself, so this is inappropriate for a tree. Commented Jul 4, 2018 at 12:15
  • @Bergi Do you suggest that I wrap them together into the Elements class and do away with Parent and Child? Commented Jul 5, 2018 at 10:20
  • @jackoverflow Probably, although I have no idea what you want to do. Commented Jul 5, 2018 at 13:06

1 Answer 1

1

Yes, it is possible to do things somehow similar in JS since ES2015 (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes). In case the app should run in different browsers and versions I would recommend you to use a compiler such Babel (https://babeljs.io/) bacause last ES features are not compatible with older browsers. Relations between child and parent classes could be found in mdn reference (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super). Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.