I'm feeling like I'm going crazy and could really use some assistance. My predicament involves a function that looks like this:
private generateTimeObject(firstObject: someInterface, secondObject?: someInterface) {
let firstTime;
let secondTime;
if (condition) {
firstTime = firstObject.time;
secondTime = secondObject ? secondObject.time : null;
} else {
firstTime = firstObject.someOtherTimeValue;
secondTime = secondObject ? secondObject.someOtherTimeValue : null;
}
firstObject.customValues = {
firstTimeValue: firstTime,
secondTimeValue: secondTime
};
}
So here's the scenario:
The function is called like this: generateTimeObject(firstObject). This means, the function creates a subObject customValues like this:
firstObject.customValues = {firstTimeValue, null}
Then, the function is called again - but this time like this: generateTimeObject(firstObject, secondObject).
This time, the expectation is for the firstObject to be written as follows:
firstObject.customValues = {firstTimeValue, secondTimeValue}
However, I am struggling to make this work. The secondTimeValue remains null, and despite trying Object.assign(), it still doesn't seem to fix the issue.
Any help in solving this problem would be greatly appreciated.
Here's a jsFiddle link. Unfortunately, everything seems to be functioning correctly there, so the problem must be originating from something else. I suspect it may have to do with reference or scope, but I just can't seem to pinpoint the exact cause. https://jsfiddle.net/pna75vrd/6/