Logging objects shows all properties but logging the properties individually shows 'undefined'

I'm trying to port a PHP function I built to Javascript and have been finding many differences that cause a lot of extra work. I am stuck on this one and cannot find any logic to it:
X: 95.29
Y: 27.39
testParse2.RXdec : 0.1

 var curPos={};
 console.log(curPos);       //X:97.19 Y:27.39 (I expect an empty object)
 console.log(curPos['X']);  //undefined (seems ok but makes no sense with above)
 console.log(curPos['Y']);  //undefined (seems ok but makes no sense with above)

   for(var Ri=0; Ri < 20; Ri++){    

     curPos['X'] = "";
     curPos['Y'] = "";
 console.log(curPos['X']);  // "" (seems ok)
 console.log(curPos['Y']);  // "" (seems ok)
 console.log(curPos);       //X:97.19 Y:27.39
     curPos.X = (((XY(lastPos[AV['A']], 'X')*1)+(testParse2.RXdec*1*Ri)).toFixed(10)*1);
     curPos.Y = (((XY(lastPos[AV['B']], 'Y')*1)+(testParse2.RYdec*1*Ri)).toFixed(10)*1);
 console.log(curPos);   // X:97.19 Y:27.39 (I expect X:95.29 + 0.1 each loop Y:27.39)
 console.log(curPos.X); // 95.29 (correct by why is above different?)
 console.log(curPos.Y); // 27.39 (correct by why is above different?)


The things that confuse me the most are:

  1. curPos gets a value before the loop even starts. The value is the value that curPos should have after the final iteration.

  2. during the loop the console log for curPos and curPos.X or .Y do not contain the same values.

  3. during the loop the console log for curPos is always the same despite changing .X and .Y each iteration

Edit: @str gave the correct explanation for the console trouble but it seems that this problem is beyond the console and actually effects the object values. after using JSON.strigify I can see this (which is good):


but now I try to transfer the data to its final array but that final array is filled with 'lazy' values:

T['tool'][T['curTool']]['points'][lineID] = testParse2;


If I stop using objects in the loop and switch to variables then build my final array like this it works:

 T['tool'][T['curTool']]['points'][lineID] = {'curX' : curX,
                                              'curY' : curY,
                                              'TYP'  : 'DR',
                                              'lineID'   : lineID,
                                              'lineName' : lineName,};

How do you send the actual object values at a particular iteration of a loop to a different array?



Browsers evaluate objects lazily in when logging. So when you expand them after the loop, they will show the properties they have at the moment of expanding and not the ones they had when the object was logged.

You can verify that by using


instead of



Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.