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?