<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">

    <title type="text">Studio Koi Code Blog</title>
    <subtitle type="text">Studio Koi Code Blog:</subtitle>
    <link rel="alternate" type="text/html" href="http://studiokoi.com/blog/index/" />
    <link rel="self" type="application/atom+xml" href="http://studiokoi.com/code/atom/" />
    <updated>2010-02-15T01:31:25Z</updated>
    <rights>Copyright (c) 2010, Greg Ferrell</rights>
    <generator uri="http://www.expressionengine.com/" version="1.6.8">ExpressionEngine</generator>
    <id>tag:studiokoi.com,2010:02:15</id>


    <entry>
      <title>Hope This works</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/hope_this_works/" />
      <id>tag:studiokoi.com,2010:blog/index/1.53</id>
      <published>2010-02-15T00:58:24Z</published>
      <updated>2010-02-15T01:31:25Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <content type="html"><![CDATA[
        <p>Just testing
</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Simple argument defaults in JavaScript</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/simple_argument_defaults_in_javascript/" />
      <id>tag:studiokoi.com,2009:blog/index/1.52</id>
      <published>2009-09-27T03:52:00Z</published>
      <updated>2009-09-26T20:15:48Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="JavaScript"
        scheme="http://studiokoi.com/code/category/JavaScript/"
        label="JavaScript" />
      <content type="html"><![CDATA[
        <p>In many other interpreted programming languages like: Python, PHP, ActionScript 3.0, etc., you can have defaults to arguments that do not get passed to functions. In the three afore mentioned languages, it's as easy as saying arg = 'default' inline in the function definition. This however is not available in the JavaScript interpreter. (It might be in ECMAScript 5, though.) But that can easily be remedied with a simple helper function.</p>

<p>I have met other programmers that find it ridiculous that you have to create features in JavaScript that are built into other languages. They use that to put down JavaScript and call it a terrible language. However, I think thats part of what makes JavaScript so great. JavaScript, though not as powerful in features as some other languages, offers enough flexibility and expressiveness that you can create what you feel that you are missing. Lets take a look at the problem and see how easy we can remedy it.</p>

<p>First, look at a basic function that is about the same in most C based languages:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span>arg1, arg2<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> arg2;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'one'</span>, <span style="color: #3366CC;">'two'</span><span style="color: #66cc66;">&#41;</span>; 	<span style="color: #009900; font-style: italic;">// returns 'two'</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'one'</span><span style="color: #66cc66;">&#41;</span>; 		<span style="color: #009900; font-style: italic;">// Error thrown</span></div></li></ol></pre>

<p>In the above example, you MUST have a second argument passed to the function, or you get an error. Some people remedy that like so:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span>arg1, arg2<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//check for defaults</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> arg1 = arg1 || <span style="color: #3366CC;">&quot;one&quot;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> arg2 = arg2 || <span style="color: #3366CC;">&quot;two&quot;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> arg2;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'one'</span>, <span style="color: #3366CC;">'also two'</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #009900; font-style: italic;">// returns 'also two'</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'one'</span><span style="color: #66cc66;">&#41;</span>; 			<span style="color: #009900; font-style: italic;">// returns 'two'</span></div></li></ol></pre>

<p>The above works ok, but is a little cumbersome. The logical or operator or '||' (double pipe) in JavaScript returns the first item if it is truthy, or the second item no matter what. Usually, you'll only see that in if statement blocks, but this idiom works well as an either or return value and is used such. <strong>NOTE: This idiom would not work well with any value passed that is falsey: A blank string, an integer 0, boolean false, etc., because it returns the second item when the first argument is a falsey value and not just undefined or null.</strong></p>

<p>The above fix is lousy, though, because it becomes a mess when you have more than one optional argument. Take the following example that uses an object literal as the only argument and allows for passing only the arguments you want to send:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//check for defaults</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	args.<span style="color: #006600;">arg1</span> = args.<span style="color: #006600;">arg1</span> || <span style="color: #3366CC;">&quot;one&quot;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	args.<span style="color: #006600;">arg2</span> = args.<span style="color: #006600;">arg2</span> || <span style="color: #3366CC;">&quot;two&quot;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	args.<span style="color: #006600;">arg3</span> = args.<span style="color: #006600;">arg3</span> || <span style="color: #3366CC;">&quot;three&quot;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> args.<span style="color: #006600;">arg2</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;arg2&quot;</span>:<span style="color: #3366CC;">&quot;also two&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #009900; font-style: italic;">// returns 'also two'</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;arg1&quot;</span>:<span style="color: #3366CC;">&quot;also one&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #009900; font-style: italic;">// returns 'two'</span></div></li></ol></pre>

<p>The above works a little better because it allows you to pass only the arguments you want without having to null out others. It still presents ugly default checking and the same issues with falsey statements. We can use the same idea, but implement it far better with a simple function that will take a set of defaults and replace passed items over it and return the a useable object with all defaults and arguments in one.</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> defaults<span style="color: #66cc66;">&#40;</span>def, arg<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">for</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> arg<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		def<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span> = arg<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> def;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>The above code takes exactly two arguments, the first being an object with the default 'name:value' pairs for your arguments. The second one is the argument object actually passed to the defaults function. The second object is iterated over and all of its values are placed over the default ones and the default object is returned. This will give a useable object with defaults in place and actual passed arguments where presented.</p>

<p>How it actually works can be displayed as such:</p>

<table class="inline-table">
	<thead>
		<tr>
			<th>Defaults Object</th>
			<th>Arguments Object</th>
			<th>Resulting Object</th>
		</tr>
	</thead>
	<tbody>
		<tr>
			<td>"one"</td>
			<td>(nothing passed)</td>
			<td>"one"</td>
		</tr>
		<tr>
			<td>"two"</td>
			<td>"also two"</td>
			<td>"also two"</td>
		</tr>	
		<tr>
			<td>"three"</td>
			<td>(nothing passed)</td>
			<td>"three"</td>
		</tr>
	</tbody>
</table>

<p>Lets see it in action:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #66cc66;">&#40;</span>args<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	args = defaults<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		arg1 :<span style="color: #3366CC;">&quot;one&quot;</span>,</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		arg2 :<span style="color: #3366CC;">&quot;two&quot;</span>,</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		arg3 :<span style="color: #3366CC;">&quot;three&quot;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>, args<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> args.<span style="color: #006600;">arg2</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;arg2&quot;</span>:<span style="color: #3366CC;">&quot;also two&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #009900; font-style: italic;">// returns 'also two'</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">foo<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#123;</span><span style="color: #3366CC;">&quot;arg1&quot;</span>:<span style="color: #3366CC;">&quot;also one&quot;</span><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #009900; font-style: italic;">// returns 'two'</span></div></li></ol></pre>

<p>The default arguments are much less cluttered and were properly applied to items missing from the passed args object. Armed with our new defaults function and knowledge of how to use a single object to send all arguments, we can now safely send only select arguments, falsey or not, and have default arguments take care of the rest.</p>

<p>Enjoy!</p> 
      ]]></content>
    </entry>

    <entry>
      <title>I Won Frank Peak&#8217;s Twitter Contest!</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/i_won_frank_peaks_twitter_contest/" />
      <id>tag:studiokoi.com,2009:blog/index/1.51</id>
      <published>2009-09-09T06:18:00Z</published>
      <updated>2009-09-09T10:17:13Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="News"
        scheme="http://studiokoi.com/code/category/News/"
        label="News" />
      <content type="html"><![CDATA[
        <p>If you like art at all and you don't already follow <a href="http://twitter.com/frankpeak" title="Frank Peak on Twitter">Frank Peak</a> on twitter, or visit <a href="http://frankpeak.com" title="Frank Peak's Online Portfolio">his website</a>, I would highly recommend it. He periodically has "Almost Daily Drawings" that are his random doodles and concept drawings. His <a href="http://frankpeak.com/portfolio.html" title="Portfolio - Frank Peak">portfolio</a> is also very, VERY good.</p>

<p>Frank is a very skilled illustrator and 3D artist who recently held a <a href="http://www.frankpeak.com/blog/?p=154" title="BUTTON PACK GIVE AWAY! - Frank Peak">contest</a> on Twitter for people who would retweet him. I was one of the fortunate winners of the contest.</p>

<p>Included in the package was a SWEET envelope that Frank personalized for each winner with random (and rather cute) artwork. Inside the package was a button pack with buttons by <a href="http://royalbuttons.com" title="Royal Buttons -  Custom Buttons Made to Order">Royal Buttons Custom Buttons</a>. There was also an unbent copy of the package art, and a sweet random drawing on nice card stock with a personal note from Frank, thanking the winner for participation.</p>

<p>Frank said he would also have some future contests, so even though this one is over, there could be some stuff up for grabs later.</p>

<div class="thumb-box">
<a href="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_1.jpg" title="Frank Peak Contest" class="thickbox thumb-left"><img src="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_1_t.jpg" /></a>
<a href="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_2.jpg" title="Frank Peak Contest" class="thickbox thumb-left"><img src="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_2_t.jpg" /></a>
<a href="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_3.jpg" title="Frank Peak Contest" class="thickbox thumb-left"><img src="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_3_t.jpg" /></a>
<a href="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_4.jpg" title="Frank Peak Contest" class="thickbox thumb-left"><img src="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_4_t.jpg" /></a>
<a href="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_5.jpg" title="Frank Peak Contest" class="thickbox thumb-left"><img src="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_5_t.jpg" /></a>
<a href="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_6.jpg" title="Frank Peak Contest" class="thickbox thumb-left"><img src="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_6_t.jpg" /></a>
<a href="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_7.jpg" title="Frank Peak Contest" class="thickbox thumb-left"><img src="http://codeblog.studiokoi.com/images/frankpeak/frank_peak_contest_7_t.jpg" /></a>
<div class="clear-left"></div>
</div>
 
      ]]></content>
    </entry>

    <entry>
      <title>Don&#8217;t miss the Appcelerator Titanium Launch Party</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/dont_miss_the_appcelerator_titanium_launch_party/" />
      <id>tag:studiokoi.com,2009:blog/index/1.50</id>
      <published>2009-06-05T19:40:00Z</published>
      <updated>2009-06-05T11:50:32Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="News"
        scheme="http://studiokoi.com/code/category/News/"
        label="News" />
      <content type="html"><![CDATA[
        <p>On Tuesday June 9th, the Appcelerator team will be throwing a product launch party at Apple’s World Wide Developer Conference to toast the arrival of Titanium Beta. If you are going to be at the WWDC head over to Jillian’s in San Francisco between 6 and 9 p.m. where they will unveil Titanium Beta.</p>

<p> I wish I could be there myself, but flying out to San Fran on a Tuesday is a tall order. Check it out for yourself and be sure to go if you are there. <a href="http://www.appcelerant.com/titanium-beta-launch-party-wwdc-june-9th-6-9pm.html" title="Celebrate the arrival of Titanium Beta">Celebrate the arrival of Titanium Beta</a> </p> 
      ]]></content>
    </entry>

    <entry>
      <title>Please update your RSS and ATOM feeds to the Code Blog</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/please_update_your_rss_and_atom_feeds_to_the_code_blog/" />
      <id>tag:studiokoi.com,2009:blog/index/1.48</id>
      <published>2009-04-20T06:06:00Z</published>
      <updated>2009-04-19T23:12:42Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="News"
        scheme="http://studiokoi.com/code/category/News/"
        label="News" />
      <content type="html"><![CDATA[
        <p>Please update your RSS and ATOM feeds to this site. I recently discovered a small error with my back-end code that was allowing erroneous links to appear for my RSS and ATOM feeds. These bad links will work until the end of the summer at which time I will remove them and only be updating the proper links.</p>

<p>
<a href="http://codeblog.studiokoi.com/share/code/atom/" title="Studio Koi Code Blog Atom feed">Update your ATOM feed to the Code Blog.</a><br/>
<a href="http://codeblog.studiokoi.com/share/code/rss_2.0/" title="Studio Koi Code Blog RSS feed">Update your RSS feed to the Code Blog.</a><br/>
</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Adobe AIR App: JSLint AIR</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/adobe_air_app_jslint_air/" />
      <id>tag:studiokoi.com,2009:blog/index/1.46</id>
      <published>2009-04-16T07:33:00Z</published>
      <updated>2009-04-19T19:20:29Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="AIR"
        scheme="http://studiokoi.com/code/category/air/"
        label="AIR" />
      <content type="html"><![CDATA[
        <p>In attempts to make my JavaScript applications better, I frequently use <a href="http://www.crockford.com/" title="Douglas Crockford's Wrrrld Wide Web">Douglas Crockford</a>'s JavaScript verifier application <a href="http://www.jslint.com/" title="JSLint: The JavaScript Verifier">JSLint</a>. If you have never used JSLint, I would advise reviewing <a href="http://www.JSLint.com/lint.html" title="What is JSLint?">the background on JSLint</a></p>

<p>In order to use it more easily and efficiently on local files, I took the JSLint JS file and ported it to an Adobe AIR application for the desktop. You can import the file from your desktop or paste the code in directly. In a future edition, I will add drag and drop ability to JS files. I used <a href="http://jqueryui.com/">jQuery UI</a> for the app UI and plan to eventually add skin choices to the options menu base on jQuery UI default skins. The app will auto update, so you will be able to see new features as soon as I add them if you accept the updates. </p>

<p>Check out the <a href="http://software.studiokoi.com/applications/jslintair/preview_jslintair.jpg" class="thickbox">Interface Preview</a>, and give the app a try. Please leave all comments and bug issues as comments in this thread. Enjoy!</p>

<div class="flashHolder">
<div id="flash_1_jslintair"> 
	<script type="text/javascript">
		_DEFER.flash['flash_1_jslintair'] = {
			"swfurl":"/images/AIRInstallBadge.swf",
			"width" : "215",
			"height": "180",
			"params":{
                                "wmode": "transparent"
                        }, 
			"vars":{
				"appname"	: "JSLint AIR",
				"appid"		: "JSLintAIR",
				"appurl"	: "http://software.studiokoi.com/applications/jslintair/jslint.air",
				"airversion": "1.5",
				"image"		: "http://software.studiokoi.com/applications/jslintair/badge_jslintair.jpg"
			},
			"attr": {}
		};
	</script>
</div>
</div> 
      ]]></content>
    </entry>

    <entry>
      <title>Execution order of functions and variables in JavaScript and ActionScript.</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/execution_order_of_functions_and_variables_in_javascript_and_actionscript/" />
      <id>tag:studiokoi.com,2009:blog/index/1.45</id>
      <published>2009-04-13T07:17:00Z</published>
      <updated>2009-04-13T08:03:55Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="JavaScript"
        scheme="http://studiokoi.com/code/category/JavaScript/"
        label="JavaScript" />
      <content type="html"><![CDATA[
        <p>Many newcomers to web development may not be used to how web implementations of JavaScript execute. Namely, in what <em>order</em> it executes. This can be the source of many errors that are difficult to pinpoint if you aren't experienced with it. I think this also affects people who are very experienced with other programming languages and fosters a lot of undeserved hate towards ActionScript and JavaScript. (It's important to note that not all implementations of JavaScript are the same, and that some may in fact defer from what is said below. This article concerns browser and Flash based ECMAScript.)</p>

<p>The thing to understand about JavaScript is that it isn't compiled for the most part. (This is not <em>exactly</em> true for ActionScript because of the introduction of classes in ActionScript 2.0, however, the runtime script remains the same. You might argue that it compiles into a *.swf file, but in reality, that is the combination of everything that has to do with the Flash file. The ActionScript still remains live.) It's good that it isn't compiled though, because this allows for much more dynamic, expressive coding.</p>

<p>So, if it isn't compiled, how does it work? Browser-based JavaScript code is all executed at first calling, nothing is compiled or cached in a scope until it is called (except AS2 &amp; AS3 classes). It works this way because, with the exception of basic, immutable variable types: strings, booleans, numbers, and 'undefined', every variable in JavaScript is an object. This includes 'null' and functions ('null' is technically a basic variable, but is reported as an object in JavaScript). Since functions are objects, this means their value in memory is also like an object and names are only assigned by reference. Lets see how this works:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//bob first initialization</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> bob<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bob'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//set jan to bob via reference</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> jan = bob;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//set bob to another function</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> bob<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'newbob'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">jan<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//alerts 'bob'</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bob<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//alerts 'newbob'</span></div></li></ol></pre>

<p>This where things can be very confusing to people who are used to other languages where functions are <em>just</em> functions and can only be written once to a variable name at compile time. In order to understand better, let's write the same code again with anonymous functions:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//bob first initialization</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> bob = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'bob'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//set jan to bob via reference</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> jan = bob;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//set bob to another function</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bob = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'newbob'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">jan<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//alerts 'bob'</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">bob<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//alerts 'newbob'</span></div></li></ol></pre>

<p>This is how functions are actually assigned in JavaScript. The traditional C style that was seen in the previous example is translated at runtime into something like the above. So, when you code JavaScript, imagine instead that all functions are really variable pointers to anonymous functions in memory. (ActionScript 3.0 in strict mode attempts to stop this sort of behavior by warning you when your code tries to overwrite variables that have already been set. While this is good for heavier languages, I think it is moving away from what makes ECMAScript really dynamic in the first place.)</p>

<p>At this point, you might be asking: "Why is all this important to the execution order of JavaScript?". This gives you background as to why JavaScript doesn't execute anything until runtime. In light of this, there is one thing JavaScript does to speed things up that is VERY important to understanding its execution order: JavaScript caches declared functions <em>before</em> any other variables, after this, it goes back to the top of the scope and runs variable definitions and functions calls in the order that they appear. This is where the confusion begins for the uninitiated.</p>

<p>Let's say for example that you have a function that returns a value from a passed variable. You might want to assign a variable to the result of that function like so:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> newNumber = secondPower<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//25</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//first definition of function</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> secondPower<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> n * n;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>This works because the function was cached before the variable was assigned. However, doing things this way in your own code is a <strong>TERRIBLE</strong> idea because it can cause more problems than it solves. Lets look at an example of where this might fail. Remember our previous example where we assigned the anonymous function to a variable name? While in that example, it worked perfectly, it actually causes a strange thing to happen to execution order:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> newNumber = secondPower<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//error, secondPower is not defined yet</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//some parsers would not even get this far due to the above error</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//thrown errors in ECMAScript halt the execution scope they are in</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> secondPower = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> n * n;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> newNumber2 = secondPower<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//25</span></div></li></ol></pre>

<p>This error happens because when the parser came through, it cached the anonymous function before anything else, but <em>not</em> the variable name assignment to the anonymous function. Therefore, when we called 'secondPower', it had not been assigned yet and the first calling was undefined and possibly threw an error. It is important to remember that the bad thing that happened here was not the fact that we assigned a variable name to an anonymous function in place of the classic C way to declare a function, but rather the bad idiom of calling a function before it is assigned. Anonymous functions offer huge benefits to ECMAScript and are not inherently bad.</p>

<p>Other problems can arise from calling functions before they are declared even if the function is written in the classic style. Perhaps the function we use for variable assignment requires an outside variable to function properly. This would cause an error if the function was called before the assignment of the dependent variable took place:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> newNumber = multiplyNumber<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//multiple is not defined yet, incorrect result with possible error </span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> multiple = <span style="color: #CC0000;">2</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//this is cached and available before all other variables</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> multiplyNumber<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> n * multiple;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>As we can see here, an error would occur in our variable assignment because even though the function was properly cached, its dependent was not assigned yet and the function could not execute properly.</p>

<p>So, knowing this, what should you do to avoid these errors? Here is a quick list to help you when writing your code:
	<ul>
		<li>Never call a function before it is declared</li>
		<li>Declare all variables in a execution scope as soon as possible</li>
		<li>Only assign variables to a function result after all functions and other variables have been declared</li>
	</ul>
</p>

<p>Just to reiterate, let's look at how a good code setup should be writen:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//declare all variables as soon as possible</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> newNumber, multiple = <span style="color: #CC0000;">2</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//declare all functions before calling them</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> multiplyNumber<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> n * multiple;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//finally, set variable values that require function results</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">newNumber = multiplyNumber<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//10</span></div></li></ol></pre>

<p>Declaring your variables and functions in this way will allow you to use more expressive methods to set functions and will most importantly avoid errors. For some further reading, check out <a href="http://javascript.crockford.com/code.html" title="Code Conventions for the JavaScript Programming Language">Douglas Crockford's recommendations on JavaScript coding conventions</a>.</p>

<p>While I mostly mentioned JavaScript in this Article, the majority of these principles and Douglas Crockford's suggestions apply to ActionScript as well.</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Type&#45;checking arrays in JavaScript</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/typechecking_arrays_in_javascript/" />
      <id>tag:studiokoi.com,2009:blog/index/1.44</id>
      <published>2009-01-10T08:06:00Z</published>
      <updated>2009-01-26T15:43:54Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="JavaScript"
        scheme="http://studiokoi.com/code/category/JavaScript/"
        label="JavaScript" />
      <content type="html"><![CDATA[
        <p>In JavaScript the typeof operator reports an array as 'object'. While this is technically correct (an array in JavaScript is really just a specialized object), it's a pain in the neck when you are type checking a variable.</p>

<p>There are a number of ways to identify an array in JavaScript. Checking for properties of an array isn't totally accurate because you can add any named member to an object to imitate an array. Checking for 'instanceof Array' works most of the time but could be tricky in some implementations of JavaScript.</p>

<p>Mark Miller of Google gives us a solution that is standard to the ECMAScript documentation:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">Object.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">toString</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>value<span style="color: #66cc66;">&#41;</span> === <span style="color: #3366CC;">'[object Array]'</span>;</div></li></ol></pre>

<p>What this essentially does is take the standard Object method "toString" and applies it to the passed argument (which is your array you are checking). This makes sure that it reports itself according to standard, which all real arrays should. We can implement it in an easy checking function that returns a boolean:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> isArray<span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> Object.<span style="color: #006600;">prototype</span>.<span style="color: #006600;">toString</span>.<span style="color: #006600;">apply</span><span style="color: #66cc66;">&#40;</span>a<span style="color: #66cc66;">&#41;</span> === <span style="color: #3366CC;">'[object Array]'</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>Props to Douglas Crockford because I read about this on his blog. There is also some <a href="http://blog.360.yahoo.com/blog-TBPekxc1dLNy5DOloPfzVvFIVOWMB0li?p=916#comments" title="Douglas Crockford | Yahoo 360">further discussion</a> there on using this method and i recommend reading it.</p>

<p>As a bonus of sorts, this also works in ActionScript 2.0 and ActionScript 3.0.</p> 
      ]]></content>
    </entry>

    <entry>
      <title>JavaScript Errors: Testing for the existence of functions in JavaScript</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/javascript_errors_testing_for_the_existence_of_functions_in_javascript/" />
      <id>tag:studiokoi.com,2008:blog/index/1.43</id>
      <published>2008-12-29T22:38:00Z</published>
      <updated>2008-12-29T12:52:20Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="JavaScript"
        scheme="http://studiokoi.com/code/category/JavaScript/"
        label="JavaScript" />
      <content type="html"><![CDATA[
        <p>When using multiple JavaScript libraries, it is inevitable that one JavaScript file will need to call functions named in other libraries. This is necessary but can cause issues if all of the libraries aren't present.</p>

<p>Error handling with JavaScript is somewhat lackluster. With this in mind, you should do your best to catch and handle errors and avoid sending them to the end-user. In order to prevent errors and allow for scalability, you should test for functions that aren't present in the script where you are calling them.</p>

<p>Lets say for example that you have downloaded a JavaScript library named "bob.js" and it contains just a main function named "bob". To keep things clean, you might load this file separately. (Of course this is a bit unrealistic, no one would make a separate file for one function, but for the sake of example, bear with me.) Your loading code might look like this:</p>


<pre class="xml"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;bob.js&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/script<span style="font-weight: bold; color: black;">&gt;</span></span></span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;script</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;mycode.js&quot;</span><span style="font-weight: bold; color: black;">&gt;</span></span><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/script<span style="font-weight: bold; color: black;">&gt;</span></span></span></div></li></ol></pre>

<p>So, correctly, we have loaded the "bob" library first and our code second.</p>

<p>For the sake of example and understanding, lets suppose that the "bob" function just returns a simple math result. It isn't important what the bob function does, but it might be easier for some to understand if we had a real example:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//bob.js</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> bob<span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> n * n;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>Now this takes us to our real example. Given that we have the function "bob" in another file, we want to use it in our own code, "mycode.js". When the page loads, we want to add a div tag with some text that has a number in it. We are going to use the "bob" function to return the number to the power of 2 and write it:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//mycode.js</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #000066;">onload</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//the starting number</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> oldNum = <span style="color: #CC0000;">43</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//make the number we are going to write</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> newNum = bob<span style="color: #66cc66;">&#40;</span>oldNum<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//set the body tag to a variable</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> body = document.<span style="color: #006600;">getElementsByTagName</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//make the div container</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> myDiv = document.<span style="color: #006600;">createElement</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//create the text container</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> myText = document.<span style="color: #006600;">createTextNode</span><span style="color: #66cc66;">&#40;</span>newNum<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//add the text to the div</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	myDiv.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>myText<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//add the div to the body</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	body.<span style="color: #006600;">appendChild</span><span style="color: #66cc66;">&#40;</span>myDiv<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>While the above code could be written quite a bit smaller, it works well and it is plain what is happening. You run into an issue however when the "bob.js" file isnt present. When you run into an error in JavaScript, it usually kills the chain the function or event is called in. So in the above instance, as soon as it hit the second variable, and the bob code wasn't present, the code would simply halt on error and nothing would display on screen, potentially breaking many other things in the process.</p>

<p>This brings the issue of error handling and function testing. One of the tools that we do have in JavaScript is the try/catch(/finally) statement. It is very handy for areas of code that can be error prone or have outside influence on the application. We could fix our example by adding a try/catch statement that would have a backup variable in case the "bob.js" isn't present:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//mycode.js</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #000066;">onload</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//the starting number</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> oldNum = <span style="color: #CC0000;">43</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//make the number we are going to write</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">try</span> <span style="color: #66cc66;">&#123;</span>	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900; font-style: italic;">//works if bob is present</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #003366; font-weight: bold;">var</span> newNum = bob<span style="color: #66cc66;">&#40;</span>oldNum<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span><span style="color: #66cc66;">&#40;</span>e<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900; font-style: italic;">//catches the error if bob is missing and provides an alternative</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #003366; font-weight: bold;">var</span> newNum = oldNum;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//..etc</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>This will effectively remove the chance for error if "bob.js" isn't present. However, the try/catch statement takes a bit of a performance hit due to all it is designed to do. When simply testing for functions, I feel it is a bit verbose.</p>

<p>My personal preference is just to test for the existence of the function before I attempt to call it. This way you don't spend the CPU time calling the function, getting the failure, sending the error, catching the error, then running the error block. When testing for a function, it's usually best to make sure it is actually a function rather than simply checking for it to be not undefined. This is doubly important in situations where you are using multiple libraries or working with a lot of people on the same file. Your code and their code could have the same name for two different types of objects. JavaScript has no overwrite protection even on its own built in functions unless specific platforms add it in, therefor you must take it upon yourself to check.</p>

<p>Checking for the existence of functions can be done easily using the typeof operator in JavaScript. Many times, I code that simply checks for "undefined" or not, then lets the code run. As mentioned above, this still leaves some potential for errors in your code. Granted, there will always be margin for error, but the goal is to make habits that minimize it and help you the real issues. Therefore I think it is better to make sure your function is actually a function. The same code we used with the try/catch statement can be written similarly with the typeof checking:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//mycode.js</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #000066;">onload</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//the starting number</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> oldNum = <span style="color: #CC0000;">43</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//make the number we are going to write</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> bob === <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900; font-style: italic;">//works if bob is present and is a function</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #003366; font-weight: bold;">var</span> newNum = bob<span style="color: #66cc66;">&#40;</span>oldNum<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900; font-style: italic;">//if bob is not present, provides an alternative</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #003366; font-weight: bold;">var</span> newNum = oldNum;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//..etc</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>This has better performance than the try/catch statement and gives you a better idea where an error could be if it occurs. However, we could write this even shorter to make the code a little cleaner using a ternary operator:</p>

<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//mycode.js</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #000066;">onload</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//the starting number</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> oldNum = <span style="color: #CC0000;">43</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//make the number we are going to write</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//if bob is present and is a function, make number</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//if bob is not present, provides an alternative</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> newNum = <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> bob === <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #66cc66;">&#41;</span>? bob<span style="color: #66cc66;">&#40;</span>oldNum<span style="color: #66cc66;">&#41;</span> : oldNum;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//..etc</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>One last thing to mention when using typeof. It will throw an error if you are testing for a function inside of a nonexistent parent. So, say "bob" is an object, with a subobject "math", with a method called "double". If we test for "bob.math.double" and "bob" doesn't exist, JavaScript will throw an error with the typeof operator. To avoid such errors you could test for each segment of the method's parent chain before testing for the method itself:</p>
<pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #009900; font-style: italic;">//mycode.js</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">window.<span style="color: #000066;">onload</span> = <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//the starting number</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> oldNum = <span style="color: #CC0000;">43</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//make the number we are going to write</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">if</span><span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> bob === <span style="color: #3366CC;">&quot;object&quot;</span> &amp;&amp; </div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #000066; font-weight: bold;">typeof</span> bob.<span style="color: #006600;">math</span> === <span style="color: #3366CC;">&quot;object&quot;</span> &amp;&amp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	   <span style="color: #000066; font-weight: bold;">typeof</span> bob.<span style="color: #006600;">math</span>.<span style="color: #006600;">double</span> === <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900; font-style: italic;">//works if bob is present and is a function</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #003366; font-weight: bold;">var</span> newNum = bob.<span style="color: #006600;">math</span>.<span style="color: #006600;">double</span><span style="color: #66cc66;">&#40;</span>oldNum<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #009900; font-style: italic;">//if bob is not present, provides an alternative</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #003366; font-weight: bold;">var</span> newNum = oldNum;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//..etc</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>This seems a lot longer given the possibilities of nested objects, but doing it this way also prevents the issue of same-named objects clobbering each other and sneaking in to create errors. If you are really confident with your code and have no outside workers/code coming in, you could skip the middle statements and just test for the parent object.</p>

<p>There we have it. This method of function existence checking has saved me lots of time and has created some interesting idioms for projects where external code may or may not be present. Enjoy!</p> 
      ]]></content>
    </entry>

    <entry>
      <title>All comments on the Code Blog now require moderator approval</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/all_comments_on_the_code_blog_now_require_moderater_approval/" />
      <id>tag:studiokoi.com,2008:blog/index/1.42</id>
      <published>2008-12-11T02:46:00Z</published>
      <updated>2008-12-10T16:51:18Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="News"
        scheme="http://studiokoi.com/code/category/News/"
        label="News" />
      <content type="html"><![CDATA[
        <p>Unfortunately, the griefers of The Internet, comment spammers, have made it necessary to take the precaution of requiring all comments made on this site to be moderated. This means that any comments posts you see will not be imeadiately visible until moderator approves it. I apologize for the inconvenience, but the spam just isnt good for anyone, and this is the only way I can keep up with it.</p>

<p>On a side note: I realize I haven't posted in a while, but a lot of things have been happening in my home life recently. However, I do have some posts in the pipeline, so subscribe to the RSS feed and keep checking back. Thanks to all the readers for your support.</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Making Anonymous Functions and Closures Work in ActionScript 2.0</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/making_anonymous_functions_and_closures_work_in_actionscript_20/" />
      <id>tag:studiokoi.com,2008:blog/index/1.39</id>
      <published>2008-08-26T05:35:01Z</published>
      <updated>2008-08-25T22:12:23Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://studiokoi.com/code/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>One of the most powerful idioms available in JavaScript is the anonymous function closure: e.g. (function(){})();. If you are unfamiliar with this, it simply creates an anonymous scope bubble that can be used to prevent automatic global variables, or trick JavaScript into allowing private variables. Unfortunately, this idiom isnt available in ActionScript 2.0. It simply doesnt work. (As of ActionScript 3.0, it works perfectly.)</p>
 
<p>This means that, on the surface, the only way to prevent time line variables from being constantly created is an init function of sorts. (ActionScript 2.0 has a pseudo global object which is the main time line of a movie level, and a totally global object named _global.) This also means that private variables are restricted to the creation of object classes. I personally get annoyed at times with classes in ActionScript. One of the things that has always bugged me with ActionScript 2.0 and the introduction of classes is that they are hard to make portable. (I work on no less than 3 computers in a single week, and 4-5 when on business trips, so portability is paramount to my work.) Rather than being able to simply include the classes in the same folder as the flash working file (*.fla), you must specify where on the computer the includes are. This is just annoying if you work on the file on several different computers. There are a couple of ways to avoid naming the class include folder on each computer, but they are as much of a pain as the default method. Fortunately, there is a solution.</p>

<p>To reiterate, while this works in JavaScript and ActionScript 3.0, it will not in ActionScript 2.0:
</p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//statements</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> myFunc = <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> hiddenVar = <span style="color: #ff0000;">&quot;hidden&quot;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> hiddenVar;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">hiddenVar		<span style="color: #808080; font-style: italic;">//undefined</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myFunc.<span style="color: #006600;">hiddenVar</span>; 	<span style="color: #808080; font-style: italic;">//undefined</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">myFunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 		<span style="color: #808080; font-style: italic;">//returns &quot;hidden&quot;</span></div></li></ol></pre>

<p>The good news about ActionScript 2.0 is that it retains the ability to pass anonymous functions as variables to another function. Like so:
</p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> myArray = <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #cc66cc;">15</span><span style="color: #66cc66;">&#93;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> sortedArray = myArray.<span style="color: #0066CC;">sort</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> a - b;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>sortedArray<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//returns [1, 5, 6, 15, 23]</span></div></li></ol></pre>

<p>Knowing this, we can emulate anonymous function closures in ActionScipt 2.0. First we will create a global function that returns the function passed to it.</p>
<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">_global</span>.<span style="color: #006600;">closure</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> f;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>To use it, you treat it the same as you would the parenteses in the javascript idiom:
</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">closure<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//statements</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>

<p>Changing the name to something shorter will make this more practical and the code a little cleaner.</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">_global</span>.$ = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>f<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> f;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">$<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//statements</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre>

<p>Given this new ability, we can now apply anonymous closures to our script without littering the time line object with useless variables.</p> 

<p>Lets use a hypothetical situation to illustrate how we might use the anonymous closure. In this script, we want to make 5 buttons that each need to trace their respective number when you click them. We could code each button individually, but that is messy and doesn't promote good code reuse. Instead, lets make a loop that makes all the buttons work, in addition to making it easier to add more buttons.</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i = <span style="color: #cc66cc;">1</span>; i &lt;= <span style="color: #cc66cc;">5</span>; i++<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//for each button</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0066CC;">this</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">&quot;click&quot;</span> + i + <span style="color: #ff0000;">&quot;_btn&quot;</span><span style="color: #66cc66;">&#93;</span>.<span style="color: #0066CC;">onPress</span> = $<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//return a function that has access to n via</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">// a closure</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #808080; font-style: italic;">//trace the number passed to the parent closure</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>n<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span>i<span style="color: #66cc66;">&#41;</span>; <span style="color: #808080; font-style: italic;">//pass in current loop iteration for caching</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre>

<p>Now when you press click3_btn, it will trace '3' and click5_btn, '5'. In this way, we avoid creating useless variables inside each button, and we didn't have to create a singular use function.</p>

<p>Earlier I mentioned my slight distaste for how classes were implemented in ActionScript 2.0. Let me first caveat this statement by saying they are extremely useful and are wonderful for individuals or teams that have good version networks and servers. Just for myself and the smaller applications I write, I prefer portable objects. Lets see how we would write a singleton object with private variables and functions:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> bob = $<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//private variables</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> secret = <span style="color: #ff0000;">&quot;you found me&quot;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//private functions</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">function</span> compare<span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> a &gt; b;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//constructor</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> Bob = <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//public members/variables</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		works		:<span style="color: #000000; font-weight: bold;">true</span>,</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		getSecret	:<span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">return</span> secret;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span>,</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		getCompare	:<span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">return</span> compare<span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//return object</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> Bob;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//test</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">typeof</span> bob<span style="color: #66cc66;">&#41;</span> 		<span style="color: #808080; font-style: italic;">//returns object</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">works</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns true</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">getSecret</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns the string: you found me</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">secret</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns undefined</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">getCompare</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns false</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>bob.<span style="color: #006600;">compare</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns undefined function</span></div></li></ol></pre>

<p>As you can see with the tests, we have a singleton object with private functions and variables as well as public methods and members. With a little work, using the prototypal inheritance method, we can make this a reusable pseudo class:</p>

<pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> Bob = $<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//private variables</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> secret = <span style="color: #ff0000;">&quot;you found me&quot;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//private functions</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">function</span> compare<span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> a &gt; b;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//constructor</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> Bob = <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #808080; font-style: italic;">//public members/variables</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #0066CC;">this</span>.<span style="color: #006600;">works</span> = <span style="color: #000000; font-weight: bold;">true</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//public methods</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Bob.<span style="color: #0066CC;">prototype</span>.<span style="color: #006600;">getSecret</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> secret;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	Bob.<span style="color: #0066CC;">prototype</span>.<span style="color: #006600;">getCompare</span> = <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">return</span> compare<span style="color: #66cc66;">&#40;</span>a, b<span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//---------------------------</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #808080; font-style: italic;">//return constructor</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> Bob;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//invoke</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> mybob = <span style="color: #000000; font-weight: bold;">new</span> Bob<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #808080; font-style: italic;">//test</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">typeof</span> Bob<span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns function</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">typeof</span> mybob<span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns object	</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">works</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns true</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">getSecret</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns the string: you found me</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">secret</span><span style="color: #66cc66;">&#41;</span>;		<span style="color: #808080; font-style: italic;">//returns undefined</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">getCompare</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns false</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>mybob.<span style="color: #006600;">compare</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;	<span style="color: #808080; font-style: italic;">//returns undefined</span></div></li></ol></pre>

<p>So, with a single global function, we can bring the magic of anonymous function closures to ActionScript 2.0. Enjoy!</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Parsing a nested or name spaced object from a string in ECMAScript</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/parsing_a_nested_or_name_spaced_object_from_a_string_in_ecmascript/" />
      <id>tag:studiokoi.com,2008:blog/index/1.23</id>
      <published>2008-01-10T06:37:00Z</published>
      <updated>2008-01-09T20:39:03Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="JavaScript"
        scheme="http://studiokoi.com/code/category/JavaScript/"
        label="JavaScript" />
      <content type="html"><![CDATA[
        <p>Often times when I am making dynamic scripts in Flash or JavaScript, I want to call an object method using a string. It may be a situation where I am stuck using <a href="http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary372.html" title="ActionScript Dictionary: fscommand">fscommand</a> (due to a customer request of a Flash version &lt;8) or just writing script with script.</p>

<p>The problem that comes with this method is the advent of ActionScript 3 with Flash CS3/Flex 2 and the rising use in namespaces by JavaScript developers (myself included). Both encour a heavy usage of object methods instead of one-off functions. Dynamically calling singleton functions is easy, you can just use subscript notation, e.g. object[&#8216;member/method&#8217;]. What you can&#8217;t do with this method is pass it any dot syntax and expect it to work e.g. object[&#8216;subobject.method&#8217;]. The subscript notation is only meant to work one level. So the previous example won&#8217;t work. In order to fix this issue and to make sending object methods/members in a single string a little easier, I wrote a function that takes a string and a base object (the default is the window object if no object reference is passed) and returns the method/member. (I know at this point that some will simply direct me to ECMAScript&#8217;s built in eval() function, but I personally avoid using it at all costs as it can have unintended, and unsecure results. I&#8217;ll let <a href="http://javascript.about.com/library/bleval.htm" title="About.com: Alternatives to eval">Stephen Chapmen tell you</a> because this article is about something else.)
</p>

<p>The function is executed somewhat simply by splitting the string at the dots and adding them as subscripts to one another in order. However, there are some other pieces to it that make it a little more friendly to mistakes and general usage.</p>

<p><pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">function</span> parseNameSpace<span style="color: #66cc66;">&#40;</span>s , o<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//default to window if no object sent</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #003366; font-weight: bold;">var</span> w = o || window, a;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//reutrn if parsing not necessary or not a method</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span> s.<span style="color: #006600;">indexOf</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.'</span><span style="color: #66cc66;">&#41;</span> == <span style="color: #CC0000;">-1</span> <span style="color: #66cc66;">&#41;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> w<span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span> === <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #003366; font-weight: bold;">false</span> : w<span style="color: #66cc66;">&#91;</span>s<span style="color: #66cc66;">&#93;</span> ;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//make array for looping through</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	a = s.<span style="color: #006600;">split</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">'.'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//run though members / methods</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i = <span style="color: #CC0000;">0</span>,l = a.<span style="color: #006600;">length</span>; i &lt; l; i++<span style="color: #66cc66;">&#41;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		w = w<span style="color: #66cc66;">&#91;</span>a<span style="color: #66cc66;">&#91;</span>i<span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #009900; font-style: italic;">//return object if it is valid, or return false</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> w === <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #66cc66;">&#41;</span> ? <span style="color: #003366; font-weight: bold;">false</span> : w ;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre></p>

<p>The function takes two variables, first the string that you want to parse to an object member, and the second optional base object. The first variable w looks for the object &#8220;o&#8221; and if not found, defaults to &#8220;window&#8221; which is the default parent object of browser based JavaScript. (You could change it to &#8220;this&#8221; and it would work in ActionScript as well). Second, we check to see if the passed string has any dots in it. If not, we check to see if it is a regular function. This way you could use this function as a catch all even if you aren&#8217;t always passing nested functions. If it isnt a member of the window object, false is returned. If it does have dots, we split them into an array, and loop through it, making each a member of the previous using the subscript notation. Finally we check to see if the result is valid, and we return false if it isn&#8217;t, or a reference to the object itself.</p>

<p>Let&#8217;s take a look at a simple way to use it. Lets say we have an object literal with a subobject, and a method inside that.</p>

<p><pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> obj = <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	innerObj: <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		func: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;worked&quot;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre></p>

<p>Then we retrieve it via string with the function.</p>

<p><pre class="javascript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #003366; font-weight: bold;">var</span> objFunc = parseNameSpace<span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;obj.innerObj.func&quot;</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000066;">alert</span><span style="color: #66cc66;">&#40;</span>objFunc<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>; <span style="color: #009900; font-style: italic;">//shows &quot;worked&quot;</span></div></li></ol></pre></p>

<p>This should work most places as long as you are mindful of function scope. Enjoy!</p> 
      ]]></content>
    </entry>

    <entry>
      <title>Create Right Click Menus to Test AIR Files (Win/Mac)</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/create_right_click_menus_to_test_air_files_win_mac/" />
      <id>tag:studiokoi.com,2007:blog/index/1.21</id>
      <published>2007-08-22T09:18:02Z</published>
      <updated>2007-08-22T07:24:04Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="AIR"
        scheme="http://studiokoi.com/code/category/air/"
        label="AIR" />
      <content type="html"><![CDATA[
        <p>At the beginning of this year, when I first heard about <a href="http://labs.adobe.com/technologies/air/" title="Adobe Integrated Runtime">A.I.R.</a> I was excited to learn more about it and see what I could do with it. What really jumpstarted my interest was a trip to Cincinnati, OH to the <a href="http://onair.adobe.com/" title="Adobe onAIR Bus Tour">Adobe onAIR Bus Tour</a>. At this point I am full steam excited about working with AIR.</p>

<p>I work on a Windows machine at work and Mac at home, so I am also excited to see cross platform development. One thing that is a little wonky about AIR is the way that you test your applications. Since AIR is built with an SDK instead of a builder application, such as Flash, you have you use the terminal to launch the SDK. So I came up with methods for OS X and Windows XP to use right-click contextual menus to test AIR applications. I will first note that you can use <a href="http://aptana.com" title="Aptana">Aptana</a>, <a href="http://labs.adobe.com/technologies/flex/" title="Flex 3 Beta &quot;Moxie&quot;">Flex 3</a>, <a href="http://labs.adobe.com/wiki/index.php/AIR:Flash_CS3_Professional_Update#Download_and_Install" title="Flash CS3 AIR Plug-in">Flash CS3</a>, and <a href="http://labs.adobe.com/wiki/index.php/AIR:Dreamweaver_CS3_Extension" title="Dreamweaver CS3 AIR Plug-in">Dreamweaver CS3</a> to test your AIR apps, but i thought i would make something for the hardcore coders. Though, I personally use all the aforementioned for AIR.</p>

<p>I will assume you have already installed the <a href="http://labs.adobe.com/downloads/airsdk.html" title="Adobe AIR SDK">SDK</a> on either system.</p>

<p>Windows XP: <a href="#mac">[skip to mac version]</a><hr /></p>

<p>In Windows XP, you can customize your right-click menus for any filetype with the 'folder options' panel. First, navigate to control panel, then folder options. Navigate to XML (It will be close to the bottom.).</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_slct_avd.gif" alt="image" width="386" height="475" /></p>

<p>If the button says 'Restore' where you expect it to say 'Advanced', it is because you have changed the Windows default opening program for the xml file type. Click 'Restore' first then you can click 'Advanced'. (When you are finished, you can change the opening program back when you are finished, the menu will still stick.)</p>

<p>Next, click the 'edit' button and we will create a new contextual command. You will need the full path of the location of your AIR SDK. For this example, I will use mine:</p>

<code>C:\Program Files\Adobe\AIR SDK\bin\adl.exe</code>

<p>This will application field in quotes, followed by "%1". This will notate that you are opening the file right-clicked with this program. (This works on most other windows programs as well.) Then give it a name you can remember. This is how it will show up in the contextual menu. Just replace my AIR bin location with yours.</p>

<code>"C:\Program Files\Adobe\AIR SDK\bin\adl.exe" "%1"</code>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_code.gif" alt="image" width="347" height="361" /></p>

<p>Now click OK and you should see your command in the window:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_fin.gif" alt="image" width="347" height="318" /></p>

<p>Now you can right-click your AIR application.xml and test them right from the Windows file explorer:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_rtclk.gif" alt="image" width="374" height="328" /></p>

<p>The CMD prompt will open, then your air file should open. The CMD prompt will be your error output window.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/xp_func_adl_run.gif" alt="image" width="550" height="394" /></p>

<p>&nbsp;</p>
<p><a name="mac">OS X:</a><hr /></p>

<p>The Mac version will be somewhat similar, but we will use Automator to do the function. This means you will need at least OS X 10.4 for this tutorial.</p>

<p>Open Automator, and go to 'Finder' in the Library menu, under Applications. Drag 'Get Selected Finder Items' to the work space.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_get.gif" alt="image" width="540" height="104" /></p>

<p>Next, go to 'Automator' in the Library menu, and drag 'Run AppleScript' to the work space under 'Get Selected Finder Items'.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_aplscr.gif" alt="image" width="540" height="377" /></p>

<p>For the Applescript, I had a little help from <a href="http://www.macosxhints.com/article.php?story=20050827164648766" title="MacOSXHints.com | Use Automator to open chosen folder in Terminal">Mac OS X Hints</a>. You will need the directory for your Adobe AIR SDK. Mine is:</p>

<code>/Applications/Adobe AIR SDK/bin/adl</code>

<p>For this one, I will give the code to you. Paste this into the Applescript window and replace my AIR Adl location with yours:</p>

<pre class="applescript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">on</span> <span style="color: #000066;">run</span> <span style="color: #66cc66;">&#123;</span>input, parameters<span style="color: #66cc66;">&#125;</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">tell</span> application <span style="color: #ff0000;">&quot;Terminal&quot;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		activate</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">the</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000066;">count</span> <span style="color: #b1b100;">of</span> <span style="color: #000000; font-weight: bold;">the</span> window<span style="color: #66cc66;">&#41;</span> = <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span> <span style="color: #000066;">or</span> ¬</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">the</span> busy <span style="color: #b1b100;">of</span> window <span style="color: #cc66cc;">1</span> = <span style="color: #000066;">true</span><span style="color: #66cc66;">&#41;</span> <span style="color: #b1b100;">then</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">tell</span> application <span style="color: #ff0000;">&quot;System Events&quot;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">				keystroke <span style="color: #ff0000;">&quot;n&quot;</span> using command down</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">tell</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">if</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">		do <span style="color: #b1b100;">script</span> <span style="color: #ff0000;">&quot;'/Applications/Adobe AIR SDK/bin/adl' <span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> &amp; <span style="color: #66cc66;">&#40;</span>POSIX path <span style="color: #b1b100;">of</span> ¬</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">			<span style="color: #66cc66;">&#40;</span>input <span style="color: #000066;">as</span> string<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> &amp; <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #b1b100;">in</span> window <span style="color: #cc66cc;">1</span></div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">end</span> <span style="color: #b1b100;">tell</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">return</span> input</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #b1b100;">end</span> <span style="color: #000066;">run</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>

<p>Next, we will save this as a Plug-in. Under the File menu, select 'Save As Plug-in..'. Then select finder as the application and give it a name you can remember:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_plug.gif" alt="image" width="351" height="296" /></p>
<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_save.gif" alt="image" width="406" height="143" /></p>

<p>Now you can just right-click your AIR application.xml, hover over Automater, and choose your new workflow to preview AIR projects!</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/mac_adl_rgtclk.gif" alt="image" width="558" height="500" /></p>

<p>Enjoy!</p>

 
      ]]></content>
    </entry>

    <entry>
      <title>Expression engine 1.6 Preview and Rick Ellis Birthday Contest</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/expression_engine_16_preview_and_rick_ellis_birthday_contest/" />
      <id>tag:studiokoi.com,2007:blog/index/1.20</id>
      <published>2007-06-19T08:14:00Z</published>
      <updated>2008-07-01T10:05:17Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="ExpressionEngine"
        scheme="http://studiokoi.com/code/category/ExpressionEngine/"
        label="ExpressionEngine" />
      <content type="html"><![CDATA[
        <p>So Expression Engine is previewing version 1.6 (filled with LOADS of new features) and it is also EE CEO Rick Ellis' Birthday. To celebrate this and some new software, they are having a photoshop contest that ends noon Tuesday June 19th, 2007. Check out the <a href="http://expressionengine.com/forums/viewthread/54250/" title="Expression Engine Forums">forums here</a> for the other submissions. Here is the <a href="http://expressionengine.com/blog/entry/160_preview_and_multiple_rick_manager_birthday_contest/" title="Expression Engine Blog">original blog post</a>.</p>

<p>Without further adieu here are my entries.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/ricky_mouse_m.jpg" style="border: 0;" alt="image" width="450" height="299" />
<br/><a href="http://codeblog.studiokoi.com/images/uploads/ricky_mouse.jpg" title="View Larger" class="thickbox">View Larger</a></p>

 <p><img src="http://codeblog.studiokoi.com/images/uploads/rick_oboe_m.jpg" style="border: 0;" alt="image" width="450" height="299" />
<br/><a href="http://codeblog.studiokoi.com/images/uploads/rick_oboe.jpg" title="View Larger" class="thickbox">View Larger</a></p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/rick_tricycle_m.jpg" style="border: 0;" alt="image" width="450" height="299" />
<br/><a href="http://codeblog.studiokoi.com/images/uploads/rick_tricycle.jpg" title="View Larger" class="thickbox">View Larger</a></p>


<p><img src="http://codeblog.studiokoi.com/images/uploads/rick_tiger_m.jpg" style="border: 0;" alt="image" width="450" height="299" />
<br/><a href="http://codeblog.studiokoi.com/images/uploads/rick_tiger.jpg" title="View Larger" class="thickbox">View Larger</a></p> 
      ]]></content>
    </entry>

    <entry>
      <title>Import classes and Multiple ActionScript layers in Flash</title>
      <link rel="alternate" type="text/html" href="http://studiokoi.com/code/comments/import_classes_and_multiple_actionscript_layers_in_flash/" />
      <id>tag:studiokoi.com,2007:blog/index/1.19</id>
      <published>2007-05-24T06:11:00Z</published>
      <updated>2007-05-23T21:15:03Z</updated>
      <author>
            <name>Greg Ferrell</name>
            <email>nospam@example.com</email>
            <uri>http://codeblog.studiokoi.com</uri>      </author>

      <category term="Flash&#45;ActionScript"
        scheme="http://studiokoi.com/code/category/Flash-ActionScript/"
        label="Flash&#45;ActionScript" />
      <content type="html"><![CDATA[
        <p>At work I am making a new Flash interface for a project, and my goal this time around was to make as many global functions out of repetitive ones as possible so we could save development time.</p>

<p>As I began writing and testing Prototype class extensions and global functions, I noticed something I had not run into before with flash. If Import classes on one ActionScript layer, and call it on another Actions layer, or even the next frame, it produces an ActionScript error.</p>

<p>Normally, I put all actions and functions on one layer and have no keyframes unless they are inside other MovieClips, or I need to animate something to audio cues. With this current project, there was going to be so much ActionScript, I decided to do more than one Actions layer so I could keep everything straight. Never having a reason to do this until now, I was not prepared for some of the problems I ran into. First and foremost being that: If import a class on one Actions layer, and I create a new class object from that group on another layer, I get a syntax error.</p>

<p>Here is a quick example. I have a MovieClip named &#8220;box1&#8221; and I am going to tween it with the Tween class. normally I would just put it all on the same layer like this:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/imprtClsMltLyrs1.gif"  alt="script" width="342" height="212" /></p>

<p><pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">Tween</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">easing</span>.<span style="color: #006600;">*</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">var</span> boxTween:Tween = <span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #66cc66;">&#40;</span>box1, <span style="color: #ff0000;">&quot;_x&quot;</span>, Regular.<span style="color: #006600;">easeOut</span>, box1.<span style="color: #0066CC;">_x</span>, box1._x<span style="color: #cc66cc;">+300</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div></li></ol></pre></p>

<p>This works perfectly and makes the box move across the screen on load. When I ran into the error was when I decided that I wanted all imports on one actions layer and the object on another. Like so:</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/imprtClsMltLyrs2.gif"  alt="moved script" width="329" height="211" /></p>

<p>I expected it to work just the same, as I have My publish settings set to ActionScript 2.0, top down, and Flash 8. Instead, I was presented with a nice error.</p>

<p><img src="http://codeblog.studiokoi.com/images/uploads/imprtClsMltLyrs3.gif"  alt="flash error" width="425" height="209" /></p>

<p>The problem is replicated also by putting the new Tween object in the following frame. Effectively, any frame that ISN&#8217;T the one I imported the class into. This strikes me as totally odd, as functions endure over frames and from layer to layer. I also tested this with other import classes besides the Tween class. I had the same issues with the Alert class and BitmapData class.</p>

<p>For now, the simple solution I came up with was making a function in the same frame/layer as the import classes that I would simply call elsewhere when I needed the tween to happen. This of course makes no sense to me at all, but it works perfectly.</p>

<p><pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">Tween</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">easing</span>.<span style="color: #006600;">*</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> tweenIt<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #000000; font-weight: bold;">var</span> boxTween:Tween = <span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #66cc66;">&#40;</span>box1, <span style="color: #ff0000;">&quot;_x&quot;</span>, Regular.<span style="color: #006600;">easeOut</span>, box1.<span style="color: #0066CC;">_x</span>, box1._x<span style="color: #cc66cc;">+300</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li></ol></pre></p>

<p>Calling this function in other frames and layers has worked so far, and has more than solved my problem. If you are using multiple tweens, or classes, you could use passthrough variables to be able to make the function usable in other situations.</p>

<p><pre class="actionscript"><ol><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">Tween</span>;</div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">transitions</span>.<span style="color: #006600;">easing</span>.<span style="color: #006600;">*</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">function</span> tweenIt<span style="color: #66cc66;">&#40;</span>mc:<span style="color: #0066CC;">MovieClip</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">Void</span> <span style="color: #66cc66;">&#123;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	mc.<span style="color: #006600;">boxTween</span> = <span style="color: #000000; font-weight: bold;">new</span> Tween<span style="color: #66cc66;">&#40;</span>mc, <span style="color: #ff0000;">&quot;_x&quot;</span>, Regular.<span style="color: #006600;">easeOut</span>, mc.<span style="color: #0066CC;">_x</span>, mc._x<span style="color: #cc66cc;">+300</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #000000; font-weight: bold;">true</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="background: #fcfcfc;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #66cc66;">&#125;</span></div></li><li style="background: #f0f0f0;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">tweenIt<span style="color: #66cc66;">&#40;</span>box1<span style="color: #66cc66;">&#41;</span></div></li></ol></pre></p>

<p>If I found out any more info as to why this is a problem, I will post it here. If anyone else has some insight, please leave me a comment.</p> 
      ]]></content>
    </entry>


</feed>