title: Call apply method with arguments in JavaScript
body: <p>I wanted to invoke a function using the javascript apply() method. This works fine if the function has no arguments. i.e.</p>
  
  <pre><code>function test()
  {
    console.log(this);
  }
  
  body = document.getElementsByTagName("body")[0]; // shortcut to body element
  
  test.apply(body); // returns [object HTMLBodyElement]
  </code></pre>
  
  <p>But I can't seem to do the same thing to invoke a function that has arguments:</p>
  
  <pre><code>function test(msg)
  {
    console.log(msg);
  }
  
  body = document.getElementsByTagName("body")[0]; // shortcut to body element
  
  test(this).apply(body); // mysteriously returns the correct result, then
  // typeError: 'undefined' is not an object (evaluating "test(this).apply".
  </code></pre>
  
  <p>The examples above are completely trivial but the grain of my question is: How do I use the apply() method to invoke a function with arguments.</p>