I have seen in youtube videos about js that the speaker was using:
const hero = {
health:10
}
Object.defineProperty(hero, 'status', {
get(){
if(this.health > 50){
return 'fit like a champ';
}else {
return 'badly hurt';
}
}
});
console.log(hero.status)
to assign new keys to objects based on condition but we can also use it in this way:
if(hero.health>50){
hero.status = 'good'
}else {
hero.status='bad'
}
console.log(hero.status)
I want to know which is the better approach and the best practice. Thanks