3
I found this question which speaks almost of the same subject but her focus is on access to parent class methods. And my doubt is to know why the use of super()
before the declaration of variables in the child class, as in the example below:
class List{
constructor(){
this.data = [];
}
add(data){
this.data.push(data);
}
showdata(){
console.log(this.data);
}
}
class TodoList extends List{
constructor(){
//qual a finalidade deste super para a criação de variáveis na classe filha ?
super();
this.owner = "John";
}
showmeowner(){
console.log(this.owner);
}
}
const MyList = new TodoList();
If I withdraw the super()
of the class builder TodoList
the browser returns me the following error:
Uncaught Typeerror: Cannot set Property 'Owner' of Undefined at new Todolist (Bundle.js:54) at Bundle.js:68
Like property owner
may be like undefined
if she’s in the class builder TodoList
? Because the super()
is needed in this situation ?