title: Javascript Ninja: Current element in array as function context
body: <p>I'm trying to wrap my head around some code in Javascript ninja on page 57. </p>
  
  <pre><code>function forEach(list, callback) {
      for (var n = 0; n &lt; list.length; n++) {
          callback.call(list[n], n); //makes the current element the function context of callback???
      }
  }
  
  var weapons = ['shuriken', 'katana', 'nunchucks'];
  
  forEach(weapons,
      function(index) {
          assert(this == weapons[index], "Got the expected value of " + weapons[index]);
      }
  );
  </code></pre>
  
  <p>I can pass the index of the array in the callback without using the call method and that makes  sense. What I don't understand is why would I want to use the current element as the function context. What is this trying to accomplish?</p>
  
  <p>I also am having a hard time understanding how this could be done. I'm assuming that the call method is looking at the list item as an object and not as the element in the array. But I'm not sure that's correct.</p>
  
  <p>My other thought was that by not assigning the function context to the current element the function context would be the <code>forEach</code> function. I'm not sure what trouble that would cause if any and maybe that had something to do with the reason for changing the function context. </p>