javascript - 在日志记录中显示两个不同数组的数组
javascript - array displaying two different arrays in logging
我对我的 javascript 代码中发生的事情感到非常困惑。我正在尝试编写一个函数来创建一个随机点数组。但是,当我在循环内记录数组(其功能是向数组添加点),然后在循环外再次记录它时,我打印了两个不同的数组。
第一个,如预测的随机 X,Y 点列表,但第二个日志仅包含最后输入的 X,Y 点。
function Pick()
{
var numPoints = 4 * Math.floor(4+Math.random()*5);
var chosen_pts = [];
var lastPt;
for(var i = 0;i<numPoints;i++)
{
if(chosen_pts.length==0)
{
var temp = new Point(Math.floor(Math.random()*250),Math.floor(Math.random()*250));
var newPt = pickClose(temp);
chosen_pts.push(newPt);
lastPt = newPt;
}
else{
var newPt = pickClose(lastPt);
chosen_pts.push(newPt);
}
console.log(chosen_pts[i]); //LINE 106
}
console.log("\noutside of the loop:")
for(var i = 0;i<numPoints;i++)
{
console.log(chosen_pts[i]); //LINE 111
}
}
查看控制台照片
Console Array 1
Console Array 2
编辑:
function pickClose(lastPt)
{
var x = lastPt["X"];
var y = lastPt["Y"];
var dx = 0;
var dy = 0;
var rand = Math.floor(1+Math.random()*100);
if(rand<50){
dx = 1+Math.floor(Math.random()*10);
dy = 1+Math.floor(Math.random()*10);
if( (dx+dy)%3==0 ){
dx*=-1;
}
}
else if(rand<80)
{
dx = 1+Math.floor(Math.random()*25);
dy = 1+Math.floor(Math.random()*25);
if( (dx+dy)%3==0 ){
dy*=-1;
}
}
else{
dx = 1+Math.floor(Math.random()*60);
dy = 1+Math.floor(Math.random()*60);
if( (dx+dy)%4==0 ){
dx*=-1;
dy*=-1;
}
}
if( (x+dx) < 500&& (x+dx) >=0 )
lastPt["X"]+=dx;
else
lastPt["X"]-=dx;
if( (y+dy) < 500&& (y+dy) >=0 )
lastPt["Y"]+=dy;
else
lastPt["Y"]-=dy;
return lastPt;
}
看起来很乱,但本质上我想要根据初始随机数从 for(dx,dy) 中随机选择不同范围的值。
pickClose
函数总是 return 传递的元素。 javascript 中的对象通过引用 传递 ,因此您稍后对该对象所做的任何更改也将应用于对您存储的对象的所有其他引用。
澄清一下:
var point1 = new Point(1, 2);
var point2 = pickClose(point1);
// inside pickClose, parameter lastPt = point1:
lastPt["X"] += dx; // <- this also alters point1!
lastPt["Y"] += dy; // <- this also alters point1!
所以如果你想 return 一个 new Point
在你的函数中(而不是改变传递的那个),你必须创建一个新的您更改的对象和 return:
var newX = x, newY = y;
// instead of:
lastPt["X"]+=dx;
// do:
newX += dx;
// then, at the bottom, instead of
return lastPt;
// create a new instance
return new Point(newX, newY);
您的 pickClose 函数将 lastPt 作为参考,因此您正在修改数组中已有的 Point 并再次添加它。
尝试将第 103 行更改为:
var newPt = pickClose(new Point(lastPt.X, lastPt.Y));
我对我的 javascript 代码中发生的事情感到非常困惑。我正在尝试编写一个函数来创建一个随机点数组。但是,当我在循环内记录数组(其功能是向数组添加点),然后在循环外再次记录它时,我打印了两个不同的数组。
第一个,如预测的随机 X,Y 点列表,但第二个日志仅包含最后输入的 X,Y 点。
function Pick()
{
var numPoints = 4 * Math.floor(4+Math.random()*5);
var chosen_pts = [];
var lastPt;
for(var i = 0;i<numPoints;i++)
{
if(chosen_pts.length==0)
{
var temp = new Point(Math.floor(Math.random()*250),Math.floor(Math.random()*250));
var newPt = pickClose(temp);
chosen_pts.push(newPt);
lastPt = newPt;
}
else{
var newPt = pickClose(lastPt);
chosen_pts.push(newPt);
}
console.log(chosen_pts[i]); //LINE 106
}
console.log("\noutside of the loop:")
for(var i = 0;i<numPoints;i++)
{
console.log(chosen_pts[i]); //LINE 111
}
}
查看控制台照片 Console Array 1 Console Array 2
编辑:
function pickClose(lastPt)
{
var x = lastPt["X"];
var y = lastPt["Y"];
var dx = 0;
var dy = 0;
var rand = Math.floor(1+Math.random()*100);
if(rand<50){
dx = 1+Math.floor(Math.random()*10);
dy = 1+Math.floor(Math.random()*10);
if( (dx+dy)%3==0 ){
dx*=-1;
}
}
else if(rand<80)
{
dx = 1+Math.floor(Math.random()*25);
dy = 1+Math.floor(Math.random()*25);
if( (dx+dy)%3==0 ){
dy*=-1;
}
}
else{
dx = 1+Math.floor(Math.random()*60);
dy = 1+Math.floor(Math.random()*60);
if( (dx+dy)%4==0 ){
dx*=-1;
dy*=-1;
}
}
if( (x+dx) < 500&& (x+dx) >=0 )
lastPt["X"]+=dx;
else
lastPt["X"]-=dx;
if( (y+dy) < 500&& (y+dy) >=0 )
lastPt["Y"]+=dy;
else
lastPt["Y"]-=dy;
return lastPt;
}
看起来很乱,但本质上我想要根据初始随机数从 for(dx,dy) 中随机选择不同范围的值。
pickClose
函数总是 return 传递的元素。 javascript 中的对象通过引用 传递 ,因此您稍后对该对象所做的任何更改也将应用于对您存储的对象的所有其他引用。
澄清一下:
var point1 = new Point(1, 2);
var point2 = pickClose(point1);
// inside pickClose, parameter lastPt = point1:
lastPt["X"] += dx; // <- this also alters point1!
lastPt["Y"] += dy; // <- this also alters point1!
所以如果你想 return 一个 new Point
在你的函数中(而不是改变传递的那个),你必须创建一个新的您更改的对象和 return:
var newX = x, newY = y;
// instead of:
lastPt["X"]+=dx;
// do:
newX += dx;
// then, at the bottom, instead of
return lastPt;
// create a new instance
return new Point(newX, newY);
您的 pickClose 函数将 lastPt 作为参考,因此您正在修改数组中已有的 Point 并再次添加它。
尝试将第 103 行更改为:
var newPt = pickClose(new Point(lastPt.X, lastPt.Y));