title: Javascript array filter: getting the context right
body: <p>While trying to write some awesome JS I ran into some strange JS-behaviour. I've created a piece of code to demonstrate my problem:</p>
  
  <p><a href="http://jsfiddle.net/CeyCy/" rel="nofollow">http://jsfiddle.net/CeyCy/</a></p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
      $(function() {
        var images = $('.images').children();
        var filtered = images.filter(function(i, image) {
          console.log(this);
        }, "abc");
      });
      &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;div class="options"&gt;
        &lt;div class="filters"&gt;
        &lt;/div&gt;
        &lt;ol class="pagination"&gt;
        &lt;/ol&gt;
      &lt;/div&gt;
      &lt;div class="results"&gt;
        &lt;ul class="images"&gt;
          &lt;li data-category="Zakelijk"&gt;Result 1&lt;/li&gt;
          &lt;li data-category="Speels"&gt;Result 2&lt;/li&gt;
          &lt;li data-category="Blogging"&gt;Result 3&lt;/li&gt;
          &lt;li data-category="Zakelijk"&gt;Result 4&lt;/li&gt;
          &lt;li data-category="Speels"&gt;Result 5&lt;/li&gt;
          &lt;li data-category="Blogging"&gt;Result 6&lt;/li&gt;
          &lt;li data-category="Zakelijk"&gt;Result 7&lt;/li&gt;
          &lt;li data-category="Speels"&gt;Result 8&lt;/li&gt;
          &lt;li data-category="Blogging"&gt;Result 9&lt;/li&gt;
          &lt;li data-category="Zakelijk"&gt;Result 10&lt;/li&gt;
          &lt;li data-category="Speels"&gt;Result 11&lt;/li&gt;
          &lt;li data-category="Blogging"&gt;Result 12&lt;/li&gt;
          &lt;li data-category="Zakelijk"&gt;Result 13&lt;/li&gt;
          &lt;li data-category="Speels"&gt;Result 14&lt;/li&gt;
          &lt;li data-category="Blogging"&gt;Result 15&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Ok, now my question. When using the Array.filter method you pass in a callback function as first parameter and an optional context as second parameter. As you can see, I'm passing "abc" as context (which is ridiculous, ofcourse). I would expect my console to throw a lot of "abc", but instead it prints the jQuery elements!</p>
  
  <p>Can someone shine some light on this please?</p>
  
  <p>Thanks,</p>
  
  <p>Martijn</p>