I would like to make Bob a NPC with all NPC class parameters, and all Civil class parameters, but I don't know how to declare it so both Classes read the params associative array to define Bob properties.
My code:
class Civil {
constructor(params) {
params["gold"] ? this.gold = params["gold"] : this.gold = 0;
}
}
class NPC extends Civil {
constructor(params) {
super();
params["name"] ? this.name = params["name"] : this.name = "No name defined";
params["race"] ? this.race = params["race"] : this.race = "No race defined";
}
}
var bob = new NPC({
"name": "Bob",
"race": "Human",
"gold": 10
});
console.log(bob.name);
//returns that "params" is undefined in Civil class
If I don't use params in the Civil class and simply set this.gold to an int, everything works, and Bob.gold returns the int, Bob.name returns "Bob", and Bob.race returns "Human".
It's one of the first time I experiment with classes (to make a small game), and I don't fully understand how class inheritance works.