<?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: Maintaining state within anonymous functions</title>
	<atom:link href="http://www.sitepoint.com/blogs/2004/05/31/maintaining-state-within-anonymous-functions/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sitepoint.com/blogs/2004/05/31/maintaining-state-within-anonymous-functions/</link>
	<description>News, opinion, and fresh thinking for web developers and designers. The official podcast of sitepoint.com.</description>
	<lastBuildDate>Mon, 23 Nov 2009 09:18:42 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: liorean</title>
		<link>http://www.sitepoint.com/blogs/2004/05/31/maintaining-state-within-anonymous-functions/comment-page-1/#comment-3471</link>
		<dc:creator>liorean</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">2034567510#comment-3471</guid>
		<description>&lt;p&gt;Well, you aren&#039;t really using a local value, there. Also, I believe you&#039;re using an ineffective way to recur. As an example we can take the factorial function:&lt;/p&gt;

&lt;pre&gt;
&lt;code class=&#039;javascript&#039;&gt;

(function(a){
    return (a&gt;0)?
        (a*arguments.callee(--a)):
        1;&lt;br /&gt;
})(4); // =&gt; 24

&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;This is an anonymous function expression, using a lookup of arguments and then a lookup of callee. Another method to do the same thing would be to use a named function expression, and use that local name as selfreference, producing only a single lookup of self. &lt;/p&gt;

&lt;pre&gt;
&lt;code class=&#039;javascript&#039;&gt;
(function self(a){
    return (a&gt;0)?
        (a*self(--a)):
        1; 
})(4); // =&gt; 24
&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;And a clumsier third way would be to use a closure instead, requiring a lookup, scope jump, lookup for both a and f:&lt;/p&gt;

&lt;pre&gt;
&lt;code class=&#039;javascript&#039;&gt;

(function(a,f){
    return (f=function(){
        return (a&gt;0)?
            (a*f(--a)):
            1;
    })();
})(4); // =&gt; 24

&lt;/code&gt;
&lt;/pre&gt;

&lt;p&gt;Now, both of the first ways, if you apply them to your example, would create a member of the function object itself. If the function object happens to be stored in a variable, you could access it from the outside. However, if you implement it as a closure bound variable a la 3, you get it to be private but still persistent. Thus, double wrapping it in function expressions might ensure privacy. Your example might not need it, but I&#039;m sure there are cases that does.&lt;/p&gt;

</description>
		<content:encoded><![CDATA[<p>Well, you aren&#8217;t really using a local value, there. Also, I believe you&#8217;re using an ineffective way to recur. As an example we can take the factorial function:</p>
<pre>
<code class='javascript'>

(function(a){
    return (a>0)?
        (a*arguments.callee(--a)):
        1;<br />
})(4); // => 24

</code>
</pre>
<p>This is an anonymous function expression, using a lookup of arguments and then a lookup of callee. Another method to do the same thing would be to use a named function expression, and use that local name as selfreference, producing only a single lookup of self. </p>
<pre>
<code class='javascript'>
(function self(a){
    return (a>0)?
        (a*self(--a)):
        1; 
})(4); // => 24
</code>
</pre>
<p>And a clumsier third way would be to use a closure instead, requiring a lookup, scope jump, lookup for both a and f:</p>
<pre>
<code class='javascript'>

(function(a,f){
    return (f=function(){
        return (a>0)?
            (a*f(--a)):
            1;
    })();
})(4); // => 24

</code>
</pre>
<p>Now, both of the first ways, if you apply them to your example, would create a member of the function object itself. If the function object happens to be stored in a variable, you could access it from the outside. However, if you implement it as a closure bound variable a la 3, you get it to be private but still persistent. Thus, double wrapping it in function expressions might ensure privacy. Your example might not need it, but I&#8217;m sure there are cases that does.</p>]]></content:encoded>
	</item>
	<item>
		<title>By: liorean</title>
		<link>http://www.sitepoint.com/blogs/2004/05/31/maintaining-state-within-anonymous-functions/comment-page-1/#comment-3472</link>
		<dc:creator>liorean</dc:creator>
		<pubDate>Tue, 30 Nov 1999 00:00:00 +0000</pubDate>
		<guid isPermaLink="false">2034567510#comment-3472</guid>
		<description>&lt;p&gt;Hmm, butchered my nicely formatted samples. Oh well. Anyway I just say a mistake there:&lt;br /&gt;
(a*f(--a)) should be (a--*f()) in the last example.&lt;/p&gt;

</description>
		<content:encoded><![CDATA[<p>Hmm, butchered my nicely formatted samples. Oh well. Anyway I just say a mistake there:<br />
(a*f(&#8211;a)) should be (a&#8211;*f()) in the last example.</p>]]></content:encoded>
	</item>
</channel>
</rss>
