Follow

@noracodes i’m wondering if this is broadly the pattern you’re looking for or if there’s more here that i am missing?

class Pokémon {
#⁠type

constructor(data) {
Object.assign(this, data)
}

get type() { return this.#⁠type }
set type(x) {
if (!["Normal", "Fighting", "Flying", "Poison", "Ground", "Rock", "Bug", "Ghost", "Steel", "Fire", "Grass", "Water", "Electric", "Psychic", "Ice", "Dragon", "Dark", "Fairy"].includes(x)) throw new RangeError("Invalid type");
else this.#⁠type = x;
}
}

new Pokémon({type: "Fire"});
new Pokémon(JSON.parse("{\"type\": \"Fire\"}"));
new Pokémon({type: "Bird"}); // error

· · Web · 1 · 0 · 3

@noracodes (if you copy and paste this code there is an invisible character after the hashes to prevent them from becoming hashtags)

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.