I’m rewriting some standalone functions that create and adjust the properties of several objects to now be properties of those objects themselves – mostly as an experiment as it’s not something I’ve really tried before.
One of these functions operates over the properties of the object with a for...in loop. How is this going to interact with the functions, including itself, that are now properties of the object?
I’m guessing it will try to perform the code in the loop on the functions as well, which is not written to handle functions. If that’s the case, what’s best way (if there is a “best way” of what I’m doing) of avoiding that problem?
I’m thinking that perhaps another object within the object to store the data generated at runtime would be reasonable – then I could just perform the for…in on that.
Thanks in advance.
The key section of code, simplified:
// An object to hold separate Inventory Adjustments by date
const inventoryAdjustments = {
adjustCorrespondingCodes: (adjustment) => {
// Other function code
},
save: (user) => {
try {
for (const inventoryAdjustment in this) { // The for...in loop
this[inventoryAdjustment].save();
}
return true;
} catch (e) {
// Error handling
return false;
}
}
};