<?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: Functions, activation objects and &#8216;this&#8217; in ActionScript 3</title>
	<atom:link href="http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/</link>
	<description>code = joy</description>
	<lastBuildDate>Fri, 02 Dec 2011 01:36:37 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
	<item>
		<title>By: Corona Game Example: OOP, Scope, and Box2D Collisions &#8211; Part 1 &#124; Flex &#38; Flash Consultant &#8211; Jesse Warden dot Kizz-ohm</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-26826</link>
		<dc:creator>Corona Game Example: OOP, Scope, and Box2D Collisions &#8211; Part 1 &#124; Flex &#38; Flash Consultant &#8211; Jesse Warden dot Kizz-ohm</dc:creator>
		<pubDate>Tue, 05 Jul 2011 00:02:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-26826</guid>
		<description>[...] problem&#8230;?&#8221; in his/her head. Specifically all the scope issues we had in AS1/AS2 using Activation Objects, and Delegate. Functional afficiandos&#8217; think it&#8217;s neat&#8230; until they have a [...]</description>
		<content:encoded><![CDATA[<p>[...] problem&#8230;?&#8221; in his/her head. Specifically all the scope issues we had in AS1/AS2 using Activation Objects, and Delegate. Functional afficiandos&#8217; think it&#8217;s neat&#8230; until they have a [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lua for ActionScript Developers: Crash Course &#124; Flex &#38; Flash Consultant &#8211; Jesse Warden dot Kizz-ohm</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-25086</link>
		<dc:creator>Lua for ActionScript Developers: Crash Course &#124; Flex &#38; Flash Consultant &#8211; Jesse Warden dot Kizz-ohm</dc:creator>
		<pubDate>Tue, 25 Jan 2011 17:46:13 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-25086</guid>
		<description>[...] You can read more about lexical scoping, closures, and all the other Functional stuff if you want, but I wanted to focus on Scope because it&#8217;s bloody AS1&#8242;s thisThing.owner/mx.utils.Delegate all over again. If you don&#8217;t remember what I&#8217;m talking about, or you have no context, Keith talks about it and how to get around ActionScript 1&#8242;s scoping challenges via the Activation Object and Mike Chambers elaborates on how closures work in AS3. [...]</description>
		<content:encoded><![CDATA[<p>[...] You can read more about lexical scoping, closures, and all the other Functional stuff if you want, but I wanted to focus on Scope because it&#8217;s bloody AS1&#8242;s thisThing.owner/mx.utils.Delegate all over again. If you don&#8217;t remember what I&#8217;m talking about, or you have no context, Keith talks about it and how to get around ActionScript 1&#8242;s scoping challenges via the Activation Object and Mike Chambers elaborates on how closures work in AS3. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kirill Mourzenko</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-22137</link>
		<dc:creator>Kirill Mourzenko</dc:creator>
		<pubDate>Fri, 14 May 2010 14:25:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-22137</guid>
		<description>I wouldn&#039;t take that literally. Usage of nameless functions is not indicative of bad architecture right off the bat. But it is best not to use them in application code to avoid memory leaks and not to design APIs that rely on them. When used carefully they can help you create some very powerful, higher level mechanisms inside frameworks.</description>
		<content:encoded><![CDATA[<p>I wouldn&#8217;t take that literally. Usage of nameless functions is not indicative of bad architecture right off the bat. But it is best not to use them in application code to avoid memory leaks and not to design APIs that rely on them. When used carefully they can help you create some very powerful, higher level mechanisms inside frameworks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Evan</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-15729</link>
		<dc:creator>Evan</dc:creator>
		<pubDate>Wed, 04 Feb 2009 16:46:07 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-15729</guid>
		<description>Grant, I am a bit confused.  When you state that the use of anonymous functions within code can indicate an architectural problem I would take that at near gospel truth - as you are the industry leader. (sometimes I wonder if Grant Skinner is a pseudonym for 10 Superman type flash guys - just kidding). 

but I came across this post that uses an anonymous event handler function as part of a function literal within a function closure in order to  &quot;pass arguments to an event handler&quot; 
http://www.transcendentaxis.com/dthompson/blog/archives/19#more-19

Would you say that this could be an acceptable use case of an anonymous function?</description>
		<content:encoded><![CDATA[<p>Grant, I am a bit confused.  When you state that the use of anonymous functions within code can indicate an architectural problem I would take that at near gospel truth &#8211; as you are the industry leader. (sometimes I wonder if Grant Skinner is a pseudonym for 10 Superman type flash guys &#8211; just kidding). </p>
<p>but I came across this post that uses an anonymous event handler function as part of a function literal within a function closure in order to  &#8220;pass arguments to an event handler&#8221;<br />
<a href="http://www.transcendentaxis.com/dthompson/blog/archives/19#more-19" rel="nofollow">http://www.transcendentaxis.com/dthompson/blog/archives/19#more-19</a></p>
<p>Would you say that this could be an acceptable use case of an anonymous function?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: mikechambers</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-14307</link>
		<dc:creator>mikechambers</dc:creator>
		<pubDate>Thu, 09 Oct 2008 18:58:40 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-14307</guid>
		<description>@grant

Good catch (I updated the post).

Here is the info from the docs:
http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_21.html

Activation Object is an object that &quot;is created that stores the parameters and any local variables or functions declared in the function body. &quot;

&quot;A function closure is an object that contains a snapshot of a function and its lexical environment. A function&#039;s lexical environment includes all the variables, properties, methods, and objects in the function&#039;s scope chain, along with their values. Function closures are created any time a function is executed apart from an object or a class.&quot;

mike chambers

mesh@adobe.com</description>
		<content:encoded><![CDATA[<p>@grant</p>
<p>Good catch (I updated the post).</p>
<p>Here is the info from the docs:<br />
<a href="http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_21.html" rel="nofollow">http://livedocs.adobe.com/flex/3/html/help.html?content=03_Language_and_Syntax_21.html</a></p>
<p>Activation Object is an object that &#8220;is created that stores the parameters and any local variables or functions declared in the function body. &#8221;</p>
<p>&#8220;A function closure is an object that contains a snapshot of a function and its lexical environment. A function&#8217;s lexical environment includes all the variables, properties, methods, and objects in the function&#8217;s scope chain, along with their values. Function closures are created any time a function is executed apart from an object or a class.&#8221;</p>
<p>mike chambers</p>
<p><a href="mailto:mesh@adobe.com">mesh@adobe.com</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-14305</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Thu, 09 Oct 2008 18:10:14 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-14305</guid>
		<description>this is definitely an old tric i&#039;ve used since as1 and still use for javascript when needed. But i would agree with Grant, it&#039;s usually quite avoidable, although I tend to use it for my inline setInterval functions and setTimeout stuff, javascript obviously</description>
		<content:encoded><![CDATA[<p>this is definitely an old tric i&#8217;ve used since as1 and still use for javascript when needed. But i would agree with Grant, it&#8217;s usually quite avoidable, although I tend to use it for my inline setInterval functions and setTimeout stuff, javascript obviously</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Grant Skinner</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-14302</link>
		<dc:creator>Grant Skinner</dc:creator>
		<pubDate>Thu, 09 Oct 2008 15:52:10 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-14302</guid>
		<description>Unless I&#039;m mistaken, you have your terminology slightly confused. The closure object is a transparent object that wraps function references and maintains a pointer back to the scope it was created in. This only applies to named / non-dynamic functions. Closure objects are new in AS3, and are the reason that the Delegate class is no longer needed for things like event listeners (because the method is wrapped in a CO, and is called in its original scope).

What you are referring to above is the &quot;activation object&quot; of anonymous functions. The activation object holds the local variables that were defined when the anonymous function was instantiated. Activation objects have been around since AS1.

It&#039;s also worth noting that in almost every case, the use of an anonymous function in your code indicates an architectural problem. There are almost no real uses for anonymous functions - they are less efficient, much harder to debug, and far harder to grok when reading code.</description>
		<content:encoded><![CDATA[<p>Unless I&#8217;m mistaken, you have your terminology slightly confused. The closure object is a transparent object that wraps function references and maintains a pointer back to the scope it was created in. This only applies to named / non-dynamic functions. Closure objects are new in AS3, and are the reason that the Delegate class is no longer needed for things like event listeners (because the method is wrapped in a CO, and is called in its original scope).</p>
<p>What you are referring to above is the &#8220;activation object&#8221; of anonymous functions. The activation object holds the local variables that were defined when the anonymous function was instantiated. Activation objects have been around since AS1.</p>
<p>It&#8217;s also worth noting that in almost every case, the use of an anonymous function in your code indicates an architectural problem. There are almost no real uses for anonymous functions &#8211; they are less efficient, much harder to debug, and far harder to grok when reading code.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris Deely</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-14294</link>
		<dc:creator>Chris Deely</dc:creator>
		<pubDate>Thu, 09 Oct 2008 02:36:28 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-14294</guid>
		<description>Nice explanation, Mike.  I notice that in your example you made use of a class declared outside of your main package.  I have used that approach many times in my current AS3 project.

However, today I stumbled upon a strange error related to their use.  If you take this same code, and make FunctionClosureTest implement any interface, you will get compile errors in Flash CS4!

I am hoping this is just a bug in the beta version I was using, but if you try this code, I think you&#039;ll see the same results I did.

&lt;code&gt;
package {
	import flash.display.Sprite;
        import ITest;

	public class FunctionClosureTest extends Sprite implements ITest
	{
		public function FunctionClosureTest()
		{
                    trace(&quot;Hello world&quot;);
                }
                public function method1():void
                {
                     trace(&quot;method1&quot;);
                }
        }
}
class Foo
{
	public function runFunction(f:Function):void
	{
		f();
	}
}
&lt;/code&gt;

And elsewhere define the interface...
&lt;code&gt;
package {
   public interface ITest
   {
       function method1():void
   }
}
&lt;/code&gt;

CS4 will tell you that you have not implemented the interface appropriately, although the required methods are there.  Even stranger still, the solution is to add another interface!

The following works just fine:
&lt;code&gt;
public class FunctionClosureTest extends Sprite implements ITest, IFixTheProblem
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Nice explanation, Mike.  I notice that in your example you made use of a class declared outside of your main package.  I have used that approach many times in my current AS3 project.</p>
<p>However, today I stumbled upon a strange error related to their use.  If you take this same code, and make FunctionClosureTest implement any interface, you will get compile errors in Flash CS4!</p>
<p>I am hoping this is just a bug in the beta version I was using, but if you try this code, I think you&#8217;ll see the same results I did.</p>
<p><code><br />
package {<br />
	import flash.display.Sprite;<br />
        import ITest;</p>
<p>	public class FunctionClosureTest extends Sprite implements ITest<br />
	{<br />
		public function FunctionClosureTest()<br />
		{<br />
                    trace("Hello world");<br />
                }<br />
                public function method1():void<br />
                {<br />
                     trace("method1");<br />
                }<br />
        }<br />
}<br />
class Foo<br />
{<br />
	public function runFunction(f:Function):void<br />
	{<br />
		f();<br />
	}<br />
}<br />
</code></p>
<p>And elsewhere define the interface&#8230;<br />
<code><br />
package {<br />
   public interface ITest<br />
   {<br />
       function method1():void<br />
   }<br />
}<br />
</code></p>
<p>CS4 will tell you that you have not implemented the interface appropriately, although the required methods are there.  Even stranger still, the solution is to add another interface!</p>
<p>The following works just fine:<br />
<code><br />
public class FunctionClosureTest extends Sprite implements ITest, IFixTheProblem<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sev</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-14292</link>
		<dc:creator>Sev</dc:creator>
		<pubDate>Wed, 08 Oct 2008 23:30:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-14292</guid>
		<description>Best is to avoid function closures whenever possible. Contrary to Javascript they are a quite a performance retarder too. Apart from that I LOVE that methods are bound to their instance&#039;s scope now: makes a coder&#039;s life so much more easier (never ever Delegate anymore!) :-) Still the internally created method closures remain a mystery to me..</description>
		<content:encoded><![CDATA[<p>Best is to avoid function closures whenever possible. Contrary to Javascript they are a quite a performance retarder too. Apart from that I LOVE that methods are bound to their instance&#8217;s scope now: makes a coder&#8217;s life so much more easier (never ever Delegate anymore!) :-) Still the internally created method closures remain a mystery to me..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Darren</title>
		<link>http://www.mikechambers.com/blog/2008/10/08/function-closures-and-this-in-actionscript-3/comment-page-1/#comment-14290</link>
		<dc:creator>Darren</dc:creator>
		<pubDate>Wed, 08 Oct 2008 22:42:29 +0000</pubDate>
		<guid isPermaLink="false">http://www.mikechambers.com/blog/?p=1560#comment-14290</guid>
		<description>I also ran into this problem in AS2. I eneded up putting something similar to var scope:Object = this; at the top of all my Class methods. What&#039;s changed with AS3?</description>
		<content:encoded><![CDATA[<p>I also ran into this problem in AS2. I eneded up putting something similar to var scope:Object = this; at the top of all my Class methods. What&#8217;s changed with AS3?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

