Let's say this is my factory function to generate an object.
const generateObjectWithId = (id,value) => (
{id,value}
);
I have declared an Id before I generated the object and then I incremented it using ++
let currentId = 0;
const object = generateObjectWithId(currentId++,"sample");
Result:
console.log(currentId) => 1
console.log(object) => {id:0,value:"sample"}
currentId
got incremented to 1 but the object
I received from the factory function is with id 0.
I've fixed it with incrementing the currentId before passing in the parameter and worked right now.
My fix:
let currentId = 0;
currentId++; //or currentId = currentId + 1;
const object = generateObjectWithId(currentId,"sample");
But I need someone's help to explain me why incrementing inside the parameter won't work.
Thanks a lot.