title: Every element in an array being incremented simultaneously when only one should be
body: <p>I am using the following code to increment the elements in a 2d array surrounding a given element.</p>
  
  <pre><code> EmptyCell = {number: 0}; //This has several parts in the actual code.
   list = new Array();
  
  function init(w,h){
      for (var x = 0; x &lt;= w; x++){
          list[x] = new Array();
          for (var y = 0 ; y &lt;= h; y++){
              list[x][y] = EmptyCell;
          }
      }
  }
  
  function map(func,x,y){
      var xoff = [1,1,1,0,0,-1,-1,-1];
      var yoff = [1,0,-1,1,-1,1,0,-1];
      for (var atIndex = 0; atIndex &lt; 8; atIndex++){
          func(x+xoff[atIndex],y+yoff[atIndex]);
      }
  }
  </code></pre>
  
  <p>And then I run it like this:</p>
  
  <pre><code>init(10,10);
  
  map(function(x,y){
      if (list[x] != null &amp;&amp; list[x][y] != null){
          list[x][y].number++;
      }
  },0,0);
  </code></pre>
  
  <p>Each time <code>list[x][y].number++</code> is run, every element in the entire array is incremented.
  Could somebody explain why this is happening?</p>