Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>arguments</code> <a href="http://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/arguments" rel="noreferrer">is said to be</a> an Array-like object. As you already saw you may access its elements by index, but you don't have all the Array methods at your disposal. Other examples of Array-like objects are HTML collections returned by getElementsByTagName() or getElementsByClassName(). jQuery, if you've ever used it, is also an Array-like object. After querying some DOM objects, inspect the resulting jQuery object with Firebug in the DOM tab and you'll see what I mean.</p> <p>Here's my solution for the Meebo problem:</p> <pre><code>function conc(){ if (arguments.length === 0) return ""; else return Array.prototype.slice.call(arguments).join(" "); } alert(conc("a", "b", "c")); </code></pre> <p><code>Array.prototype.slice.call(arguments)</code> is a nice trick to transform our <code>arguments</code> into a veritable Array object. In Firefox <code>Array.slice.call(arguments)</code> would suffice, but it won't work in IE6 (at least), so the former version is what is usually used. Also, this trick doesn't work for collection returned by DOM API methods in IE6 (at least); it will throw an Error. By the way, instead of <code>call</code> one could use <code>apply</code>.</p> <p>A little explanation about Array-like objects. In JavaScript you may use pretty much anything to name the members of an object, and numbers are not an exception. So you may construct an object that looks like this, which is perfectly valid JavaScript:</p> <pre><code>var Foo = { bar : function() { alert('I am bar'); }, 0 : function() { alert('I am 1'); }, length : 1 } </code></pre> <p>The above object is an Array-like object for two reasons:</p> <ol> <li>It has members which names are numbers, so they're like Array indexes</li> <li>It has a <code>length</code> property, without which you cannot transform the object into a veritable Array with the construct: <code>Array.prototype.slice.call(Foo);</code></li> </ol> <p>The arguments object of a Function object is pretty much like the Foo object, only that it has its special purpose.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload