<?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: arguments: A JavaScript Oddity</title>
	<atom:link href="http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/</link>
	<description>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
	<lastBuildDate>Sat, 07 Nov 2009 23:35:20 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: eric d.</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-864863</link>
		<dc:creator>eric d.</dc:creator>
		<pubDate>Mon, 19 Jan 2009 14:01:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-864863</guid>
		<description>Hi, thanks for that &lt;strong&gt;brilliant&lt;/strong&gt; article. Made a lot of things a lot clearer!
&lt;em&gt;Note:&lt;/em&gt; &lt;code&gt;new RegExp(&quot;%([1-&quot; + arguments.length + &quot;])&quot;, &quot;g&quot;);&lt;/code&gt; will fail passed 9 arguments (the regexp would be &lt;code&gt;&quot;%([1-10])&quot;&lt;/code&gt; so it will only match &lt;code&gt;%0&lt;/code&gt; and &lt;code&gt;%1&lt;/code&gt;).

I think an easy fix would be something like:
&lt;code&gt;function format(string) {
&#160;&#160;var args = arguments;
&#160;&#160;var pattern = new RegExp(&lt;strong&gt;&quot;%([0-9]+)&quot;&lt;/strong&gt;, &quot;g&quot;);
&#160;&#160;return String(string).replace(pattern, function(match, index) {
&#160;&#160;&#160;&#160;&lt;strong&gt;if (index == 0 &#124;&#124; index &gt;= args.length)
&#160;&#160;&#160;&#160;&#160;&#160;throw &quot;Invalid index in format string&quot;;&lt;/strong&gt;
&#160;&#160;&#160;&#160;return args[index];
&#160;&#160;});  
	};
&lt;/code&gt;
&lt;em&gt;(Sorry for nitpicking, I understand it was only an example and brevety is the main objective, but its a great function to have)&lt;/em&gt;</description>
		<content:encoded><![CDATA[<p>Hi, thanks for that <strong>brilliant</strong> article. Made a lot of things a lot clearer!<br />
<em>Note:</em> <code>new RegExp(&quot;%([1-&quot; + arguments.length + &quot;])&quot;, &quot;g&quot;);</code> will fail passed 9 arguments (the regexp would be <code>&quot;%([1-10])&quot;</code> so it will only match <code>%0</code> and <code>%1</code>).</p>
<p>I think an easy fix would be something like:<br />
<code>function format(string) {
&nbsp;&nbsp;var args = arguments;
&nbsp;&nbsp;var pattern = new RegExp(<strong>&quot;%([0-9]+)&quot;</strong>, &quot;g&quot;);
&nbsp;&nbsp;return String(string).replace(pattern, function(match, index) {
&nbsp;&nbsp;&nbsp;&nbsp;<strong>if (index == 0 || index &gt;= args.length)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;throw &quot;Invalid index in format string&quot;;</strong>
&nbsp;&nbsp;&nbsp;&nbsp;return args[index];
&nbsp;&nbsp;});  
	};
</code><br />
<em>(Sorry for nitpicking, I understand it was only an example and brevety is the main objective, but its a great function to have)</em></p>]]></content:encoded>
	</item>
	<item>
		<title>By: brothercake</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-825444</link>
		<dc:creator>brothercake</dc:creator>
		<pubDate>Thu, 13 Nov 2008 01:16:22 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-825444</guid>
		<description>But don&#039;t use arguments just for the sake of it - only if you really need to access them as a collection - because this:

&lt;code&gt;function colorize() { ... }&lt;/code&gt;

is not as easy to understand at a glance, as this:

&lt;code&gt;function colorize(foreground, background) { ... }&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>But don&#8217;t use arguments just for the sake of it &#8211; only if you really need to access them as a collection &#8211; because this:</p>
<code>function colorize() { ... }</code>
<p>is not as easy to understand at a glance, as this:</p>
<code>function colorize(foreground, background) { ... }</code>]]></content:encoded>
	</item>
	<item>
		<title>By: polvero</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-825275</link>
		<dc:creator>polvero</dc:creator>
		<pubDate>Wed, 12 Nov 2008 19:57:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-825275</guid>
		<description>instead of...
&lt;code&gt;var args = Array.prototype.slice.call(arguments); &lt;/code&gt;
do this...
&lt;code&gt;var args = [].slice.call(arguments, 0); &lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>instead of&#8230;<br />
<code>var args = Array.prototype.slice.call(arguments); </code><br />
do this&#8230;<br />
<code>var args = [].slice.call(arguments, 0); </code></p>]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Tetlaw</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-824797</link>
		<dc:creator>Andrew Tetlaw</dc:creator>
		<pubDate>Wed, 12 Nov 2008 00:21:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-824797</guid>
		<description>Trep, yeah I didn&#039;t explain the closures that are used in the above code.

you&#039;ll notice the variable &lt;code&gt;times&lt;/code&gt;, is passed to the outer function, but used in the anonymous function. Which is fine, JavaScript let&#039;s us do that.

However, every time a function is called the local variable &lt;code&gt;arguments&lt;/code&gt; is created. So I can use it safely inside the inner anonymous function here:

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;fn.apply(null, arguments);&lt;/code&gt;&lt;/pre&gt;

Because I know that when that anonymous function is eventually called, &lt;code&gt;arguments&lt;/code&gt; will contain the arguments that are supplied at the time it&#039;s called, not at the time when we are defining the anonymous function.

However, to use setTimeout I have to create yet another anonymous function, the one that calls apply:

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;function(){self.apply(null,args)}&lt;/code&gt;&lt;/pre&gt;

However, to the apply call I need to pass the original arguments that were supplied to the outer anonymous function... so I use yet another closure:

&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var args = Array.prototype.slice.call(arguments);&lt;/code&gt;&lt;/pre&gt; 

The &lt;code&gt;args&lt;/code&gt; variable is defined in the outer anonymous function, but used within the inner anonymous function.

Phew!</description>
		<content:encoded><![CDATA[<p>Trep, yeah I didn&#8217;t explain the closures that are used in the above code.</p>
<p>you&#8217;ll notice the variable <code>times</code>, is passed to the outer function, but used in the anonymous function. Which is fine, JavaScript let&#8217;s us do that.</p>
<p>However, every time a function is called the local variable <code>arguments</code> is created. So I can use it safely inside the inner anonymous function here:</p>
<pre><code class="javascript">fn.apply(null, arguments);</code></pre>
<p>Because I know that when that anonymous function is eventually called, <code>arguments</code> will contain the arguments that are supplied at the time it&#8217;s called, not at the time when we are defining the anonymous function.</p>
<p>However, to use setTimeout I have to create yet another anonymous function, the one that calls apply:</p>
<pre><code class="javascript">function(){self.apply(null,args)}</code></pre>
<p>However, to the apply call I need to pass the original arguments that were supplied to the outer anonymous function&#8230; so I use yet another closure:</p>
<pre><code class="javascript">var args = Array.prototype.slice.call(arguments);</code></pre>
<p>The <code>args</code> variable is defined in the outer anonymous function, but used within the inner anonymous function.</p>
<p>Phew!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Trep</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-824791</link>
		<dc:creator>Trep</dc:creator>
		<pubDate>Wed, 12 Nov 2008 00:07:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-824791</guid>
		<description>Errata: the third paragraph should read:
&#171;I made some experiments and it works just as well if you call it on the “args” array created by splicing “arguments”&#187;.</description>
		<content:encoded><![CDATA[<p>Errata: the third paragraph should read:<br />
&laquo;I made some experiments and it works just as well if you call it on the “args” array created by splicing “arguments”&raquo;.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Trep</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-824789</link>
		<dc:creator>Trep</dc:creator>
		<pubDate>Wed, 12 Nov 2008 00:05:04 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-824789</guid>
		<description>Hmmm... that repeat() function is really one of those little thingy to sit down and meditate on! ;-)
-

I was wondering: the &quot;apply&quot; method should take an array as its second argument, yet you call it successfully as
--
fn.apply(null, arguments);
--
if made some experiments and it works just as well if you call on the &quot;args&quot; array created by splicing &quot;arguments&quot;.
-
On the other hand &quot;self.apply(null,args)&quot; doesn&#039;t work if you call it on &quot;arguments&quot; instead of &quot;args&quot;.
-
Hm... ok, I got it. &quot;self.apply(null,args)&quot; is inside another anonymous function so &quot;arguments&quot; get stomped over by the new one. The power of meditation!</description>
		<content:encoded><![CDATA[<p>Hmmm&#8230; that repeat() function is really one of those little thingy to sit down and meditate on! ;-)<br />
-</p>
<p>I was wondering: the &#8220;apply&#8221; method should take an array as its second argument, yet you call it successfully as<br />
&#8211;<br />
fn.apply(null, arguments);<br />
&#8211;<br />
if made some experiments and it works just as well if you call on the &#8220;args&#8221; array created by splicing &#8220;arguments&#8221;.<br />
-<br />
On the other hand &#8220;self.apply(null,args)&#8221; doesn&#8217;t work if you call it on &#8220;arguments&#8221; instead of &#8220;args&#8221;.<br />
-<br />
Hm&#8230; ok, I got it. &#8220;self.apply(null,args)&#8221; is inside another anonymous function so &#8220;arguments&#8221; get stomped over by the new one. The power of meditation!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Jesse</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-824784</link>
		<dc:creator>Jesse</dc:creator>
		<pubDate>Tue, 11 Nov 2008 23:56:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-824784</guid>
		<description>Don&#039;t get me wrong, I&#039;m a big fan of Javascript and consider it one of the most expressive languages I&#039;ve worked with, but while it allows for some incredibly elegant solutions to problems, it also fosters a tremendous amount in-elegance in readability and clarity of purpose. I was just wondering if there are any best-practices for code layout, naming, commenting, beyond the usual programming conventions.</description>
		<content:encoded><![CDATA[<p>Don&#8217;t get me wrong, I&#8217;m a big fan of Javascript and consider it one of the most expressive languages I&#8217;ve worked with, but while it allows for some incredibly elegant solutions to problems, it also fosters a tremendous amount in-elegance in readability and clarity of purpose. I was just wondering if there are any best-practices for code layout, naming, commenting, beyond the usual programming conventions.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: awasson</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-824775</link>
		<dc:creator>awasson</dc:creator>
		<pubDate>Tue, 11 Nov 2008 23:33:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-824775</guid>
		<description>Really cool article!

A couple of years ago I stumbled upon the arguments variable while writing a quick-n-dirty slideshow script. I only learned enough to do the job so it&#039;s great to get some more insight &amp; info from your article.

Thanks,
Andrew </description>
		<content:encoded><![CDATA[<p>Really cool article!</p>
<p>A couple of years ago I stumbled upon the arguments variable while writing a quick-n-dirty slideshow script. I only learned enough to do the job so it&#8217;s great to get some more insight &amp; info from your article.</p>
<p>Thanks,<br />
Andrew </p>]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Tetlaw</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-824762</link>
		<dc:creator>Andrew Tetlaw</dc:creator>
		<pubDate>Tue, 11 Nov 2008 22:58:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-824762</guid>
		<description>Jessie, yeah, it&#039;s hard to write, hard to work out what the code does if you didn&#039;t write it, and just as hard to explain what it&#039;s dong in a blog post like this :)

There&#039;s lots of invisible forces at work, especially regarding scope. But once, you are familiar with the patterns, you can identify and write them easy enough.

It makes JavaScript so expressive though, and that makes it worth persisting with.</description>
		<content:encoded><![CDATA[<p>Jessie, yeah, it&#8217;s hard to write, hard to work out what the code does if you didn&#8217;t write it, and just as hard to explain what it&#8217;s dong in a blog post like this :)</p>
<p>There&#8217;s lots of invisible forces at work, especially regarding scope. But once, you are familiar with the patterns, you can identify and write them easy enough.</p>
<p>It makes JavaScript so expressive though, and that makes it worth persisting with.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew Tetlaw</title>
		<link>http://www.sitepoint.com/blogs/2008/11/11/arguments-a-javascript-oddity/comment-page-1/#comment-824752</link>
		<dc:creator>Andrew Tetlaw</dc:creator>
		<pubDate>Tue, 11 Nov 2008 22:29:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.sitepoint.com/blogs/?p=3170#comment-824752</guid>
		<description>Dougal, you&#039;re right! Sorry for making such a noob mistake. The correct way to check is this:

&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;  var a = [];
  alert(a.constructor.toString());&lt;/code&gt;&lt;/pre&gt;

In Firefox that will return:

&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;function Array() { [native code] }&lt;/code&gt;&lt;/pre&gt;

Inside a function, if you call:

&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;  alert(arguments.constructor.toString());&lt;/code&gt;&lt;/pre&gt;

You&#039;ll get:

&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;function Object() { [native code] }&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Dougal, you&#8217;re right! Sorry for making such a noob mistake. The correct way to check is this:</p>
<pre><code class="html">  var a = [];
  alert(a.constructor.toString());</code></pre>
<p>In Firefox that will return:</p>
<pre><code class="html">function Array() { [native code] }</code></pre>
<p>Inside a function, if you call:</p>
<pre><code class="html">  alert(arguments.constructor.toString());</code></pre>
<p>You&#8217;ll get:</p>
<pre><code class="html">function Object() { [native code] }</code></pre>]]></content:encoded>
	</item>
</channel>
</rss>
