JavaScript数组范围问题 [英] Javascript Array Scope Issue
问题描述
解决 - 这个例子code的工作,所以我将它比作非工作我有发现不一致。
SOLVED - This example code worked, so I compared it to the non-working I had and found a discrepancy.
在我的code,一个声明的颜色数组我忘了前添加一个变种的时候,没有工作。
In my code, the one that did not work, when declaring the color array I forgot to add a "var" before it.
m_color = [];
m_color[0] = 255;
m_color[1] = 255;
m_color[2] = 255;
m_color[3] = 255;
而不是:
var m_color = [];
m_color[0] = 255;
m_color[1] = 255;
m_color[2] = 255;
m_color[3] = 255;
就是这样。未产生错误,我想这创造了它由所有粒子共享的全局数组。
That's it. No error was generated and I guess it created it as a global array shared by all the particles.
感谢您的答案大家。我会用它们来写出更好的JavaScript的code。
Thanks for the answers everyone. I will use them to write better javascript code.
老问题(它不再是相关的)是如下:
Old question (which is no longer relevant) is below:
我有一个JavaScript数组范围这个疯狂的问题。
I have this crazy issue with javascript arrays scope.
我有一个类粒子:
function ParticleClass()
{
var m_color = [];
m_color[0] = 255;
m_color[1] = 255;
m_color[2] = 255;
m_color[3] = 255;
var m_red = 255;
var m_green = 255;
var m_blue = 255;
this.SetRandomColorRGB = SetRandomColorRGB;
this.SetRandomColorArray = SetRandomColorArray;
this.DrawParticleRGB = DrawParticleRGB;
this.DrawParticleArray = DrawParticleArray;
function SetRandomColorRGB()
{
m_red = Math.floor( Math.random() * 255 );
m_green = Math.floor( Math.random() * 255 );
m_blue = Math.floor( Math.random() * 255 );
}
function SetRandomColorArray()
{
m_color[0] = Math.floor( Math.random() * 255 );
m_color[1] = Math.floor( Math.random() * 255 );
m_color[2] = Math.floor( Math.random() * 255 );
}
function DrawParticleRGB( ctx )
{
// I draw the particle using m_red, m_green and m_blue.
}
function DrawParticleArray( ctx )
{
// I draw the particle using m_color[0], m_color[1], m_color[2]
}
}
我然后创建ParticleClass粒子阵列,并吸引他们。
I then create an array of ParticleClass particles and draw them.
如果我创建了一堆颗粒,并尝试绘制在屏幕上,SetRandomColorRGB和DrawParticleRGB的伟大工程。每个粒子都有不同的颜色。
If I create a bunch of particles and try to draw them on the screen, SetRandomColorRGB and DrawParticleRGB works great. Each particle has a different color.
如果我用SetRandomColorArray和DrawParticleArray,所有的粒子具有相同的颜色。每一个新粒子产生的时候,所有的粒子变成最后一种颜色SetRandomColorArray回升。
If I use SetRandomColorArray and DrawParticleArray, all the particles have the same color. Each time a new particle is created, all the particles change to the last color SetRandomColorArray picked.
在我看来,数组共享内存,而其他变量则没有。这是真的?这是JavaScript的一个怪癖?别的东西怎么回事?
It looks to me that arrays share memory, whereas other variables do not. Is this true? Is this a quirk of Javascript? Is something else going on?
感谢。
推荐答案
在这里...这可能有助于解释为什么它正在发生:
here... this may help explain why it is happening :
var data = {}; // create a data object
data.string = "hey society"; // Add a string value property
data.num = 0; // Add a integer value property
data.arr = [0,1,2]; // Add an array property
data.date = new Date(); // Add an object property
// here is where the fun starts!
// Create a var for string property
var sString = data.string; // sets to "hey society", as expected.. cool
data.string = "changed" // change the value and the compare :
data.string == sString // returns false, the values are different
// same happens for a number.
// Now lets copy this array
var oArr = data.arr; // (seeing the comment pattern? XD)
data.arr .push(3); // and modify it.
data.arr == oArr // should be false? Nope. returns true.
// arrays are passed by reference.
var oDate = data.date // what about objects?
data.date .setHours(0); // modify the variable and
oDate.getTime() == data.date.getTime() // it returns true, too!
// so, how do we fix the array problem?
// right in front of yer nose
var oArrFix = data.arr.splice(0) // get a new array based on
data.arr .push(4) // an unmodified version of the original
data.arr == oArrFix // false.. huh..
// How do we use this as a fix
data.arr["changed"] == false;
oArrFix = ChangeOriginalArray( data.arr );
// When you are expecting an array..
function ChangeOriginalArray( arr ) // pass as a parameter
{
var aArr = arr.splice(0); // make a copy!
if (aArr["changed"] == false) // check has same value!
{
aArr["newKey"] = "newVal"; // add a value
arr["changed"] = true; // change original value
}
return aArr;
}
oArrFix["newKey"] == data.arr["newKey"] // false
oArrFix["changed"] == true // no, created
data.arr["changed"] == oArrFix["changed"] // nah, passed by value, not reference
这篇关于JavaScript数组范围问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!