I’m trying to code a simple text RPG using JS and I wanted to perform this:
I have two objects: Player and Item. “Potion” is one instance of Item, and it increases Player hp by 30 when used.
I’m using the following code and it works fine, but I’d like to know if there is a “better” way to perform this without using “eval”. Note that the “ef” key in the instance of Item must specify which keys of Player it affects and what it must do.
class Player {
constructor(a={}) {
this.na = a.na
this.st = {hp: 100, atk: 10, def: 10, en: 50}
}
}
class Item {
static potion = new this({
na: "Potion",
ef: ['st.hp', '+= 30']
})
constructor(a={}) {
this.na = a.na
this.ef = a.ef ?? []
}
use(target) {
eval(`target.${this.ef[0]} ${this.ef[1]}`)
}
}
const me = new Player({na: "Azmuth"})
Item.potion.use(me)
console.log(me)
Sorry for bad english/explanation and thank you in advance.