<?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; accessors</title>
	<atom:link href="http://cdmckay.org/blog/tag/accessors/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Programming, Politics and Game Design</description>
	<lastBuildDate>Thu, 09 Sep 2010 06:34:36 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Arrayzing vs. Array: get() and set()</title>
		<link>http://cdmckay.org/blog/2009/02/15/arrayzing-vs-array-get-and-set/</link>
		<comments>http://cdmckay.org/blog/2009/02/15/arrayzing-vs-array-get-and-set/#comments</comments>
		<pubDate>Sun, 15 Feb 2009 22:49:11 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Arrayzing]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[accessors]]></category>
		<category><![CDATA[arrays]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[mutators]]></category>

		<guid isPermaLink="false">http://couchware.ca/blogs/cam/?p=222</guid>
		<description><![CDATA[What does Arrayzing provide that a normal Array doesn&#8217;t?  I hope to answer that question in my series of posts Arrayzing vs. Array.  In today&#8217;s installment, we&#8217;ll take a look at what the get() and set() functions offer over an Array&#8217;s traditional [] accessor.

When using plain old JavaScript Array objects, a common operation [...]]]></description>
			<content:encoded><![CDATA[<p>What does Arrayzing provide that a normal Array doesn&#8217;t?  I hope to answer that question in my series of posts <em>Arrayzing vs. Array</em>.  In today&#8217;s installment, we&#8217;ll take a look at what the get() and set() functions offer over an Array&#8217;s traditional [] accessor.</p>
<p><span id="more-222"></span></p>
<p>When using plain old JavaScript Array objects, a common operation is to access or modify elements.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</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;">&#93;</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: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// = 6</span>
a<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">6</span><span style="color: #339933;">;</span>
a.<span style="color: #660066;">join</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;6,2,3&quot;</span></pre></div></div>

<p>Arrayzing also supports the built-in [] accessor.  However, it should be noted there are limitations to accessing an Array in this manner, most notable amongst them being that we must always index from the left.  For example, imagine we wanted to access the last element.  Using a normal Array, we would have to do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</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;">&#93;</span><span style="color: #339933;">;</span>
a<span style="color: #009900;">&#91;</span>a.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">4</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// a = [1, 2, 4];</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#91;</span>a.<span style="color: #660066;">length</span> <span style="color: #339933;">-</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// shows 4</span></pre></div></div>

<p>Although this works, it&#8217;s not the most convenient or elegant way to do it.  To address this problem, Arrayzing provides two new functions <strong>get()</strong> and <strong>set()</strong>.  These functions act essentially the same as the built-in [] accessors with an added benefit: we can index from the left <em>and</em> the right.  For example, to implement the above example using Arrayzing, we could instead do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, 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: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
a.<span style="color: #660066;">set</span>$<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// a = [1, 2, 4];</span>
<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>a.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// shows 4</span></pre></div></div>

<p><em>(Aside: If you&#8217;re not familiar with the $ after set$(), check out this <a href="http://cdmckay.org/blog/2009/02/15/arrayzing-design-choices/">article</a>.  It means that the function is a mutator).</em></p>
<p>What&#8217;s more, all Arrayzing commands that take an index as an argument allow right-indexing.  What convenience!</p>
<p>There is one further advantage of using set(): we can change an element in your zing (a zing is an Arrayzing array) without modifying the original array.  For example, if we wanted to replace an element in our zing but didn&#8217;t want to modify the original array, we would do this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:Consolas, 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: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> a.<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;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// not set$()</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;">// returns &quot;1,2,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;">// returns &quot;10,2,3&quot;</span></pre></div></div>

<p>As you can see, Arrayzing provides a strict superset of the capabilities of a normal Array&#8217;s accessors, allowing you to write cleaner, easier code.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2009/02/15/arrayzing-vs-array-get-and-set/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
