0

I'm officially losing my mind. Am I missing something about javascript scope?

this.allUnits = {/*old data*/};

this.reconfigure = ( newData ) => {
    console.log( newData.allUnitsArray ); // correct

    this.allUnits = {};

    for( var i = 0; i < newData.allUnitsArray.length; ++i ) {
        var piece = newData.allUnitsArray[i];
        console.log( piece ); // correct

        this.allUnits[ piece.uid ] = piece;
        console.log( this.allUnits[piece.uid] ); // correct
    }

    console.log( this.allUnits ); // *SOME* ELEMENTS STILL REFERENCE OLD DATA!?!?
};

It isn't possible for any other code to be executing between the end of the for loop and the next console log, is it?

Many thanks in advance for your help. :)

Robert Lombardo
  • 152
  • 1
  • 9
  • 1
    No, no other code will execute in between, but pretty likely [some code will execute afterwards and change what you see in the console](https://stackoverflow.com/q/23392111/1048572) – Bergi Jul 06 '17 at 20:16
  • Please provide a [mcve] including example data and the unexpected outputs you are getting – Bergi Jul 06 '17 at 20:17
  • as @Bergi stated above, it's pretty hard to tell whats going on when we don't have the complete picture... at a guess this could be a closure issue https://developer.mozilla.org/en/docs/Web/JavaScript/Closures for reference – Matthew Brent Jul 06 '17 at 20:21
  • Lets make a wild guess and say possibly this is non-strict-mode code and `this.allUnits = {};` has no effect due to the property not being writable. The following code then overwrites some properties with the new values and the others remain the old. – ASDFGerte Jul 06 '17 at 20:34

0 Answers0