r/learnjavascript • u/kumikoneko • Jul 26 '26
Noob question about generating property.object addresses
Hi everybody,
I'm trying to write up a personal project automating some ttrpg mechanics, where the "attack" function will take the target's name as an argument and feed it into the "defence" action that uses the target's Dex and one of their skills (i.e. "Void).
Referring to the target's Dex seems to work fine from inside the "defence" action, but when I try to implement the Character.Skill referent through variables I'm met with "Uncaught ReferenceError: Void is not defined."
//very basic and temporary exalt template
class Exalt {
constructor() {
this.Essence = 1;
this.Dex = 1;
this.Defence = 1;
}
}
const Antigone = new Exalt()
Antigone.Dex = 4
Antigone.Void = 5
var front = "Antigone"
var back = "Void"
let a = JSON.parse(JSON.stringify(front + "." + back))
console.log(a) // "Antigone.Void" for some reason
console.log(JSON.parse(Antigone.Void)) //5, the expected value
function Defence(Character, Skill) {
return Math.ceil((Character.Dex + JSON.parse(Character.Skill))/2)+1
} // this runs into an error, I think
console.log(Defence(Antigone, Void))
console.log(Antigone) includes "Void" as one of the stats, so I don't even know.
I've seen a suggestion to JSON.parse the string twice, but that results in "unexpected character."
Now, my JS experience is only a few days, so I'm guessing there is a better way of achieving what I'm trying to do, so I'll be grateful whether you suggest a different approach or explain why my code doesn't do what I think it should.
Thanks!
Edit: I just realized I'm completely overthinking and should probably make defence a function within the Exalt class, so it can be referenced the same way I'm referencing Dex.
View all comments
Show parent comment
1
u/kumikoneko Jul 26 '26
Thanks a lot!
It works now. I've tried the "Character[Skill]" notation before, but it didn't work because I didn't have the "", and I'm not sure how long it would've taken me to figure it out.
I'll also add the check, thanks for the tip.