0
var Stars = new Array(1,2,3,4,5,6,7,8,9,10);
var StarsX = new Array(451,455,460,470,490,100,160,220,280,300);
var StarsY = new Array(451,455,460,470,490,480,390,330,200,120);
var Starsm = Stars
var StarsmX = StarsX
var StarsmY = StarsY

//just shows the output of the variables in a text field
function contpost(){Tfield.innerHTML=Stars + "<br/>" + StarsX + "<br/>" + StarsY + "<br/>" + Starsm + "<br/>" + StarsmX + "<br/>" + StarsmY;}

//cycles through the "for" loop and posts the variables
function newpost(){
for (var i=0;i<Stars.length;i++){
    StarsmX[i] = StarsX[i] + 10;
    StarsmY[i] = StarsY[i] + 10;
    }
contpost()
}


var Tfield= (This is the text field)
var canv_one= (This is a canvas in my document)
canv_one.addEventListener('click', newpost);

If I run this code (Firefox 9), the if/then loop adds 10 to both the "mX" variable and the "X" variable.

I now know approximately how it does it. It's simply because I set them equal early on. When I substitute this in the declarations, it works properly:

var Starsm = new Array()
var StarsmX = new Array()
var StarsmY = new Array()

What I don't understand is why javascript is doing this. It seems logical to me that if you set them equal once, during the global declaration, they will not re-equate later on (and do it backward, at that. I never set StarsX equal to StarsmX.)

I'm pretty sure this means I'm not understanding some kind of basic functionality of Javascript, and that worries me.

Can anybody help?

3
  • Just an aside: when creating an array the preferred syntax is [] rather than new Array() (shorter, easier to read, and avoids the problem where calling new Array(5) creates an array with five elements rather than a single element with value 5). So var Stars = [1,2,3,4,5,6,7,8,9,10]; and var Starsm = []; Commented Dec 29, 2011 at 3:31
  • OK. I'll go back and change that later. I'm basically learning Javascript from whatever I can google. ...Best practices aren't always clear. Commented Dec 29, 2011 at 3:32
  • Cool. You may like to look at MDN: developer.mozilla.org/en/JavaScript/Guide. Also, a Google tip: if you want to find out about some JavaScript keyword or function, add " mdn" or " mdc" to the end of your Google search terms, e.g., "array mdn" - this will tend to bring the MDN page about that keyword/function to the top of the search results - you generally don't even need the word "javascript". Commented Dec 29, 2011 at 3:42

1 Answer 1

4

It isn't adding anything to the variable. It's adding to the Array, and both variables reference the same Array object.

This is because in JavaScript you only get to hold a reference to an object, not the object itself. So the reference to the Array is the value being copied, not the Array.

If you wanted a shallow clone of the Array, use slice().

var Starsm = Stars.slice()
var StarsmX = StarsX.slice()
var StarsmY = StarsY.slice()

Or just do as you said, and create empty Arrays.

Sign up to request clarification or add additional context in comments.

3 Comments

So you're saying that basically, what I did is say that "starsm" is now a variable that contains the array "stars"? My intention was to create a new array with the name "Starsm" (etc) that cloned the original.
@user1120353: Yes. var Starsm = Stars copies the object reference held by Stars, so they both end up with a reference to the same Array object. To (shallow) clone the original Array object, you can use .slice().
Thanks. The idea that I could do that hadn't even occurred to me for some reason. I appreciate the prompt help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.