JavaScript How do I make a link to a property if the property is yet to be declared? (In a Class)

So, the parent class Select declares this.elem as a DOM-element <select> and this.value, that links to a value of selected option

class Select  {

    constructor(classList, isTwoLevel, index){

        this.elem = document.createElement("select") 
        this.value = this.elem.children[this.elem.selectedIndex].value;// error here!   
    }
}

child class MySelect adds options, assigns values to them and appends them to this.elem.

class MySelect extends Select {

    constructor(){

        super();

        let opt1 = document.createElement("option");
        opt1.value = "foo";
        this.elem.appendChild(opt1);

        let opt2 = document.createElement("option");
        opt2.value = "bar";
        this.elem.appendChild(opt2);

    }
}

As expected, when creating a new exemplar of the MySelect class an error occurs:

let testSelect = new MySelect(); // Uncaught TypeError: Cannot read property 'value' of undefined

document.body.appendChild(testSelect.elem);

I don't want to move declaration of this.value to the child classes as it is supposed to be a universal properties for all the child classes, what should I do?

Answer:1

These aren't declarations, they're just assignments.

If you want value at the parent level but don't have a value to assign to it until the child class has done its initialization, you can:

  1. Assign it null at the parent level and then assign it another value later, or

  2. Pass the options for the select into the parent constructor, or

  3. Make value an accessor property (a getter) as CertainPerformance pointed out.

Answer:2

The whole idea is to take users input in a form and display their input in a JSON object. In state I have an array and inside it another array. My Form.js looks like this, state= { groups:[{ ...

The whole idea is to take users input in a form and display their input in a JSON object. In state I have an array and inside it another array. My Form.js looks like this, state= { groups:[{ ...

In componentDidMount I'm trying to get data called topic from /api/topics/${params.TopicId route then from response I want to send only topic.user_id to another route and get whole user as response. ...

In componentDidMount I'm trying to get data called topic from /api/topics/${params.TopicId route then from response I want to send only topic.user_id to another route and get whole user as response. ...

  1. getting data from api
  2. getting data from firestore
  3. getting data from twitter
  4. getting data from a dead phone
  5. getting data from old hard drive
  6. getting data from json
  7. getting data from firebase
  8. getting data from android to iphone
  9. getting data from another sheet in excel
  10. getting data from google analytics
  11. getting data from excel in python
  12. getting data from figure matlab
  13. getting data from instagram
  14. getting data from api python
  15. getting data from broken iphone
  16. getting data from json javascript
  17. getting data from a dead computer
  18. getting data from multiple tables sql
  19. getting data from snapchat
  20. getting data from api react

These are the geometries: http://p5js.org/examples/examples/3D_Geometries.php I've already tried the syntax for the 2d shapes but it doesn't appear to work. Is there a special syntax for the WEBGL ...

These are the geometries: http://p5js.org/examples/examples/3D_Geometries.php I've already tried the syntax for the 2d shapes but it doesn't appear to work. Is there a special syntax for the WEBGL ...

Today I wrote code for some programming contest. When I run it I was surprised because of error. Cannot read property 'forEach' of undefined in a place where looks like "error free space". sum = 0 ...

Today I wrote code for some programming contest. When I run it I was surprised because of error. Cannot read property 'forEach' of undefined in a place where looks like "error free space". sum = 0 ...