<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog - ShiftEleven &#187; arguments</title>
	<atom:link href="http://shifteleven.com/articles/tag/arguments/feed" rel="self" type="application/rss+xml" />
	<link>http://shifteleven.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sat, 19 Sep 2009 11:19:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0-alpha</generator>
		<item>
		<title>Array-like Objects in JavaScript</title>
		<link>http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript</link>
		<comments>http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript#comments</comments>
		<pubDate>Fri, 29 Jun 2007 01:43:38 +0000</pubDate>
		<dc:creator>K. Adam Christensen</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[arguments]]></category>
		<category><![CDATA[array]]></category>

		<guid isPermaLink="false">http://dev.fecalrod.com/?p=32</guid>
		<description><![CDATA[It make look like an Array, hell, it even has a length attribute; however, it may not actually be an Array.  JavaScript is sometimes a quirky language to say the least and the notion of what is an Array is no exception.
So what are these array-like objects that I speak of?  Well there [...]]]></description>
			<content:encoded><![CDATA[<p>It make look like an Array, hell, it even has a length attribute; however, it may not actually be an Array.  JavaScript is sometimes a quirky language to say the least and the notion of what is an Array is no exception.</p>
<p>So what are these array-like objects that I speak of?  Well there are a few of them, one of which is <code>arguments</code>.  <code>arguments</code> is a special variable that is available inside the body of every function.  It is in fact, the list of arguments that were passed in.</p>
<p><span id="more-32"></span></p>
<h3><code>arguments</code></h3>
<p>If you inspect the <code>arguments</code> variable in a tool such as <a href="http://www.getfirebug.com,">Firebug</a> you will notice that it prints out like an array.  It has elements in sequential order and it even has a <code>length</code> property.</p>
<pre class="ruby" title="code">var testFunction = function() {
  console.log(arguments);
  console.log(arguments.length);
};</pre>
<p>So what am I griping about?  Try doing <code>arguments.shift()</code>.  Uh-oh, looks like arguments.shift isn&#8217;t a function; but that&#8217;s a function of an Array.  Another bit of fun you can do is <code>console.log(arguments.constructor)</code>.  That will print to your console &#8220;Object()&#8221; while if you did <code>[].constructor</code>, that will print &#8220;Array()&#8221;.  Having fun yet?</p>
<p>This also isn&#8217;t just limited to <code>arguments</code>.  It seems that a lot of the DOM(document object model) collections are returned as these objects rather than arrays: <code>document.getElementsByTagName()</code>, <code>document.images</code>, <code>document.childNodes</code>.  In some cases, these array-not-arrays would be better suited to become arrays.</p>
<h3>Making the Array-like Objects become Arrays</h3>
<p>Well, that heading is a misnomer.  If we want those array-like objects to behave like arrays, we are going to need a new array.</p>
<pre class="ruby" title="code">var testFunction = function() {
  // Create a new array from the contents of arguments
  var args = Array.prototype.slice.call(arguments);

  var a = args.shift();
  console.log("The first argument is: %s", a);

  // send the remaining arguments to some other function
  someOtherFunction(args);
};</pre>
<p>Clearly, the magic is all in <code>Array.prototype.slice.call(arguments)</code>.  So let me break it down per piece.</p>
<dl>
<dt><code>Array</code></dt>
<dd>this is the name of the base object that we want</dd>
<dt><code>prototype</code></dt>
<dd>this can be thought of as the namespace for the instance methods of an array</dd>
<dt><code>slice</code></dt>
<dd>this extracts a section of an array and returns a new array, and without a beginning and ending index, it simply returns a copy of the array</dd>
<dt><code>call</code></dt>
<dd>this is a very useful function, it allows you to call a function from one object and use it in the context of another</dd>
</dl>
<p>Tada!  As a side note, if you are used to using <a href="http://www.prototypejs.org,">the prototype framework</a> then you can convert these array-like objects to arrays using <code>$A()</code>, even though Prototype does it&#8217;s array conversion differently.</p>
<p>Speaking of <code>$A</code>.  Let&#8217;s say you don&#8217;t like typing that long string code above and *you&#8217;re not using Prototype*.  Then you can create a shortcut just like the Prototype folks:</p>
<pre class="ruby" title="code">var $A = function(obj) {
  return Array.prototype.slice.call(obj);
};

// Example usage:
$A(document.getElementsByTagName("li"));</pre>
]]></content:encoded>
			<wfw:commentRss>http://shifteleven.com/articles/2007/06/28/array-like-objects-in-javascript/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
