记录对象显示所有属性,但是分别记录属性显示“未定义" [英] Logging objects shows all properties but logging the properties individually shows 'undefined'

查看:222
本文介绍了记录对象显示所有属性,但是分别记录属性显示“未定义"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我构建的PHP函数移植到Javascript中,并且发现了许多差异,这些差异导致大量的额外工作.我被困在这一点上,却找不到任何逻辑:
X:95.29
Y:27.39
testParse2.RXdec:0.1

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={};
 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?)

}

最让我感到困惑的是:

  1. curPos在循环甚至开始之前就获得了一个值.该值是 最终迭代后curPos应该具有的值.

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

在循环期间,curPos和curPos.X或.Y的控制台日志不 包含相同的值.

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

在循环过程中,尽管每次迭代都更改.X和.Y,但curPos的控制台日志始终是相同的

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

@str为控制台问题提供了正确的解释,但似乎此问题超出了控制台,并且实际上影响了对象值. 使用JSON.strigify之后,我可以看到这个(很好):

@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):

console.log(JSON.stringify(testParse2));
"Xdec":97.99
"Xdec":98.09
"Xdec":98.19

但是现在我尝试将数据传输到其最终数组,但是最终数组中填充了惰性"值:

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;

console.log(JSON.stringify(T));
"Xdec":98.19,"Ydec":27.39,"curX":323.19,"curY":177.39
"Xdec":98.19,"Ydec":27.39,"curX":323.19,"curY":177.39
"Xdec":98.19,"Ydec":27.39,"curX":323.19,"curY":177.39

如果我停止在循环中使用对象并切换到变量,则可以像这样构建最终数组:

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.

您可以通过使用

console.log(JSON.stringify(curPos));

代替

console.log(curPos);

这篇关于记录对象显示所有属性,但是分别记录属性显示“未定义"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆