So I have created an object using two different ways.
First way:
const sprite = () => {
let obj = {
hp: 100,
damage: () => {
obj.hp -= 25;
},
heal: () => {
obj.hp += 25;
}
}
return obj;
}
let sprite1 = sprite();
Second way:
const sprite = () => {
let obj = {};
obj.hp = 100;
obj.damage = () => {
obj.hp -= 25;
}
obj.heal = () => {
obj.hp += 25;
}
return obj;
}
let sprite1 = sprite();
I tested both of these syntax and they both work, but I see only the second way being used more often.
So my question is:
Is it ok to use the syntax in the first way? Is there a reason why the first way syntax is not widely used? Is it bad practice, if so Why?
I am just a bit new to OOP in JS and looking for some clarification.
Thanks!