<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Cameron McKay &#187; jquery</title>
	<atom:link href="http://cdmckay.org/blog/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming and Game Development</description>
	<lastBuildDate>Mon, 14 May 2012 21:11:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>The jQuery animate() step callback function</title>
		<link>http://cdmckay.org/blog/2010/03/01/the-jquery-animate-step-callback-function/</link>
		<comments>http://cdmckay.org/blog/2010/03/01/the-jquery-animate-step-callback-function/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 16:32:59 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[animation]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1189</guid>
		<description><![CDATA[If you&#8217;ve ever needed to do more complex animations than fades and slides, then you&#8217;ve probably encountered the jQuery animate function. The animate function allows you quite a bit more flexibility than just using fadeOut or slideDown. In fact, the oft-used fades and slides simply wrap calls to animate. If you&#8217;ve ever looked at the [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever needed to do more complex animations than fades and slides, then you&#8217;ve probably encountered the <a href="http://api.jquery.com/animate/">jQuery <code>animate</code> function</a>.  The <code>animate</code> function allows you quite a bit more flexibility than just using <code>fadeOut</code> or <code>slideDown</code>.  In fact, the oft-used fades and slides simply wrap calls to <code>animate</code>.</p>
<p>If you&#8217;ve ever looked at the jQuery <code>animate</code> docs at <a href="http://api.jquery.com">api.jquery.com</a> you might have noticed that one of the optional arguments you can define is <code>step</code> which is defined as:</p>
<blockquote><p>A function to be called after each step of the animation.</p></blockquote>
<p>&#8230;and that&#8217;s it.  If you search for &#8220;step&#8221; on the page, you won&#8217;t see another mention of it.</p>
<p><span id="more-1189"></span></p>
<p>Fortunately, a quick search on Google for &#8220;jquery animate step&#8221; will yield <a href="http://docs.jquery.com/Release:jQuery_1.2/Effects">the jQuery 1.2 release notes</a> that sheds a bit of light <a href="http://docs.jquery.com/Release:jQuery_1.2/Effects#Extensible_Animations">on the step function</a>:</p>
<blockquote><p>You can now extend jQuery animations with a function that is fired on every step of the animation that changes the style of the element being animated. It can be extended for specific css properties, or even to create a custom animation type.</p>
<p>For example, you can pass in an extra step function to .animate() to perform actions like animation synchronization.</p></blockquote>
<p>The release notes also contain a code sample:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#go&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.block:first&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> left<span style="color: #339933;">:</span> <span style="color: #CC0000;">100</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
      duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span>
      step<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>now<span style="color: #339933;">,</span> fx<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.block:gt(0)&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;left&quot;</span><span style="color: #339933;">,</span> now<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now we know the arguments <code>step</code> takes: <code>now</code> and <code>fx</code>.  We can also tell from the code that <code>now</code> contains the value being animated.  <code>fx</code>, however, is still a bit mystery.  We can solve that mystery by running the above code in <a href="http://getfirebug.com/">Firebug</a>, setting a break point when the step function is called, and then inspecting the <code>fx</code> and <code>now</code> objects.  Here are the results of that operation:</p>
<table>
<thead>
<tr>
<th colspan="3">The <code>fx</code> object</th>
</tr>
<tr>
<th>Attribute</th>
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td>elem</td>
<td>DOM Element</td>
<td>The element being animated.</td>
</tr>
<tr>
<td>end</td>
<td>Number</td>
<td>The value the animation will end at.</td>
</tr>
<tr>
<td>now</td>
<td>Number</td>
<td>The value the animation is currently at.</td>
</tr>
<tr>
<td>options</td>
<td>Object</td>
<td>Some animations options.</td>
</tr>
<tr>
<td>options.curAnim</td>
<td>Object</td>
<td>Information about the current animation.  For example, if you passed <code>{ opacity: 0, top: "+=16" }</code> as the first parameter of <code>animate</code>, then they will be attributes of this object.</td>
</tr>
<tr>
<td>options.duration</td>
<td>Number</td>
<td>The duration that you passed to <code>animate</code>.</td>
</tr>
<tr>
<td>pos</td>
<td>Number</td>
<td>Sweeps from 0.0 to 1.0 during the animation. Sort of like a ‘t’ value for interpolations.</td>
</tr>
<tr>
<td>prop</td>
<td>String</td>
<td>The CSS property being varied (e.g. &#8220;opacity&#8221; or &#8220;top&#8221;).</td>
</tr>
<tr>
<td>start</td>
<td>Number</td>
<td>The starting value of the CSS property being animated.</td>
</tr>
<tr>
<td>startTime</td>
<td>Number</td>
<td>The timestamp indicating what time the animation started.</td>
</tr>
<tr>
<td>state</td>
<td>Number</td>
<td>Which step of the animation we&#8217;re on.  For example, if you made an animation that was moving 100px to the right over 100ms, and jQuery moves elements every 10ms, then there would be 10 states.</td>
</tr>
<tr>
<td>unit</td>
<td>String</td>
<td>The unit of the CSS value being animated (e.g. &#8220;px&#8221; or &#8220;em&#8221;), if applicable.</td>
</tr>
</tbody>
</table>
<p>There were a couple more attributes (most of them functions) that I did not include because I either didn&#8217;t think they&#8217;d be useful or I didn&#8217;t really know what they did.  If you need to know what those do, I suggest Firebugging up your code and taking a look yourself <img src='http://cdmckay.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/03/01/the-jquery-animate-step-callback-function/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>The JSS Stylesheet, Part 1</title>
		<link>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/</link>
		<comments>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/#comments</comments>
		<pubDate>Tue, 05 May 2009 16:05:40 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[event properties]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[stylesheets]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=459</guid>
		<description><![CDATA[I&#8217;ve said previously that JSS is a lot like CSS. What is CSS? Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language. If that blurb were written about JSS, it&#8217;d sound a little like this: JavaScript [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve said previously that JSS is a lot like <a href="http://en.wikipedia.org/wiki/CSS">CSS</a>.  What is CSS?</p>
<blockquote><p>Cascading Style Sheets (CSS) is a style sheet language used to describe the presentation (that is, the look and formatting) of a document written in a markup language.</p></blockquote>
<p>If that blurb were written about JSS, it&#8217;d sound a little like this:</p>
<blockquote><p>JavaScript Style Sheets (JSS) is a style sheet language used to describe the presentation (that is, the look and formatting) and functionality of a document written in HTML.</p></blockquote>
<p>In this article, we will look at the syntactic structure of a JSS stylesheet.  We will also talk briefly about including JSS stylesheets in your HTML documents.</p>
<p><span id="more-459"></span></p>
<p>At this point, some people may be wondering: <em>aren&#8217;t we mucking up CSS by combining functionality and presentation in the same place?</em>  This is a valid concern.  However, just because we can combine presentation and functionality in JSS, doesn&#8217;t necessarily mean we should.  A good JSS site will have two or more JSS stylesheets: one for JSS styles (if they are needed) and one for JSS event properties.  JSS event properties will be covered in a future article.</p>
<h3>Syntactic Structure</h3>
<p>Let&#8217;s get back to the CSS/JSS connection.  What do people do with CSS?  They write stylesheets.  More specifically, they write rules.  In JSS, we write rules too.  In fact, we write them in a very similar format to CSS, although with some important differences owing to the fact that JSS stylesheets are actually JavaScript programs.  Here&#8217;s the basic format of a CSS stylesheet:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">rule-selector-a
<span style="color: #00AA00;">&#123;</span>
  property-a<span style="color: #00AA00;">:</span> value-a<span style="color: #00AA00;">;</span>
  property-b<span style="color: #00AA00;">:</span> value-b<span style="color: #00AA00;">;</span>
  ...
<span style="color: #00AA00;">&#125;</span>
&nbsp;
rule-selector-b
<span style="color: #00AA00;">&#123;</span>
  property-a<span style="color: #00AA00;">:</span> value-a<span style="color: #00AA00;">;</span>
  property-b<span style="color: #00AA00;">:</span> value-b<span style="color: #00AA00;">;</span>
  ...
<span style="color: #00AA00;">&#125;</span>
&nbsp;
...</pre></div></div>

<p>In JSS, we use pretty much the same format, with the differences being the addition of boilerplate code (1), quoted selectors with colons (2), underscores instead of dashes in property names (3), quoted values with commas (4) and commas between rules (5).  The JSS format is shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// (1)</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// (2)</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// (3) and (4)</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span>
  ...
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// (5)</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span>
  ...
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
...
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// (1)</span></pre></div></div>

<p>Let&#8217;s talk a bit about each of these difference:</p>
<ol>
<li><strong>Boilerplate code.</strong>  This is an unfortunate side effect of having to use JavaScript to extend CSS.  Fortunately, it&#8217;s the same for each JSS stylesheet, so it&#8217;s not too much of a hassle to remember.</li>
<li><strong>Quoted selectors with colons.</strong> Since the selector portion of a JSS rule is a string, it must be represented in JSS as a JavaScript string.  All strings in JavaScript must be either single-quoted (as in &#8216;Hello!&#8217;) or double-quoted (as in &#8220;w00t w00t&#8221;).  The colon following the selector is a necessity as internally, JSS selectors act as keys in a JavaScript object.  A JSS selector may be any valid jQuery selector.</li>
<li><strong>Underscores instead of dashes in property names.</strong>  This is not strictly true, although using underscores instead of dashes is the recommended way of representing JSS properties.  Consider, for example, the <em>font-family</em> property from CSS.  This can be represented in 3 ways in JSS:
<ol>
<li>font_family</li>
<li>&#8220;font_family&#8221;</li>
<li>&#8220;font-family&#8221;</li>
</ol>
</li>
<li><strong>Quoted values with commas.</strong> Unlike CSS, all values in JSS are quoted with either single or double quotes.  Furthermore, between declarations, there must be a comma.<br />
<br/></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// This is correct.</span>
<span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// This is incorrect.</span>
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span>
  property_a<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-a&quot;</span><span style="color: #339933;">,</span>
  property_b<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;value-b&quot;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// This declaration should not </span>
                         <span style="color: #006600; font-style: italic;">// have a comma.</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</li>
<li><strong>Commas between rules.</strong> Like declarations, each rule must have a comma between it:

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #3366CC;">&quot;rule-selector-a&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
<span style="color: #3366CC;">&quot;rule-selector-b&quot;</span><span style="color: #339933;">:</span>
<span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// Make sure the last rule has no comma after it.</span></pre></div></div>

</li>
</ol>
<h3>Loading JSS and JSS stylesheets</h3>
<p>Now that we understand the syntactic structure of a JSS stylesheet, it&#8217;s time to take look at how we include them in an HTML file.  Every page that makes use of JSS will have the following structure:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Title<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-1.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-2.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    ...
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sheet-n.jss.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Your website here. --&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>Let&#8217;s quickly go through the components here.  The first <code>script</code> tag includes the jQuery library, which is required by JSS.  The second <code>script</code> tag includes the JSS jQuery plugin, which is the brains of JSS, and is required for any JSS stylesheet to work.  All the <code>script</code> tags after that are the JSS stylesheets that you wish to load.</p>
<p>As you can see, you can add an unlimited number of JSS stylesheets, and when the page loads, they&#8217;ll be applied in the same order they are included in the page.  Keep in mind, however, that if you&#8217;re including many <code>script</code> tags on a single webpage, it is advisable to use some sort of JavaScript loader.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/05/05/the-jss-stylesheet/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing JSS</title>
		<link>http://cdmckay.org/blog/2009/04/30/introducing-jss/</link>
		<comments>http://cdmckay.org/blog/2009/04/30/introducing-jss/#comments</comments>
		<pubDate>Fri, 01 May 2009 04:43:34 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JSS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[ie7]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[selector]]></category>
		<category><![CDATA[stylesheet]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=391</guid>
		<description><![CDATA[Ever wanted to make an image rollover, but you don&#8217;t know JavaScript? In this tutorial, we show you how JSS, a CSS-like language, makes this a breeze. Consider these two images: Imagine we wanted to make them &#8220;rollover&#8221;, i.e. the zebra should be shown when nothing is happening, and the shark should be showed when [...]]]></description>
			<content:encoded><![CDATA[<p>Ever wanted to make an image rollover, but you don&#8217;t know JavaScript?  In this tutorial, we show you how JSS, a CSS-like language, makes this a breeze.</p>
<p><span id="more-391"></span></p>
<p>Consider these two images:</p>
<p><img src="http://couchware.ca/users/cam/example/rollover/image_off.gif" alt="Zebra" /><br />
<img src="http://couchware.ca/users/cam/example/rollover/image_on.gif" alt="Shark" /></p>
<p>Imagine we wanted to make them &#8220;rollover&#8221;, i.e. the zebra should be shown when nothing is happening, and the shark should be showed when the mouse is &#8220;rolled over&#8221; (i.e. on top of) the zebra.  Like this:</p>
<p><img src="http://couchware.ca/users/cam/example/rollover/image_off.gif" src-out="http://couchware.ca/users/cam/example/rollover/image_off.gif" src-over="http://couchware.ca/users/cam/example/rollover/image_on.gif" /><script src="http://www.google.com/jsapi" type="text/javascript"></script> <script type="text/javascript"><!--
google.load("jquery", "1.3.2");
google.setOnLoadCallback(function()
{
  $("img[src-over]").hover(
    function() { $(this).attr('src', $(this).attr('src-over')); },
    function() { $(this).attr('src', $(this).attr('src-out')); }
  );
});
// --></script></p>
<p>Before we start writing some JSS, let&#8217;s write some HTML for a page just showing the rollover at rest (i.e. just show a plain old static image):</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>If we copied this code into an HTML file and ran it on our browser, we would see a static image of a zebra.  Not that exciting, right?  Now let&#8217;s see how we can use JSS to make this zebra &#8220;rollover&#8221; to the shark when the mouse is on it.  Here&#8217;s the JSS stylesheet:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s step through this syntax, because there&#8217;s a lot going on here, even though it&#8217;s a single line.  First, let&#8217;s annotate the JSS source so we can refer to it easier:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// 1. Boilerplate that starts any JSS stylesheet.</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// 2. Rule</span>
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// 3. CSS Selector</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// 4. JSS Declaration</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you might have noticed, a JSS stylesheet looks very similar to a CSS stylesheet.  Let&#8217;s walk through the anatomy of a JSS stylesheet.</p>
<p>At the top of any JSS stylesheet there is some boilerplate code (1).  This is necessary because a JSS stylesheet is actually a JavaScript program.  However, beyond the fact that this boilerplate needs to be in every JSS stylesheet, you don&#8217;t really need to know what it does.</p>
<p>A JSS stylesheet is made up of an unlimited number of rules (2).  This is the same as in a CSS stylesheet.  Inside a JSS rule, there are two components, a selector (3) and an unlimited number of declarations (inside a declaration block) (4).  This is where we start to see some of the advantages of JSS over using plain CSS.</p>
<p>JSS selectors are powered by <a href="http://jquery.com">jQuery</a>.  As such, JSS supports any CSS3-compliant selector.  In addition, JSS also allows you to use any of the jQuery CSS selector extensions as well.</p>
<p>For example, in CSS, this stylesheet should stripe all the tables on the page as alternating gray and white:</p>

<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;">table tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>odd<span style="color: #00AA00;">&#41;</span>
<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#eee</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span>
&nbsp;
table tr<span style="color: #3333ff;">:nth-</span>child<span style="color: #00AA00;">&#40;</span>even<span style="color: #00AA00;">&#41;</span>
<span style="color: #00AA00;">&#123;</span> <span style="color: #000000; font-weight: bold;">background-color</span><span style="color: #00AA00;">:</span> <span style="color: #cc00cc;">#fff</span><span style="color: #00AA00;">;</span> <span style="color: #00AA00;">&#125;</span></pre></div></div>

<p>However, if you tried to use that CSS stylesheet in the two most popular browsers available (IE7 and Firefox 3) you would discover that it didn&#8217;t work.  On the other hand, if you use the following JSS stylesheet&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;table tr:nth-child(odd)&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span> background_color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#eee&quot;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;table tr:nth-child(even)&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span> background_color<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;#fff&quot;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;you would discover that it worked in all major browsers.  By using a JavaScript selector engine, JSS offers better cross-browser styling than regular CSS.</p>
<p><em>(Aside: Before all you web developers out there go crazy, I would like to note that I realize there are several drawbacks to using JavaScript to style your page.  Stay tuned to the end of this article for a discussion of them).</em></p>
<p>Now let&#8217;s return to our task of making a rollover.  How does the JSS stylesheet make it possible?  Let&#8217;s take a look at that JSS stylesheet again:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span> <span style="color: #006600; font-style: italic;">// 1. Boilerplate that starts any JSS stylesheet.</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// 2. Rule</span>
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span> <span style="color: #006600; font-style: italic;">// 3. CSS Selector</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// 4. JSS Declaration</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s look closer at the rule.  More specifically, take a look at the CSS selector.  For anyone familiar CSS, it reads &ldquo;select all <strong>img</strong> tags that have the attribute <strong>src-over</strong>&rdquo;.  Why would we want to do that?  Well, as it happens, we need some way to describe which image to use when the user mouses over our image.  In order to do that, we&#8217;re going to use something called a <strong>custom attribute</strong>.</p>
<p>Custom attributes are user-defined attributes on HTML tags.  For example, the <strong>img</strong> tag has an attribute called <strong>src</strong>.  That&#8217;s a proper attribute that is defined in a W3C recommendation.  What we&#8217;re going to do is add two new attributes, <strong>src-over</strong> and <strong>src-out</strong>, that are not part of any W3C standard.  We just made them up because they&#8217;re convenient for our purposes.</p>
<p>Now that we&#8217;ve decided that we&#8217;re adding these two attributes, we need to add them to our HTML file:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span></span>
<span style="color: #009900;">        src-out<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> src-over<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_on.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>With that done, let&#8217;s take a look at the last part of the JSS stylesheet: the declaration.  A JSS declaration has a property (hover) and a value (&#8220;set-attr src {src-over} | set-attr src {src-out}&#8221;).  This is just like a CSS declaration, like &#8220;font-family: Arial, sans-serif&#8221;.  In that case, &#8220;font-family&#8221; is the property and &#8220;Arial, sans-serif&#8221; is the value.</p>
<p>In the case of the rollover JSS stylesheet, the hover property must have a value with the following structure:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jss-expression | jss-expression&quot;</span></pre></div></div>

<p>The first JSS expression on the left is run when the mouse is over the image area, and the right JSS expression is run when the mouse leaves the image area.</p>
<p>Let&#8217;s look at what happens in the JSS rollover stylesheet when the mouse is brought over the image area:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">set<span style="color: #339933;">-</span>attr src <span style="color: #009900;">&#123;</span>src<span style="color: #339933;">-</span>over<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This JSS expression reads: &#8220;set the src attribute (of the selected element) to the contents of the src-over attribute (of the selected element)&#8221;.  This is because &#8220;set-attr&#8221; is a JSS <em>command</em>.  The &#8220;set-attr&#8221; command takes two arguments: the attribute to change, and the value to change it to.  That leaves one more question.  What&#8217;s the deal with the curly braces?</p>
<p>In JSS, curly braces are basically a &#8220;get-attr&#8221; command.  They fetch the value of an attribute.  In the context they are being used here, they fetch the value of the &#8220;src-over&#8221; attribute (which happens to be the name of the shark image &#8220;image_on.gif&#8221;).</p>
<p>With that information in hand, it can be deduced that the right hand side&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">set<span style="color: #339933;">-</span>attr src <span style="color: #009900;">&#123;</span>src<span style="color: #339933;">-</span>out<span style="color: #009900;">&#125;</span></pre></div></div>

<p>&#8230;switches the src attribute back to src-out image URL when the mouse leaves the image area.</p>
<p>Thus, if we put it all together, and include the appropriate JavaScript files, we have the following.  In the &#8220;index.html&#8221; file we have:</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>JSS Rollover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'jquery.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'jquery.jss.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'text/javascript'</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">'styles.jss.js'</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span></span>
<span style="color: #009900;">        src-off<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_off.png&quot;</span> src-over<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image_on.png&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>And in the &#8220;styles.jss.js&#8221; file we have this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">jss</span>.<span style="color: #660066;">declare</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #3366CC;">&quot;img[src-over]&quot;</span><span style="color: #339933;">:</span>
  <span style="color: #009900;">&#123;</span>
    hover<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;set-attr src {src-over} | set-attr src {src-out}&quot;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>These files together create a page that has a rollover that changes from zebra to shark when the mouse is hovered over it.  Better yet, this JSS stylesheet can be used with any web page to enable rollover images.  All the user has to do is set the &#8220;src-over&#8221; and &#8220;src-out&#8221; custom attributes.</p>
<h3>Notes on JSS Availability &amp; Performance</h3>
<p>JSS applies its stylesheets using JavaScript.  This has several implications to web designers using it.  First of all, in order for a site to take advantage of JSS, JavaScript must be enabled.  Fortunately, something like 95% of all web surfers have JavaScript enabled, so that&#8217;s not too big of deal.  Secondly, JSS is a lot slower than CSS.  For most websites, this difference will probably be imperceptible deal, but it&#8217;s still something to be aware of.</p>
<p>My recommendations would be this: if you can do it in CSS, and it works properly across all browsers, then use CSS.  There&#8217;s no reason not to.  However, if you need to use JSS features like event properties (like hover) or cross-browser selectors, then use JSS.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/04/30/introducing-jss/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Arrayzing Command: merge()</title>
		<link>http://cdmckay.org/blog/2009/02/26/new-arrayzing-command-merge/</link>
		<comments>http://cdmckay.org/blog/2009/02/26/new-arrayzing-command-merge/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 01:53:12 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Arrayzing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[merge$]]></category>
		<category><![CDATA[mutators]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=242</guid>
		<description><![CDATA[Some nights, as I lay awake in bed, trying to sleep, I think about Arrayzing workflows.  Lately, I&#8217;ve been thinking of the different ways users might use Arrayzing. In particular, I&#8217;ve been thinking of how a user could edit a subset of elements in a zing without disturbing other elements. The result of this thoughtstream [...]]]></description>
			<content:encoded><![CDATA[<p>Some nights, as I lay awake in bed, trying to sleep, I think about Arrayzing workflows.  Lately, I&#8217;ve been thinking of the different ways users might use Arrayzing.  In particular, I&#8217;ve been thinking of how a user could edit a subset of elements in a zing without disturbing other elements. The result of this thoughtstream is a new Arrayzing command: <code>merge()</code>.</p>
<p><span id="more-242"></span></p>
<p>In short, the <code>merge()</code> command allows you to merge your current zing with the previous zing in your stack.  What&#8217;s the stack, you ask?</p>
<p>Recall that, by default, Arrayzing a functional API, meaning that (unless you&#8217;re using a mutator command) your zing is never modified.  Instead a new one is returned.  So, if we do this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">144</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">gteq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>&#8230;we have in fact created 3 zings, not one.  Although it seems wasteful to keep making new zings, it turns out to be pretty useful.  For one, it allows us to step backwards using the <code>undo()</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">9</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">144</span><span style="color: #009900;">&#41;</span>  <span style="color: #006600; font-style: italic;">// [1, 4, 9, 144]</span>
  .<span style="color: #660066;">gteq</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>        <span style="color: #006600; font-style: italic;">// [4, 9, 144]</span>
  .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [2, 3, 12]</span>
  .<span style="color: #660066;">undo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>         <span style="color: #006600; font-style: italic;">// [4, 9, 144]</span>
  .<span style="color: #660066;">undo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>         <span style="color: #006600; font-style: italic;">// [1, 4, 9, 144]</span>
  .<span style="color: #660066;">undo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        <span style="color: #006600; font-style: italic;">// []</span></pre></div></div>

<p><em>(Aside: For those familiar with jQuery, the <code>undo()</code> command is the same as the end() command from that library).</em></p>
<p>Keeping a history (or stack) also allows us to combine the past with the present using the <code>andSelf()</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> double <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> x <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
_<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span>    <span style="color: #006600; font-style: italic;">// [1, 2, 3]</span>
  .<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>double<span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [2, 4, 6]</span>
  .<span style="color: #660066;">andSelf</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// [2, 4, 6, 1, 2, 3]</span></pre></div></div>

<p>But what else?  Well, consider this situation:  What if we wanted to modify only a subset of a zing?  For example, imagine we a list of monetary values and we wanted to make sure they all had the &#8220;$&#8221; character in front of them.  Let&#8217;s try and do that without using <code>merge()</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fn <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>e.<span style="color: #660066;">startsWith</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;$&quot;</span> <span style="color: #339933;">+</span> e<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">map</span><span style="color: #009900;">&#40;</span>fn<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>That solution is OK.  It&#8217;s pretty simple but it still requires a lot of typing and it&#8217;s not really using Arrayzing for much (just the <code>map()</code> command).  Let&#8217;s see if we can reduce the number of keystrokes and make better use of Arrayzing&#8217;s capabilities by using the new <code>merge()</code> command:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\$/</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">prepend</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So what just happened there?  What does it all mean?  Let&#8217;s look at that again, and track the contents of the zing:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\$/</span><span style="color: #009900;">&#41;</span>    <span style="color: #006600; font-style: italic;">// [ &quot;100.00&quot; ]</span>
  .<span style="color: #660066;">prepend</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ]</span>
  .<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
  <span style="color: #006600; font-style: italic;">// [ &quot;$1.00&quot;, &quot;$1.99&quot;, &quot;$100.00&quot;, &quot;$5&quot; ]</span></pre></td></tr></table></div>

<p>In line 1, we create the Arrayzing (nothing special here).  In the following line, we filter out all strings that don&#8217;t begin with &#8220;$&#8221; using a regular expression.  Things get a little more interesting on line 3, where we use the mutator version of <code>prepend()</code> to modify the filtered array in-place.  Finally, on line 4, we use the <code>merge()</code> command which, basically puts the originally filtered monetary value back into the preceding zing.</p>
<p>So why did we use the <code>prepend$()</code> instead of <code>prepend()</code>?  The reason for that is because using <code>prepend()</code> would have created another level of history.  For example, here is what would happen if we had used <code>prepend()</code> instead of <code>prepend$()</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\$/</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">// [ &quot;100.00&quot; ]</span>
  .<span style="color: #660066;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ]</span>
  .<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ]</span></pre></td></tr></table></div>

<p>Why did that happen?  The answer to that question lies in how <code>merge()</code> merges.  Let&#8217;s start by looking at line 2 of the last code snippet.  In that line, we filtered out all strings that did not start with a &#8220;$&#8221; character.  When Arrayzing returns a new zing that contains only the elements that start with a &#8220;$&#8221;, it also remembers where the new elements came from.  So in the case of the <code>not()</code> function on line 2, Arrayzing remembers that &#8220;100.00&#8243; came from index 2 of the zing in line 1 (that is, the zing _(&#8220;$1.00&#8243;, &#8220;$1.99&#8243;, &#8220;100.00&#8243;, &#8220;$5&#8243;)).  The <code>merge()</code> command only operates on the current zing and the one immediately previous to it on the stack.</p>
<p>With that in mind, let&#8217;s look at line 3.  On line 3, we run the <code>prepend()</code> command, which creates and returns a new zing that contains the value &#8220;$100.00&#8243;.  What&#8217;s more, it also remembers that the value &#8220;$100.00&#8243; was derived from &#8220;100.00&#8243; (index 0) in the zing immediately previous to it in the stack (that is, the zing _(&#8220;100.00&#8243;)).  Thus, when merged, it simply overwrites the value it derived from, which is the 0th index of the zing [ "100.00" ]&#8230; which results in the zing [&nbsp;"$100.00"&nbsp;].  You follow all that?</p>
<p>This concept is easier to understand by annotating the previous code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> zing1 <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$1.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$1.99&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;100.00&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;$5&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [ &quot;$1.00&quot;, &quot;$1.99&quot;, &quot;100.00&quot;, &quot;$5&quot; ] index 0-3 come from nothing</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> zing2 <span style="color: #339933;">=</span> zing1.<span style="color: #660066;">not</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/^\$/</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [ &quot;100.00&quot; ] index 0 comes from index 2 in zing1</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> zing3 <span style="color: #339933;">=</span> zing2.<span style="color: #660066;">prepend</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;$&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ] index 0 comes from index 0 in zing2</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> zing4 <span style="color: #339933;">=</span> zing3.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [ &quot;$100.00&quot; ] copy index 0 from zing3 over index 0 in zing2</span></pre></div></div>

<p>Now, with that more or less explained, let&#8217;s go on to one final item:  What happens when we try to merge a zing that we&#8217;ve added to or removed from.  For example, if we added elements like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;str&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">add</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What would be in the zing returned by this sequence of functions?  In this case, when <code>merge()</code> is called, Arrayzing would not know where to merge the 7 element (since it only remembers the original indices of elements that directly derived from the previous zing).  Arrayzing deals with this scenario by simply adding all elements that it does not have an index for to the end of the previous zing in the stack.  Here&#8217;s what that same snippet would look like if we documented each step:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;str&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [ 2, 3, 4 ]</span>
  .<span style="color: #660066;">add</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">// [ 2, 3, 4, 7 ]</span>
  .<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #006600; font-style: italic;">// [ &quot;str&quot;, 2, 3, 4, &quot;a&quot;, &quot;b&quot;, 7 ]</span></pre></div></div>

<p>How about if we removed elements?  If we were to remove elements, they would simply not be merged back into the previous zing on the stack.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;str&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #009900;">&#41;</span>
  .<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>    <span style="color: #006600; font-style: italic;">// [ 2, 3, 4 ]</span>
  .<span style="color: #660066;">set</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">// [ 4, 3, 4 ]</span>
  .<span style="color: #660066;">set</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">7</span><span style="color: #009900;">&#41;</span>   <span style="color: #006600; font-style: italic;">// [ 4, 3, 7 ]</span>
  .<span style="color: #660066;">removeAt</span>$<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #006600; font-style: italic;">// [ 3, 7 ]</span>
  .<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #006600; font-style: italic;">// [ &quot;str&quot;, 2, 3, 7, &quot;a&quot;, &quot;b&quot; ]</span></pre></div></div>

<p>As you can see, the <code>merge()</code> command is a powerful addition to Arrayzing.  It allows us to select a subset of the elements, transform them, and then re-add them to our original zing.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/26/new-arrayzing-command-merge/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Arrayzing Design Choices</title>
		<link>http://cdmckay.org/blog/2009/02/15/arrayzing-design-choices/</link>
		<comments>http://cdmckay.org/blog/2009/02/15/arrayzing-design-choices/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 08:45:08 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Arrayzing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[decisions]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[mutators]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=191</guid>
		<description><![CDATA[Lately, I&#8217;ve been working an awful lot on a new project called Arrayzing.  The goal of Arrayzing is to adapt the design of jQuery to work with JavaScript arrays of anything.  For example, to get all the numbers out of an Arrayzing array (which I have started calling a zing, much to Kevin&#8217;s dismay) we [...]]]></description>
			<content:encoded><![CDATA[<p>Lately, I&#8217;ve been working an awful lot on a new project called <a href="http://cdmckay.org/blog/arrayzing">Arrayzing</a>.  The goal of Arrayzing is to adapt the design of <a href="http://jquery.com">jQuery</a> to work with JavaScript arrays of anything.  </p>
<p><span id="more-191"></span></p>
<p>For example, to get all the numbers out of an Arrayzing array (which I have started calling a <em>zing</em>, much to <a href="http://couchware.ca/blogs/kev">Kevin&#8217;s</a> dismay) we would simply have to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">42</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">42</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Although this is useful in itself, Arrayzing can still do more.  Imagine that not only did we want to extract all the numbers in an array, we wanted to sum them too.  Arrayzing can handle that without breaking a sweat:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">_<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">42</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sum</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And that&#8217;s just scratching the surface!  But enough about that (we&#8217;ll deal with the power of Arrayzing is many posts in the future).  Let&#8217;s talk about API design.</p>
<p>Arrayzing has been a lot of fun to design.  Along the way there have been many design decisions to make and I thought it&#8217;d be useful to write about them for the benefit of myself and other would-be API designers.</p>
<p>The first decision I&#8217;ll talk about is probably the most radical of the recent changes.  It has to do with the functional nature of Arrayzing (which was inherited from jQuery).</p>
<p>Basically, every time an Arrayzing command causes a zing to change (i.e. the numbers() function is invoked), Arrayzing will return a new zing as opposed to just returning a modified version of the current zing.  (Remember, a zing is an Arrayzing object).  This idea is illustrated below:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// str() is just a convenient way of calling toString()</span>
<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;two&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
a.<span style="color: #660066;">str</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;1,two,3&quot;</span>
<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> a.<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
a.<span style="color: #660066;">str</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;1,two,3&quot;</span>
b.<span style="color: #660066;">str</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;1,3&quot;</span></pre></div></div>

<p>This behaviour can be useful in a number of cases.  For instance,</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;three&quot;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;foo&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> sum <span style="color: #339933;">=</span> a.<span style="color: #660066;">numbers</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sum</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 7</span>
<span style="color: #003366; font-weight: bold;">var</span> strings <span style="color: #339933;">=</span> a.<span style="color: #660066;">strings</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;,&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;three,foo&quot;</span></pre></div></div>

<p>As you can see, we don&#8217;t need to keep a copy of the initial values (1, 2, &#8220;three&#8221;, etc.), we can simply re-use the initial zing.  We also won&#8217;t accidentally modify a zing when we don&#8217;t want to.</p>
<p>However, sometimes we want to modify an array in-place.  For example, imagine we had an object called Reader that would read from some data source, and periodically have strings available.  In order to check if the Reader has more strings, we would call Reader.hasNext().  In order to read the next string, we would call Reader.next().  So, to read all the strings that Reader has available at a given moment, we would iterate like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> array <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Array<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>Reader.<span style="color: #660066;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  array.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>Reader.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That same code in Arrayzing, as I&#8217;ve described it so far, would be this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> zing <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>Reader.<span style="color: #660066;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  zing <span style="color: #339933;">=</span> zing.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>Reader.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which has several problems.  First of all, it&#8217;s not altogether obvious that this is how we&#8217;d it.  Secondly, it&#8217;s pretty inefficent.  Every iteration is creating a new Arrayzing that needn&#8217;t exist for our purposes.  So what&#8217;s the solution?  Borrow from our friend <a href="http://ruby-lang.org">Ruby</a> and throw in some mutator methods!  </p>
<p>In Ruby, when a method modifies an object in-place, it is suffixed with an exclamation mark <strong>!</strong>.  Unfortunately, JavaScript doesn&#8217;t allow for us to use exclamation marks in our variables.  In place of that, Arrayzing uses the <strong>$</strong> character.  So now, in Arrayzing, every function that returns a modified zing also has a sister function with a suffixed $ that will modify the zing in-place.</p>
<p>Thus, returning to the example with the Reader, we can now use a mutator function:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> zing <span style="color: #339933;">=</span> _<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>Reader.<span style="color: #660066;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
  zing.<span style="color: #660066;">push</span>$<span style="color: #009900;">&#40;</span>Reader.<span style="color: #660066;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/15/arrayzing-design-choices/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

