I had made an abstract class:
class Model {
attributes = [];
constructor(data) {
this._importAttributes(data);
}
_importAttributes(data) {
this.attributes.forEach((attr) => {
this[key] = data[attr];
});
}
}
and then extended from that abstract class:
import Model from './model';
class Promotion extends Model {
attributes = ['id', 'shop_name', 'title', 'description', 'end_time'];
// No need to state this constructor, just want to state out the problem clearly
constructor(data) {
super(data); // <-- Problem occured in here,
// this.attributes is empty array inside parent constructor
}
}
so that I could use the class like this way:
let promotion = new Promotion({'id': 1, 'shop_name': 'Sample'....});
------ WHAT I WOULD LIKE TO ACHEIVE ------
I would like to use the _importAttributes()
function inside constructor()
of all extends child class. Simply just state the attributes
of the child class and start to use easily.
------ PROBLEM ENCOUNTERED ------
When I call constructor()
in the Promotion
class,
it cannot get attributes
of the Promotion
class.
Appreciate for any kindly help.