<?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>Codec &#187; javascript</title>
	<atom:link href="http://www.trembl.org/codec/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.trembl.org/codec</link>
	<description>A Personal Polylogic Code/Decode &#039;Zettelkasten&#039;</description>
	<lastBuildDate>Thu, 02 Feb 2012 03:38:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Swapping Array Items in Javascript</title>
		<link>http://www.trembl.org/codec/814/</link>
		<comments>http://www.trembl.org/codec/814/#comments</comments>
		<pubDate>Thu, 08 Dec 2011 07:55:04 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[swap]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=814</guid>
		<description><![CDATA[var myArray = &#91;&#34;a&#34;, &#34;c&#34;, &#34;b&#34;&#93;; myArray.swap&#40;1, 2&#41;; console.log&#40;myArray&#41;; // [&#34;a&#34;, &#34;b&#34;, &#34;c&#34;]; &#160; Array.prototype.swap=function&#40;a, b&#41; &#123; this&#91;a&#93;= this.splice&#40;b, 1, this&#91;a&#93;&#41;&#91;0&#93;; return this; &#125; It could also be done with a temp var, but this solution seems to be the most elegant. The key to this is in the additional arguments the splice() function can [...]]]></description>
			<content:encoded><![CDATA[
<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;a&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;b&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
myArray.<span style="color: #660066;">swap</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: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>myArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;];</span>
&nbsp;
Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">swap</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">splice</span><span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It could also be done with a temp var, but this solution seems to be the most elegant. The key to this is in the additional arguments the splice() function can accept.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">splice<span style="color: #009900;">&#40;</span>start<span style="color: #339933;">,</span> itemCount<span style="color: #339933;">,</span> additionalItems ...<span style="color: #009900;">&#41;</span></pre></div></div>

<p>splice() removes elements denominated by the <em>start</em> and <em>itemCount</em> argument, and &#8211; if specified &#8211; inserts <em>additional items</em> at the same position.</p>
<p>Also important to note is that splice() returns an <em>array</em>. And to turn that array into an item again we need to select it directly with [0].</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/814/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Copying Arrays in Javascript with slice(0)</title>
		<link>http://www.trembl.org/codec/813/</link>
		<comments>http://www.trembl.org/codec/813/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 08:10:17 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[copy]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[slice]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=813</guid>
		<description><![CDATA[Using Arrays in Javascript retain their values by references. var oneArray = &#91;&#34;a&#34;, &#34;b&#34;, &#34;c&#34;&#93;; var anotherArray = oneArray; anotherArray&#91;0&#93; = &#34;z&#34;; &#160; console.log&#40;oneArray, anotherArray&#41;; // [&#34;z&#34;, &#34;b&#34;, &#34;c&#34;], [&#34;z&#34;, &#34;b&#34;, &#34;c&#34;] Ok, but let&#8217;s say you&#8217;ll need to make a copy of the array. How do you do that? var oneArray = &#91;&#34;a&#34;, &#34;b&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>Using Arrays in Javascript retain their values by references.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> oneArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</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: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> anotherArray <span style="color: #339933;">=</span> oneArray<span style="color: #339933;">;</span>
anotherArray<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: #3366CC;">&quot;z&quot;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>oneArray<span style="color: #339933;">,</span> anotherArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [&quot;z&quot;, &quot;b&quot;, &quot;c&quot;], [&quot;z&quot;, &quot;b&quot;, &quot;c&quot;]</span></pre></div></div>

<p>Ok, but let&#8217;s say you&#8217;ll need to make a <em>copy</em> of the array. How do you do that?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> oneArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</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: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> anotherArray <span style="color: #339933;">=</span> oneArray.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
anotherArray<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: #3366CC;">&quot;z&quot;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>oneArray<span style="color: #339933;">,</span> anotherArray<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// [&quot;a&quot;, &quot;b&quot;, &quot;c&quot;], [&quot;z&quot;, &quot;b&quot;, &quot;c&quot;]</span></pre></div></div>

<p>If you&#8217;d really, really want you could also wrap it into a prototype function. Although the code-characters savings are minimal.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Array.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">copy</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: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> anotherArray <span style="color: #339933;">=</span> oneArray.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//vs</span>
<span style="color: #003366; font-weight: bold;">var</span> anotherArray <span style="color: #339933;">=</span> oneArray.<span style="color: #660066;">copy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Minimal, but maybe contextually for self-explaining.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/813/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Measuring Text in Canvas</title>
		<link>http://www.trembl.org/codec/809/</link>
		<comments>http://www.trembl.org/codec/809/#comments</comments>
		<pubDate>Tue, 22 Nov 2011 03:10:00 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[canvas]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[text]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=809</guid>
		<description><![CDATA[HTML Canvas allows for the drawing of text with ctx.fillStyle = &#34;666&#34;; // text color ctx.textAlign = &#34;center&#34;; // alignment ctx.font = &#34;normal 12px Helvetica Neue&#34;; // font ctx.fillText&#40;&#34;my Text, x , y); // draw Sometimes, you might want to draw a border around the text. Getting the textHeight should not present a problem, after [...]]]></description>
			<content:encoded><![CDATA[<p>HTML Canvas allows for the drawing of text with</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">ctx.<span style="color: #660066;">fillStyle</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;666&quot;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">// text color</span>
ctx.<span style="color: #660066;">textAlign</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;center&quot;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">// alignment</span>
ctx.<span style="color: #660066;">font</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;normal 12px Helvetica Neue&quot;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">// font</span>
ctx.<span style="color: #660066;">fillText</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;my Text, x , y);		// draw</span></pre></div></div>

<p>Sometimes, you might want to draw a border around the text. Getting the textHeight should not present a problem, after all we can set the font size directly. But textWidth is not fixed in that way, because it depends (nnnn) on the actual text that is being displayed.</p>
<p>ctx.<a href="https://developer.mozilla.org/en/Drawing_text_using_a_canvas#measureText">measureText</a> takes the text as an argument and returns an object with a <em>width</em> property, describing the width of the text.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> texttWidth <span style="color: #339933;">=</span> ctx.<span style="color: #660066;">measureText</span><span style="color: #009900;">&#40;</span>columnName<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">width</span><span style="color: #339933;">;</span></pre></div></div>

<p><em>width</em> is for now the only inhabitant of that object, but it would be a fair guess, that it might be more densely populate in the future.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/809/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Replacing HTML elements with jQuery&#8217;s replaceWith</title>
		<link>http://www.trembl.org/codec/806/</link>
		<comments>http://www.trembl.org/codec/806/#comments</comments>
		<pubDate>Wed, 28 Sep 2011 08:23:56 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[replace]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=806</guid>
		<description><![CDATA[Let&#8217;s say you are having some HTML elements that should only be enabled, if the browser sports certain features. By default, the element is disabled, like in the following example: &#60;div id=&#34;item&#34; class=&#34;disabled&#34;&#62;Open File...&#60;/div&#62; Next, we check in Javascript is this certain feature is supported. If it is, we replace the disabled element with an [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you are having some HTML elements that should only be enabled, if the browser sports certain features. By default, the element is disabled, like in the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;div</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;item&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;disabled&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Open File...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Next, we check in Javascript is this certain feature is supported. If it is, we <a href="http://api.jquery.com/replaceWith/">replace</a> the disabled element with an active version.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>isSupported<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#item'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replaceWith</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;a id=&quot;openFile&quot;&gt;Open File...&lt;/a&gt;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Which results in this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;a</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;item&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Open File...<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/a<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Simple, but effective.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/806/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Freezing Objects and Arrays in Javascript</title>
		<link>http://www.trembl.org/codec/804/</link>
		<comments>http://www.trembl.org/codec/804/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 07:50:15 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[freeze]]></category>
		<category><![CDATA[isFrozen]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[object]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=804</guid>
		<description><![CDATA[Since the ECMA-262 specifications (aka Javascript 1.8.5 aka ECMAScript 5th Edition) it is possible prevent Objects from accepting any changes to their properties. After applying Object.freeze(myObj) it won&#8217;t be possible to change, add or remove properties from myObj. So far so good. As in Javascript Arrays also inherit from Object, I could see not reason, [...]]]></description>
			<content:encoded><![CDATA[<p>Since the ECMA-262 specifications (aka Javascript 1.8.5 aka ECMAScript 5th Edition) it is possible prevent Objects from accepting any changes to their properties. After applying <em>Object.freeze(myObj)</em> it won&#8217;t be possible to change, add or remove properties from <em>myObj</em>.</p>
<p>So far so good.</p>
<p>As in Javascript Arrays also inherit from Object, I could see not reason, why freezing an array should not work.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'a'</span><span style="color: #339933;">:</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'b:2'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Object.<span style="color: #660066;">freeze</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Object.<span style="color: #660066;">isFrozen</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       <span style="color: #006600; font-style: italic;">// returns true</span>
obj.<span style="color: #660066;">a</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>                 <span style="color: #006600; font-style: italic;">// new assignment has no affect</span>
obj.<span style="color: #660066;">a</span><span style="color: #339933;">;</span>                      <span style="color: #006600; font-style: italic;">// returns 1</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> arr <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: #009900;">&#93;</span><span style="color: #339933;">;</span>
Object.<span style="color: #660066;">freeze</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Object.<span style="color: #660066;">isFrozen</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>      <span style="color: #006600; font-style: italic;">// returns true</span>
arr<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;">10</span><span style="color: #339933;">;</span>
arr<span style="color: #339933;">;</span>                       <span style="color: #006600; font-style: italic;">// returns [10, 2] ... ouch!</span></pre></div></div>

<p>It turns out, that it seems to be an implementation bug in Safari (5.1). It&#8217;s working in the latest Firefox and Chrome releases.</p>
<p>To check whether freezing arrays works in your brower, click the following button:<br />
<button onClick="javascript:checkTheFreezer()">Check the Freezer!</button></p>
<p>ps. I asked the question first on Stack Overflow: <a href="http://stackoverflow.com/questions/7509894/freezing-arrays-in-javascript">&#8216;Freezing&#8217; Arrays in Javascript?</a>. Thanks for the constructive answers.</p>
<p><script type="text/javascript">
function checkTheFreezer() {
	var arr = ["icecream", "peas", "pizza"];
	var str = "Original Array: " + arr + "\n\n";
	Object.freeze(arr);
	str += "Array is frozen? " + Object.isFrozen(arr) + "\n";
	arr[0] = "hot cocoa";
	str += "Frozen Array: " + arr + "\n\n";
	if (arr[0]==="hot cocoa") {
		str += "Array freeze DOES NOT work.";
	} else {
		str += "Array freeze works.";
	}
	alert(str);
}
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/804/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Objects and Key Array Length in Javascript</title>
		<link>http://www.trembl.org/codec/802/</link>
		<comments>http://www.trembl.org/codec/802/#comments</comments>
		<pubDate>Fri, 19 Aug 2011 06:59:45 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[ECMAScript 5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[object]]></category>
		<category><![CDATA[¥length]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=802</guid>
		<description><![CDATA[With arrays is trivial to get the length, with objects, the situations is slightly different. One approach would be to get all the keys and get the length of that array: Object.keys&#40;myObject&#41;.length; More details at Stackoverflow. This functionality is part of EMCAScript 5 specs.]]></description>
			<content:encoded><![CDATA[<p>With arrays is trivial to get the length, with objects, the situations is slightly different. One approach would be to get all the keys and get the length of that array:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Object.<span style="color: #660066;">keys</span><span style="color: #009900;">&#40;</span>myObject<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span></pre></div></div>

<p>More details at <a href="http://stackoverflow.com/questions/126100/how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascript">Stackoverflow</a>. This functionality is part of EMCAScript 5 specs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/802/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Filtering Arrays with ECMAScript 5 and filter()</title>
		<link>http://www.trembl.org/codec/785/</link>
		<comments>http://www.trembl.org/codec/785/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 03:27:56 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[ECMAScript 5]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[loop]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=785</guid>
		<description><![CDATA[Suppose you have an array of object: var array = &#91; &#123;a:13, b:false &#125;, &#123;a:7, b:true &#125;, &#123;a:78, b:true &#125; &#93; The old, ECMAScript 3 way of checking for objects, in which b is true would be a simple for-loop: var results = &#91;&#93;; for &#40;var i=0; i&#60;array.length; i++&#41; &#123; if &#40;array&#91;i&#93;.b&#41; &#123; results.push&#40;array&#91;i&#93;&#41;; &#125; [...]]]></description>
			<content:encoded><![CDATA[<p>Suppose you have an array of object:</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: #009900;">&#91;</span> 	<span style="color: #009900;">&#123;</span>a<span style="color: #339933;">:</span><span style="color: #CC0000;">13</span><span style="color: #339933;">,</span> b<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">false</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>a<span style="color: #339933;">:</span><span style="color: #CC0000;">7</span><span style="color: #339933;">,</span> b<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#123;</span>a<span style="color: #339933;">:</span><span style="color: #CC0000;">78</span><span style="color: #339933;">,</span> b<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span> <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#93;</span></pre></div></div>

<p>The old, ECMAScript 3 way of checking for objects, in which b is true would be a simple for-loop:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><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>array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">b</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		results.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Ok, that&#8217;s working, but not very elegant nor concise. Luckily, ECMAScript 5 offers a nicer way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> array.<span style="color: #660066;">filter</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: #000066; font-weight: bold;">item</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: #000066; font-weight: bold;">item</span>.<span style="color: #660066;">b</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">// if item.b is true</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/785/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sums and NaNs in Javascript</title>
		<link>http://www.trembl.org/codec/779/</link>
		<comments>http://www.trembl.org/codec/779/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 03:17:38 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[NaN]]></category>
		<category><![CDATA[sum]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=779</guid>
		<description><![CDATA[var array = &#91;132, 32, 14, 525, 52, 12, 52&#93;; var sum; for &#40;var i=0; i&#60;array.length; i++ &#41; &#123; sum += array&#91;i&#93;; console.log&#40;sum&#41;; &#125; I was slightly puzzled, why the above code outputs Nan instead of the consecutive sums of the array. var array = &#91;132, 32, 14, 525, 52, 12, 52&#93;; var sum = [...]]]></description>
			<content:encoded><![CDATA[
<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: #009900;">&#91;</span><span style="color: #CC0000;">132</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">32</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">525</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">52</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">52</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>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	sum <span style="color: #339933;">+=</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I was slightly puzzled, why the above code outputs <em>Nan</em> instead of the consecutive sums of the array.</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: #009900;">&#91;</span><span style="color: #CC0000;">132</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">32</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">14</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">525</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">52</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">12</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">52</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> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// sum needs to be init...?</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>array.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	sum <span style="color: #339933;">+=</span> array<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>After declaring sum with a number, it works. Still have to find out why.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/779/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Javascript, Array and For-Loops</title>
		<link>http://www.trembl.org/codec/749/</link>
		<comments>http://www.trembl.org/codec/749/#comments</comments>
		<pubDate>Fri, 13 May 2011 08:53:28 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[for]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[loop]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=749</guid>
		<description><![CDATA[Ok, looping over an array is something that we do all the time, for most of us it has become a second nature. var myArray = &#91;&#34;a&#34;, &#34;b&#34;, &#34;c&#34;, &#34;z&#34;&#93;; &#160; for &#40;int i=0; i&#60;myArray.length; i++&#41; &#123; console.log&#40;myArray&#91;i&#93;&#41;; &#125; But I recently came across a Javascript loop that puzzled me slightly. var myArray = &#91;&#34;a&#34;, [...]]]></description>
			<content:encoded><![CDATA[<p>Ok, looping over an array is something that we do all the time, for most of us it has become a second nature.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</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: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;z&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>int i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>myArray.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>myArray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But I recently came across a Javascript loop that puzzled me slightly.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</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: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;z&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>f <span style="color: #339933;">;</span> f<span style="color: #339933;">=</span>myArray<span style="color: #009900;">&#91;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Let&#8217;s have a closer look what&#8217;s happening here. As a reminder, For-statements have the following structure:<br />
<em>for (initiation; condition; increment) { block } </em></p>
<p>In the initiation, variables are initiated; Condition tests this var, and if it returns true, continues another loop; and here&#8217;s something funny; the increment is left blank. Don&#8217;t panic, because we are already incrementing i at the condition test.</p>
<p>This is equivalent:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</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: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;z&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>f <span style="color: #339933;">;</span> f<span style="color: #339933;">=</span>myArray<span style="color: #009900;">&#91;</span> i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This, however, is not the same:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</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: #339933;">,</span> <span style="color: #3366CC;">&quot;c&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;z&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>f <span style="color: #339933;">;</span> f<span style="color: #339933;">=</span>myArray<span style="color: #009900;">&#91;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>i gets incremented <em>before</em> it is used to traverse myArray, therefor resulting in this output:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">b
c
z</pre></div></div>

<p>In nice and simple arrays like in this example, it should not make any difference to test the array length at every time coming around, but a larger ones it might be beneficial.</p>
<p>As usual, take care that the array does not contain any falsy objects. And don&#8217;t loop an object that way.</p>
<p>Thanks to the Re-introduction to Javascript <a href="https://developer.mozilla.org/en/a_re-introduction_to_javascript">https://developer.mozilla.org/en/a_re-introduction_to_javascript</a>  for clarifying that issue for me.</p>
<div class="update">Update</div>
<p>Turns out things are not so clear after all. Let&#8217;s consider the following case: I have an array of numbers, I&#8217;d like to get the sum of all numbers in the array.<br />
One way to do it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray <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: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> sum<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;</span>this.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	sum <span style="color: #339933;">+=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">// 18</span></pre></div></div>

<p>So, shouldn&#8217;t this work too&#8230;?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myArray <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: #339933;">,</span><span style="color: #CC0000;">5</span><span style="color: #339933;">,</span><span style="color: #CC0000;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> sum<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> sum <span style="color: #339933;">=</span> myArray<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>sum<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		<span style="color: #006600; font-style: italic;">// undefined</span></pre></div></div>

<p>Apparently not&#8230; I am trying to find out why not.</p>
<p>This, on the other hand works:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> sum<span style="color: #339933;">=</span><span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span> sum<span style="color: #339933;">+=</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#91;</span>i<span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/749/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Processing Math convenience methods in pure Javascript</title>
		<link>http://www.trembl.org/codec/737/</link>
		<comments>http://www.trembl.org/codec/737/#comments</comments>
		<pubDate>Mon, 25 Apr 2011 08:18:01 +0000</pubDate>
		<dc:creator>Georg Tremmel</dc:creator>
				<category><![CDATA[Raw]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[port]]></category>
		<category><![CDATA[Processing]]></category>

		<guid isPermaLink="false">http://www.trembl.org/codec/?p=737</guid>
		<description><![CDATA[Like everybody else, I am huge fan of Processing, and like everybody else I find myself working more and more with Javascript. There is a implementation port of Processing (aptly called ProcessingJS) by John Resig of jQuery fame. It&#8217;s still lacking the 3D features, but not because they would be hard to implement in JS, [...]]]></description>
			<content:encoded><![CDATA[<p>Like everybody else, I am huge fan of <a href="http://processing.org/">Processing</a>, and like everybody else I find myself working more and more with Javascript.</p>
<p>There is a implementation port of Processing (aptly called <a href="http://processingjs.org/">ProcessingJS</a>) by <a href="http://ejohn.org/">John Resig</a> of <a href="http://jquery.com/">jQuery</a> fame. It&#8217;s still lacking the 3D features, but not because they would be hard to implement in JS, but rather the OpenGL bindings in the browsers don&#8217;t exists wide-spreadly (yet). Let&#8217;s hope WebGL is going to change that soon.</p>
<p>After working a lot with Processing, one becomes quite spoilt with it&#8217;s methods. The two best kept secrets of Processing are on the one hand the matrix state operations &#8211; pushMatrix(), and popMatrix() &#8211; and on the other hand the very handy map() method, basically mapping a number from one range to another.</p>
<p>In Javascript&#8217;s canvas you have the pushMatrix() popMatrix() equivalents as save() and restore(). The map() methods does not exists as such, but it is easy implemented. I took a look at the Processing source and rewrote the Java methods in Javascript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> p5 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
p5.<span style="color: #660066;">sq</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">*</span>a<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
p5.<span style="color: #660066;">constrain</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>amt<span style="color: #339933;">,</span> low<span style="color: #339933;">,</span> high<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>amt <span style="color: #339933;">&lt;</span> low<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> low <span style="color: #339933;">:</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>amt <span style="color: #339933;">&gt;</span> high<span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> high <span style="color: #339933;">:</span> amt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
p5.<span style="color: #660066;">degrees</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>radians<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> radians <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">180</span><span style="color: #339933;">/</span>Math.<span style="color: #660066;">PI</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
p5.<span style="color: #660066;">mag</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">,</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>a<span style="color: #339933;">*</span>a <span style="color: #339933;">+</span> b<span style="color: #339933;">*</span>b<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
p5.<span style="color: #660066;">dist</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>x1<span style="color: #339933;">,</span> y1<span style="color: #339933;">,</span> x2<span style="color: #339933;">,</span> y2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>p5.<span style="color: #660066;">sq</span><span style="color: #009900;">&#40;</span>x2<span style="color: #339933;">-</span>x1<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> p5.<span style="color: #660066;">sq</span><span style="color: #009900;">&#40;</span>y2<span style="color: #339933;">-</span>y1<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
p5.<span style="color: #660066;">lerp</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>start<span style="color: #339933;">,</span> <span style="color: #000066;">stop</span><span style="color: #339933;">,</span> amt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> start <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>stop<span style="color: #339933;">-</span>start<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> amt<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
p5.<span style="color: #660066;">norm</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> start<span style="color: #339933;">,</span> <span style="color: #000066;">stop</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: #009900;">&#40;</span>value <span style="color: #339933;">-</span> start<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000066;">stop</span> <span style="color: #339933;">-</span> start<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
p5.<span style="color: #660066;">map</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> istart<span style="color: #339933;">,</span> istop<span style="color: #339933;">,</span> ostart<span style="color: #339933;">,</span> ostop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> ostart <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>ostop <span style="color: #339933;">-</span> ostart<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>value <span style="color: #339933;">-</span> istart<span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span>istop <span style="color: #339933;">-</span> istart<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>The code including comments and a minified version is also up on GitHub, show you love and fork it.<br />
<a href="https://github.com/trembl/p5.Math.js/">https://github.com/trembl/p5.Math.js/</a></p>
<div class="update">Update &#8211; Extending Math</div>
<p>Rather than creating a dedicated object, and as all the function are kind of related to Math, I thought it would be a good idea to extend the Math object itself. (Well, it&#8217;s a good idea for my project &#8211; your mileage, especially if you are flying with a lot of libraries might vary).</p>
<p>And how to you extend a Javascript build-in object? With prototype, right?</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">prototpe</span>.<span style="color: #660066;">sq</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">*</span>a<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wrong. Because Math does not have a constructor, it therefore hasn&#8217;t been constructed and therefore does not have the prototype method. It has been <strong>instantiated</strong> and you add methods (or functions?) like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">Math.<span style="color: #660066;">sq</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">*</span>a<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You get the idea. The new code also lives at GitHub, check it out. <a href="https://github.com/trembl/p5.Math.js/">https://github.com/trembl/p5.Math.js/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.trembl.org/codec/737/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

