<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: JavaScript Function Arguments: They&#8217;re An Array And You Can Treat Them As Such</title>
	<atom:link href="http://www.dougv.com/2009/06/20/javascript-function-arguments-theyre-an-array-and-you-can-treat-them-as-such/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dougv.com/2009/06/20/javascript-function-arguments-theyre-an-array-and-you-can-treat-them-as-such/</link>
	<description>ASP.NET, PHP, XML, JavaScript, DOM, Web geekery</description>
	<lastBuildDate>Mon, 02 Jan 2012 21:22:08 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
	<item>
		<title>By: How I Code On This Blog: Elegance Vs. Transparency &#187; dougv.com « The Web home of Doug Vanderweide</title>
		<link>http://www.dougv.com/2009/06/20/javascript-function-arguments-theyre-an-array-and-you-can-treat-them-as-such/#comment-280</link>
		<dc:creator>How I Code On This Blog: Elegance Vs. Transparency &#187; dougv.com « The Web home of Doug Vanderweide</dc:creator>
		<pubDate>Fri, 26 Jun 2009 22:43:32 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougv.com/blog/?p=2342#comment-280</guid>
		<description>[...] recent comment exchange with Scriptar has prodded me to post an explanation of how I code on this [...]</description>
		<content:encoded><![CDATA[<p>[...] recent comment exchange with Scriptar has prodded me to post an explanation of how I code on this [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Vanderweide</title>
		<link>http://www.dougv.com/2009/06/20/javascript-function-arguments-theyre-an-array-and-you-can-treat-them-as-such/#comment-279</link>
		<dc:creator>Doug Vanderweide</dc:creator>
		<pubDate>Fri, 26 Jun 2009 21:49:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougv.com/blog/?p=2342#comment-279</guid>
		<description>Scriptar: Good points.

I probably have oversold the idea of JavaScript arguments being an array. As you note, standard array functions built in to JavaScript will not work on a collection.

My target audience -- newer programmers -- tends not to be that sophisticated, so I try as often as possible to employ generalities that usually hold true. Even so, it&#039;s definitely valuable to note that contrary to the title of this post, JavaScript function arguments are not truly an array. Thanks for keeping me honest.</description>
		<content:encoded><![CDATA[<p>Scriptar: Good points.</p>
<p>I probably have oversold the idea of JavaScript arguments being an array. As you note, standard array functions built in to JavaScript will not work on a collection.</p>
<p>My target audience &#8212; newer programmers &#8212; tends not to be that sophisticated, so I try as often as possible to employ generalities that usually hold true. Even so, it&#8217;s definitely valuable to note that contrary to the title of this post, JavaScript function arguments are not truly an array. Thanks for keeping me honest.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scriptar</title>
		<link>http://www.dougv.com/2009/06/20/javascript-function-arguments-theyre-an-array-and-you-can-treat-them-as-such/#comment-278</link>
		<dc:creator>Scriptar</dc:creator>
		<pubDate>Wed, 24 Jun 2009 21:05:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougv.com/blog/?p=2342#comment-278</guid>
		<description>What I was attempting to demonstrate from my example is that, while you could use the &quot;arguments&quot; object&#039;s array-like subscript syntax to access its values (as with all JavaScript objects), and it does have an array-like &quot;length&quot; property; the &quot;arguments&quot; collection &lt;em&gt;is not&lt;/em&gt; an array and you can&#039;t treat it as such because array methods such as sort(), join(), slice(), push() and pop() will not work on it. It is better to pass an actual array as one parameter to a function. Better yet, a single object with named properties where the function checks expected property existence and validity by name instead of by index:

&lt;pre lang=&quot;javascript&quot;&gt;function doStuff_v1() {
  // error, can&#039;t use join() method...
  alert(&quot;My arguments are: &quot; + arguments.join(&quot;, &quot;));
}
function doStuff_v2(arr) {
  // pretty good...
  alert(&quot;My arguments are: &quot; + arr.join(&quot;, &quot;));
}
function doStuff_v3(args) {
  // best pattern...
  alert(args.msg + args.arr.join(&quot;, &quot;));
}

try {
  // errors
	doStuff_v1(&quot;arg1&quot;, &quot;arg2&quot;, &quot;arg3&quot;);
} catch (e) {
	alert(&quot;Error: &quot; + e.message);
}

// displays &quot;My arguments are: arg1, arg2, arg3&quot;
doStuff_v2([&quot;arg1&quot;, &quot;arg2&quot;, &quot;arg3&quot;]);

// displays: &quot;Last time: arg1, arg2, arg3&quot;
doStuff_v3({msg: &quot;Last time: &quot;, arr: [&quot;arg1&quot;, &quot;arg2&quot;, &quot;arg3&quot;]});&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>What I was attempting to demonstrate from my example is that, while you could use the &#8220;arguments&#8221; object&#8217;s array-like subscript syntax to access its values (as with all JavaScript objects), and it does have an array-like &#8220;length&#8221; property; the &#8220;arguments&#8221; collection <em>is not</em> an array and you can&#8217;t treat it as such because array methods such as sort(), join(), slice(), push() and pop() will not work on it. It is better to pass an actual array as one parameter to a function. Better yet, a single object with named properties where the function checks expected property existence and validity by name instead of by index:</p>
<pre lang="javascript">function doStuff_v1() {
  // error, can't use join() method...
  alert("My arguments are: " + arguments.join(", "));
}
function doStuff_v2(arr) {
  // pretty good...
  alert("My arguments are: " + arr.join(", "));
}
function doStuff_v3(args) {
  // best pattern...
  alert(args.msg + args.arr.join(", "));
}

try {
  // errors
	doStuff_v1("arg1", "arg2", "arg3");
} catch (e) {
	alert("Error: " + e.message);
}

// displays "My arguments are: arg1, arg2, arg3"
doStuff_v2(["arg1", "arg2", "arg3"]);

// displays: "Last time: arg1, arg2, arg3"
doStuff_v3({msg: "Last time: ", arr: ["arg1", "arg2", "arg3"]});</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Doug Vanderweide</title>
		<link>http://www.dougv.com/2009/06/20/javascript-function-arguments-theyre-an-array-and-you-can-treat-them-as-such/#comment-277</link>
		<dc:creator>Doug Vanderweide</dc:creator>
		<pubDate>Wed, 24 Jun 2009 02:42:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougv.com/blog/?p=2342#comment-277</guid>
		<description>Scriptar: As you note, technically, the arguments of a function are a collection (a &lt;em&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Collection_(computing)&quot; rel=&quot;nofollow&quot;&gt;collection&lt;/a&gt;&lt;/em&gt;, for most intents and purposes, is an array of similar objects). For convenience, thanks to weak typing and implicit type casting, we can almost always treat the arguments of a function as an array of strings. As such, using the identity operator, as you note, to test for strings will almost always yield true, unless we are explicitly passing DOM or other objects.</description>
		<content:encoded><![CDATA[<p>Scriptar: As you note, technically, the arguments of a function are a collection (a <em><a href="http://en.wikipedia.org/wiki/Collection_(computing)" rel="nofollow">collection</a></em>, for most intents and purposes, is an array of similar objects). For convenience, thanks to weak typing and implicit type casting, we can almost always treat the arguments of a function as an array of strings. As such, using the identity operator, as you note, to test for strings will almost always yield true, unless we are explicitly passing DOM or other objects.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scriptar</title>
		<link>http://www.dougv.com/2009/06/20/javascript-function-arguments-theyre-an-array-and-you-can-treat-them-as-such/#comment-276</link>
		<dc:creator>Scriptar</dc:creator>
		<pubDate>Mon, 22 Jun 2009 21:43:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougv.com/blog/?p=2342#comment-276</guid>
		<description>The bonus &quot;arguments&quot; parameter is not an array, but is an object with array-like properties. Because of a design error, the &quot;arguments&quot; property does not have all of the typical array methods available to it. For this and other reasons, I believe it is preferable to use named arguments when passing an unknown number of values into a function. Then, you can have default parameters and use array functions on arrays without having to worry about the order of your parameters. For example:
&lt;pre lang=&quot;javascript&quot;&gt;
var allTheAnimals = function (args) {
  args.output = (typeof args.output === &quot;string&quot;) ? args.output : &quot;The animals you provided are: &quot;;
  alert(args.output + args.animals.sort().join(&quot;, &quot;));
};

allTheAnimals({animals: [&quot;Dog&quot;, &quot;Cow&quot;, &quot;Chicken&quot;, &quot;Cat&quot;]});
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>The bonus &#8220;arguments&#8221; parameter is not an array, but is an object with array-like properties. Because of a design error, the &#8220;arguments&#8221; property does not have all of the typical array methods available to it. For this and other reasons, I believe it is preferable to use named arguments when passing an unknown number of values into a function. Then, you can have default parameters and use array functions on arrays without having to worry about the order of your parameters. For example:</p>
<pre lang="javascript">
var allTheAnimals = function (args) {
  args.output = (typeof args.output === "string") ? args.output : "The animals you provided are: ";
  alert(args.output + args.animals.sort().join(", "));
};

allTheAnimals({animals: ["Dog", "Cow", "Chicken", "Cat"]});
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Parent &#8211; Child Select Lists Revisited: Multiple Parent &#8211; Child Select Lists Via PHP, MySQL And jQuery &#187; dougv.com « The Web home of Doug Vanderweide</title>
		<link>http://www.dougv.com/2009/06/20/javascript-function-arguments-theyre-an-array-and-you-can-treat-them-as-such/#comment-275</link>
		<dc:creator>Parent &#8211; Child Select Lists Revisited: Multiple Parent &#8211; Child Select Lists Via PHP, MySQL And jQuery &#187; dougv.com « The Web home of Doug Vanderweide</dc:creator>
		<pubDate>Sun, 21 Jun 2009 18:05:18 +0000</pubDate>
		<guid isPermaLink="false">http://www.dougv.com/blog/?p=2342#comment-275</guid>
		<description>[...] Copyright, Attribution &amp; Donations      « JavaScript Function Arguments: They’re An Array And You Can Treat Them As Such [...]</description>
		<content:encoded><![CDATA[<p>[...] Copyright, Attribution &amp; Donations      « JavaScript Function Arguments: They’re An Array And You Can Treat Them As Such [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

