TIL You can dynamically name objects
POSTED ON:
TAGS: javascript objects
You can dynamically name object keys!
Wizards use mana, while barbarians use rage.
You can do something like this!
let magicPoints;
specialPoints = 'mana';
const wizard = {
health: 20
spells: 'fireball',
[specialPoints] : 100
}
specialPoints = 'rage';
const barbarian = {
health: 100
skills: 'cleave',
[specialPoints] : 40
}
This is helpful if you were making a 'hero class factory'.
Something like this:
function createHero(class, magicPointName) {...}
Related TILs
Tagged: javascript