I'm extremely confused as to what is going on in my javascript code. I am trying to write a function that will create an array of random points. However, when I log the array inside the loop (whose function is to add points to the array), and then log it again outside of the loop, I get two different arrays printed.
我对javascript代码中发生的事情感到非常困惑。我正在尝试编写一个函数来创建一个随机点数组。但是,当我在循环中记录数组(其功能是向数组添加点),然后在循环外再次记录它时,我会打印两个不同的数组。
The first, as predicted as the list of random X,Y points, but the second log only contains the last inputed X,Y point.
第一个,如预测的随机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
}
}
See Photos of Console Console Array 1 Console Array 2
请参阅控制台控制台阵列1控制台阵列2的照片
edit:
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;
}
looks pretty messy, but essentially I wanted a different range of values to randomly choose from for(dx,dy) based on an initial random number.
看起来很乱,但基本上我想要一个不同的值范围,可以根据初始随机数随机选择(dx,dy)。
2 个解决方案
#1
0
The pickClose
function always returns the passed element. Object in javascript are passed by reference, so any changes you do to the object later will also apply to all other references to the object you have stored.
pickClose函数始终返回传递的元素。 javascript中的对象通过引用传递,因此您稍后对该对象所做的任何更改也将应用于对您存储的对象的所有其他引用。
To clarify:
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!
So if you want to return a new Point
inside your function (and not change the passed one), you have to create a new object that you alter and return:
因此,如果要在函数内部返回一个新Point(而不是更改传递的Point),则必须创建一个可以更改并返回的新对象:
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);
#2
0
Your pickClose function takes lastPt as a reference, so you are modifying the Point that already is in the array and adding it again.
pickClose函数将lastPt作为引用,因此您要修改已在数组中的Point并再次添加它。
try changing your line 103 to:
尝试将您的第103行更改为:
var newPt = pickClose(new Point(lastPt.X, lastPt.Y));
#1
0
The pickClose
function always returns the passed element. Object in javascript are passed by reference, so any changes you do to the object later will also apply to all other references to the object you have stored.
pickClose函数始终返回传递的元素。 javascript中的对象通过引用传递,因此您稍后对该对象所做的任何更改也将应用于对您存储的对象的所有其他引用。
To clarify:
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!
So if you want to return a new Point
inside your function (and not change the passed one), you have to create a new object that you alter and return:
因此,如果要在函数内部返回一个新Point(而不是更改传递的Point),则必须创建一个可以更改并返回的新对象:
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);
#2
0
Your pickClose function takes lastPt as a reference, so you are modifying the Point that already is in the array and adding it again.
pickClose函数将lastPt作为引用,因此您要修改已在数组中的Point并再次添加它。
try changing your line 103 to:
尝试将您的第103行更改为:
var newPt = pickClose(new Point(lastPt.X, lastPt.Y));