<?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>David B. Calhoun - Developer Blog &#187; javascript</title>
	<atom:link href="http://davidbcalhoun.com/tag/javascript/feed" rel="self" type="application/rss+xml" />
	<link>http://davidbcalhoun.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 08 Sep 2010 18:28:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Thursday links (July 8)</title>
		<link>http://davidbcalhoun.com/2010/thursday-links-july-8</link>
		<comments>http://davidbcalhoun.com/2010/thursday-links-july-8#comments</comments>
		<pubDate>Fri, 09 Jul 2010 04:00:09 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[links]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=338</guid>
		<description><![CDATA[Mobile YouTube Mobile Gets a Kick Start Battle of Champions: HTC Droid Incredible vs. Palm Pre Plus Designing for the Retina Display (326ppi) AUDIO: John Resig: You Don’t Know Mobile (Webstyle Magazine) &#8211; ~$5,000 minimum to do mobile testing Tapworthy: Designing Great iPhone Apps eMobile: We have the fastest network in Japan! Apple iPad User [...]]]></description>
			<content:encoded><![CDATA[<h3>Mobile</h3>
<p><a href="http://googlemobile.blogspot.com/2010/07/youtube-mobile-gets-kick-start.html">YouTube Mobile Gets a Kick Start</a><br />
<a href="http://blog.laptopmag.com/htc-droid-incredible-vs-palm-pre-plus">Battle of Champions: HTC Droid Incredible vs. Palm Pre Plus</a><br />
<a href="http://www.lukew.com/ff/entry.asp?1142">Designing for the Retina Display (326ppi)</a><br />
<a href="http://webstylemag.com/you-dont-know-mobile" class="audio">AUDIO: John Resig: You Don’t Know Mobile (Webstyle Magazine)</a> &#8211; ~$5,000 minimum to do mobile testing<br />
<a href="http://www.amazon.com/Tapworthy-Designing-Great-iPhone-Apps/dp/1449381650">Tapworthy: Designing Great iPhone Apps</a><br />
<a href="http://www.intomobile.com/2010/07/08/emobile-we-have-the-fastest-network-in-japan/">eMobile: We have the fastest network in Japan!</a><br />
<a href="http://ymobileblog.com/blog/2010/07/08/apple-ipad-user-analysis-%E2%80%94-phase-ii/">Apple iPad User Analysis — Phase II</a><br />
<a href="http://www.youtube.com/watch?v=6_TFHqIHBqM" class="video">VIDEO: Using iPhone with a Braille display (Victor Tsaran)</a><br />
<a href="http://www.pewinternet.org/Reports/2010/Mobile-Access-2010.aspx">Mobile Access 2010 (Pew Research)</a><br />
<a href="http://www.prnewswire.com/news-releases/comscore-reports-may-2010-us-mobile-subscriber-market-share-98031904.html">BlackBerry and iPhone losing ground to Android, overall smartphone growth (comScore data)</a></p>
<h3>JavaScript</h3>
<p><a href="http://blog.mozilla.com/dherman/2010/07/08/javascript-needs-modules/">JavaScript needs modules!</a><br />
<a href="http://www.dustindiaz.com/javascript-cache-provider/">JavaScript Cache Provider (Dustin Diaz)</a><br />
<a href="http://www.adequatelygood.com/2010/7/Writing-Testable-JavaScript">Writing Testable JavaScript</a></p>
<h3>CSS</h3>
<p><a href="http://5by5.tv/bigwebshow/11" class="video">VIDEO: Nicole Sullivan on CSS (The Big Web Show)</a><br />
<a href="http://css-tricks.com/css-media-queries/">CSS Media Queries &#038; Using Available Space</a><br />
<a href="http://www.nczonline.net/blog/2010/07/06/data-uris-make-css-sprites-obsolete/">Data URIs make CSS sprites obsolete (Nicholas Zakas)</a></p>
<h3>Books</h3>
<p><a href="http://books.alistapart.com/product/html5-for-web-designers">HTML5 For Web Designers</a><br />
<a href="http://www.amazon.com/Tapworthy-Designing-Great-iPhone-Apps/dp/1449381650">Tapworthy: Designing Great iPhone Apps</a><br />
<a href="http://www.amazon.com/HTML5-Up-Running-Mark-Pilgrim/dp/0596806027">HTML5: Up and Running</a><br />
<a href="http://www.amazon.com/JavaScript-Patterns-Stoyan-Stefanov/dp/0596806752">JavaScript Patterns</a></p>
<h3>Etc.</h3>
<p><a href="http://web.enavu.com/snippets/web-forms-semantic-mark-up-in-our-forms-part-2/">Web Forms: Semantic Mark Up in our Forms [part 2]</a><br />
<a href="http://trentwalton.com/2010/07/05/non-hover/">Non Hover</a> &#8211; “Elements that rely only on mousemove, mouseover, mouseout or the CSS pseudo-class :hover may not always behave as expected on a touch-screen device such as iPad or iPhone.”<br />
<a href="http://www.xanthir.com/video/">Video to ASCII conversion with Canvas</a><br />
<a href="http://accessibiliteweb.com/stuff/captcha-slider.html">CAPTCHA slider</a><br />
<a href="http://www.google.com/support/webmasters/bin/topic.py?topic=21997">Rich snippets and structured markup (Google Webmaster Central) (SEO)</a><br />
<a href="http://hacks.mozilla.org/2010/07/firefox-4-beta-1-is-here-whats-in-it-for-web-developers/">Firefox 4 beta 1 is here – what’s in it for web developers?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2010/thursday-links-july-8/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using mobile-specific HTML, CSS, and JavaScript (Mobile web part 5)</title>
		<link>http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript</link>
		<comments>http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript#comments</comments>
		<pubDate>Wed, 30 Jun 2010 03:27:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[mobile]]></category>
		<category><![CDATA[basejs]]></category>
		<category><![CDATA[blackberry.network]]></category>
		<category><![CDATA[blackberry.system]]></category>
		<category><![CDATA[capture api]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[gesture events]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[iui]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jqtouch]]></category>
		<category><![CDATA[media queries]]></category>
		<category><![CDATA[mozorientation]]></category>
		<category><![CDATA[orientationchange]]></category>
		<category><![CDATA[pastrykit]]></category>
		<category><![CDATA[touch events]]></category>
		<category><![CDATA[webkit]]></category>
		<category><![CDATA[xui]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=317</guid>
		<description><![CDATA[Mobile-specific HTML Use the viewport tag to properly fit the content to the screen: &#60;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no&#34;/&#62; Use the tel scheme for telephone numbers, allowing users to initiate calls by clicking: &#60;a href=&#34;tel:18005555555&#34;&#62;Call us at 1-800-555-5555&#60;/a&#62; Or use the sms scheme to initiate an SMS message: &#60;a href=&#34;sms:18005555555&#34;&#62; &#60;a href=&#34;sms:18005555555,18005555556&#34;&#62; &#60;!-- multiple [...]]]></description>
			<content:encoded><![CDATA[<h3>Mobile-specific HTML</h3>
<p>Use the <a href="http://davidbcalhoun.com/2010/viewport-metatag">viewport tag</a> to properly fit the content to the screen:</p>
<pre name="code" class="html">
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no&quot;/&gt;
</pre>
<p>Use the <a href="http://www.rfc-editor.org/rfc/rfc3966.txt">tel scheme</a> for telephone numbers, allowing users to initiate calls by clicking:</p>
<pre name="code" class="html">
&lt;a href=&quot;tel:18005555555&quot;&gt;Call us at 1-800-555-5555&lt;/a&gt;
</pre>
<p>Or use the <a href="http://www.rfc-editor.org/rfc/rfc5724.txt">sms scheme</a> to initiate an SMS message:</p>
<pre name="code" class="html">
&lt;a href=&quot;sms:18005555555&quot;&gt;
&lt;a href=&quot;sms:18005555555,18005555556&quot;&gt;          &lt;!-- multiple recipients --&gt;
&lt;a href=&quot;sms:18005555555?body=Text+goes+here&quot;&gt;  &lt;!-- predefined message body --&gt;
</pre>
<p>You also have access to several <a href="http://developer.apple.com/safari/library/documentation/appleapplications/reference/safariwebcontent/configuringwebapplications/configuringwebapplications.html">Apple-specific tags to use in your web application (iPhone and iPod Touch)</a>:</p>
<pre name="code" class="html">
// iOS 1.1.3+: this is the icon that's used when the user adds your app to the home screen
&lt;link rel=&quot;apple-touch-icon&quot; href=&quot;/custom_icon.png&quot;/&gt;

// iOS 3+: full-screen startup splash screen image
&lt;link rel=&quot;apple-touch-startup-image&quot; href=&quot;/startup.png&quot;&gt;

// enable full-screen mode
&lt;meta name=&quot;apple-mobile-web-app-capable&quot; content=&quot;yes&quot; /&gt;

// controls the appearance of the status bar in full-screen mode
&lt;meta name=&quot;apple-mobile-web-app-status-bar-style&quot; content=&quot;black&quot; /&gt;
</pre>
<p>And also some handy attributes to turn off annoying autocorrect features (works on iPhone, but anything else?):</p>
<pre name="code" class="html">
&lt;input autocorrect=&quot;off&quot; autocomplete=&quot;off&quot; autocapitalize=&quot;off&quot;&gt;
</pre>
<h3>Mobile-specific CSS</h3>
<p><a href="http://www.w3.org/TR/css3-mediaqueries/">Media queries</a> enable you to target specific features (screen width, orientation, resolution) within CSS itself.  You can use them two ways: 1) inline in a CSS stylesheet or 2) as the &#8220;media&#8221; attribute in the link tag, which targets an external stylesheet.  The following is an example of inline CSS that&#8217;s applied only when the device is in portrait mode:</p>
<pre name="code" class="css">
@media all and (orientation: portrait) {
	body { }
	div { }
}
</pre>
<p>Here&#8217;s the same media query using the other method, which points to an external stylesheet (not recommended, as it creates another network request):</p>
<pre name="code" class="html">
&lt;link rel=&quot;stylesheet&quot; media=&quot;all and (orientation: portrait)&quot; href=&quot;portrait.css&quot; /&gt;
</pre>
<p>Here&#8217;s a few examples:</p>
<pre name="code" class="css">
// target mobile devices
@media only screen and (max-device-width: 480px) {
	body { max-width: 100%; }
}

// recent Webkit-specific media query to target the iPhone 4's high-resolution Retina display
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
	// CSS goes here
}

// should technically achieve a similar result to the above query,
// targeting based on screen resolution (the iPhone 4 has 326 ppi/dpi)
@media only screen and (min-resolution: 300dpi) {
	// CSS goes here
}
</pre>
<p>Read more: <a href="https://developer.mozilla.org/en/css/media_queries">Media queries (Mozilla Developer Center)</a></p>
<p>(edit: removed section on <code>body[orient='portrait']</code>, which requires JavaScript and is in error.  See <a href="http://davidbcalhoun.com/2010/dealing-with-device-orientation">Part 6: Dealing with device orientation</a> for more details.)</p>
<h3>Mobile-specific JavaScript</h3>
<p>window.devicePixelRatio: determine screen resolution (analogue to the media query).  (iPhone 4 has the value 2, while Nexus One has the value 1.5).</p>
<p><a href="https://developer.mozilla.org/en/DOM/window.navigator.onLine">window.navigator.onLine</a> (boolean): not strictly mobile, but helpful for apps to determine if they&#8217;re being run offline</p>
<p><a href="http://developer.apple.com/safari/library/documentation/appleapplications/reference/safarihtmlref/articles/metatags.html">window.navigator.standalone</a> (boolean, iOS 2.1+): determine if it&#8217;s running in full-screen mode (Apple only?)</p>
<p><a href="http://developer.apple.com/safari/library/documentation/appleapplications/reference/safariwebcontent/handlingevents/handlingevents.html">touch events (iOS, Android 2.2+)</a>: touchstart, touchmove, touchend, touchcancel</p>
<p><a href="http://developer.apple.com/safari/library/documentation/internetweb/conceptual/safarivisualeffectsprogguide/InteractiveVisualEffects/InteractiveVisualEffects.html">gesture events (Apple only, iOS 2+)</a>: gesturestart, gesturechange, gesturend give access to predefined gestures (rotation, scale, position)</p>
<p><a href="http://ajaxian.com/archives/iphone-windowonorientationchange-code">window.orientation and orientationchange event</a>: triggered every 90 degrees of rotation (portrait and landscape modes)</p>
<p><a href="https://developer.mozilla.org/en/Detecting_device_orientation">MozOrientation</a> (Fennec/Firefox Mobile, Firefox 3.5+): also not strictly mobile.  Gives access to the device&#8217;s accelerometer (x-y-z orientation data), updated periodically.  Works on Android phones (I tested on the Nexus One), Windows Mobile, etc.  On the desktop this works with laptops such as Thinkpads and MacBooks.</p>
<p>-near future: camera/microphone access in Android 2.3 (announced at Google I/O 2010) through the <a href="http://www.w3.org/TR/capture-api/">Capture API</a></p>
<p>If you&#8217;re developing for a BlackBerry Widget, you have access to proprietary information through the <a href="http://www.blackberry.com/developers/docs/widgetapi/Summary_system.html">blackberry object</a> (which gives access to useful information such as blackberry.network [returns values such as CDMA and Wi-Fi] and blackberry.system).</p>
<p>You also have the option to use <a href="http://phonegap.com/">PhoneGap</a>, which augments JavaScript and gives you access to more phone features that native apps would have access to.</p>
<h3>Use a mobile-optimized JavaScript library</h3>
<p>Because smartphone browsers are standards-based, the aim of a JavaScript library on mobile is less towards API normalization and more towards providing an actual UI framework, usually to emulate the feel of native apps (and to provide easier workarounds to <a href="http://doctyper.com/archives/200808/fixed-positioning-on-mobile-safari/">lack of access to position:fixed</a>).  We&#8217;ve seen a few libraries released that emulate the iPhone UI, and in the future we might see libraries emulating the Android UI, as well as entirely new UIs.</p>
<p>There&#8217;s also a bit to be said about simply loading full desktop JavaScript libraries into mobile clients.  In my opinion this doesn&#8217;t particularly make sense, especially in a world where latency and bandwidth are so much more of a concern.  It doesn&#8217;t make sense to force the user wait longer and download code that&#8217;s ultimately useless to them (hacks for desktop browsers such as IE 6, etc).</p>
<p>Here&#8217;s a few of the mobile libraries now available:<br />
-<a href="http://www.sencha.com/">Sencha Touch</a><br />
-<a href="http://paularmstrongdesigns.com/projects/basejs/">baseJS</a><br />
-<a href="http://xuijs.com/">XUI</a><br />
-<a href="http://www.jqtouch.com/">jQTouch</a>: jQuery-style mobile library that emulates the look of the iPhone UI<br />
-<a href="http://code.google.com/p/iui/">iUI</a>: the first JavaScript library to be released, emulates the look of the iPhone UI.  Originally coded by <a href="http://www.joehewitt.com/">Joe Hewitt</a>.<br />
-<a href="http://davidbcalhoun.com/2009/pastrykit-digging-into-an-apple-pie">PastryKit</a>: Apple proprietary non-public JavaScript library with no available documentation</p>
<h3>Take advantage of new stuff!</h3>
<p>While not specific to mobile, there&#8217;s a lot of new stuff in general that you can use.  If you limit yourself to the top smartphones (iPhone, Android, and maybe webOS), compared to the desktop you immediately have access to an even wider array of new stuff, especially many Webkit proprietary features, since most of these top smartphones have browsers based on Webkit.</p>
<p>-HTML: new tags (<a href="http://diveintohtml5.org/">HTML5</a> (I&#8217;m sure you&#8217;ve heard of it by now…))<br />
-CSS: <a href="http://webkit.org/blog/130/css-transforms/">2d transforms</a>, <a href="http://webkit.org/blog/386/3d-transforms/">3d transforms</a>, animation, <a href="http://border-radius.com/">border radius</a>, <a href="https://developer.mozilla.org/en/css/@font-face">custom fonts with @font-face</a>, etc.<br />
-JavaScript: <a href="http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/">strict mode</a>, <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Core_Language_Features#Constants">constants</a>, <a href="http://davidbcalhoun.com/2009/javascript-tidbit-block-scope-with-let">block scope</a>, <a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Date">Date.now()</a>, etc.</p>
<h3>Related posts</h3>
<p>This post is part of a series on the mobile web.  You can read the other parts of the series here:<br />
<a href="http://davidbcalhoun.com/2010/viewport-metatag">Part 1: The viewport metatag</a><br />
<a href="http://davidbcalhoun.com/2010/the-mobile-developers-toolkit-mobile-web-part-2">Part 2: The mobile developer’s toolkit</a><br />
<a href="http://davidbcalhoun.com/2010/designing-buttons-that-dont-suck">Part 3: Designing buttons that don’t suck</a><br />
<a href="http://davidbcalhoun.com/2010/on-designing-a-mobile-webpage">Part 4: On designing a mobile webpage</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2010/using-mobile-specific-html-css-javascript/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>JavaScript SunSpider: HTC Evo versus HTC Incredible versus Nexus One</title>
		<link>http://davidbcalhoun.com/2010/javascript-sunspider-htc-evo-versus-htc-incredible-versus-nexus-one</link>
		<comments>http://davidbcalhoun.com/2010/javascript-sunspider-htc-evo-versus-htc-incredible-versus-nexus-one#comments</comments>
		<pubDate>Fri, 11 Jun 2010 05:07:11 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[performance]]></category>
		<category><![CDATA[htc evo]]></category>
		<category><![CDATA[htc incredible]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[mobile]]></category>
		<category><![CDATA[nexus one]]></category>
		<category><![CDATA[sunspider]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=292</guid>
		<description><![CDATA[Result table Test Evo (2.1) Incredible (2.1) Nexus One (2.2) Total 16167ms 15237ms 5452ms 3D 2014ms 1835ms 946ms Access 2126ms 1892ms 463ms Bitops 1519ms 1591ms 360ms Controlflow 243ms 206ms 20ms Crypto 1033ms 1003ms 344ms Date 1849ms 1896ms 639ms Math 1684ms 1419ms 602ms Regexp 1779ms 1673ms 155ms String 3920ms 3722ms 1923ms Thoughts The Incredible is just [...]]]></description>
			<content:encoded><![CDATA[<h3>Result table</h3>
<table>
<thead>
<tr>
<td>Test</td>
<td>Evo (2.1)</td>
<td>Incredible (2.1)</td>
<td>Nexus One (2.2)</td>
</tr>
</thead>
<tfoot></tfoot>
<tbody>
<tr>
<td>Total</td>
<td>16167ms</td>
<td>15237ms</td>
<td class="positive">5452ms</td>
</tr>
<tr>
<td>3D</td>
<td>2014ms</td>
<td>1835ms</td>
<td class="positive">946ms</td>
</tr>
<tr>
<td>Access</td>
<td>2126ms</td>
<td>1892ms</td>
<td class="positive">463ms</td>
</tr>
<tr>
<td>Bitops</td>
<td>1519ms</td>
<td>1591ms</td>
<td class="positive">360ms</td>
</tr>
<tr>
<td>Controlflow</td>
<td>243ms</td>
<td>206ms</td>
<td class="positive">20ms</td>
</tr>
<tr>
<td>Crypto</td>
<td>1033ms</td>
<td>1003ms</td>
<td class="positive">344ms</td>
</tr>
<tr>
<td>Date</td>
<td>1849ms</td>
<td>1896ms</td>
<td class="positive">639ms</td>
</tr>
<tr>
<td>Math</td>
<td>1684ms</td>
<td>1419ms</td>
<td class="positive">602ms</td>
</tr>
<tr>
<td>Regexp</td>
<td>1779ms</td>
<td>1673ms</td>
<td class="positive">155ms</td>
</tr>
<tr>
<td>String</td>
<td>3920ms</td>
<td>3722ms</td>
<td class="positive">1923ms</td>
</tr>
</tbody>
</table>
<h3>Thoughts</h3>
<p>The Incredible is just slightly faster than the Evo, to the point where it&#8217;s probably negligible or within a margin of error.  Both of these phones run on Android 2.1 with HTC&#8217;s Sense UI modifications, and represent the latest and greatest in Android phones available on the market today.  Both run on the same 1GHz Snapdragon processor (QSD8650). The Nexus One is a bit older, and runs with an older version of the Snapdragon processor (QSD8250), however it still runs at 1GHz just like the other two phones.</p>
<p>As you can see the Nexus One blows away all the competition because it&#8217;s running Android 2.2 Froyo.  These results were quite a shock to me and are quite impressive.  These results even blow away Apple&#8217;s new iOS 4 running on my iPhone 3GS, which clocked in at a total time of 13787ms compared to the Nexus One&#8217;s startling 5452ms.</p>
<h3>Testing methodology</h3>
<p>Test: <a href="http://www2.webkit.org/perf/sunspider-0.9.1/sunspider-0.9.1/driver.html">SunSpider 0.9.1</a></p>
<p>Devices: HTC Evo (Android 2.1), HTC Incredible (Android 2.1), HTC Nexus One (Android 2.2)</p>
<p>The SunSpider test was run five times on each phone.  The phone was completely turned off and on before each test.  The most extreme values of the five tests were thrown out, and the resulting four tests were averaged (sometimes from three tests when the values were very close together).</p>
<p>Raw results:</p>
<p><a href="http://davidbcalhoun.com/wp-content/uploads/2010/sunspider-htc-evo.html">SunSpider HTC Evo results (5 tests)</a></p>
<p><a href="http://davidbcalhoun.com/wp-content/uploads/2010/sunspider-htc-incredible.html">SunSpider HTC Incredible results (5 tests)</a></p>
<p><a href="http://davidbcalhoun.com/wp-content/uploads/2010/sunspider-nexus-one.html">SunSpider HTC Nexus One results (5 tests)</a></p>
<h3>Related links</h3>
<p><a href="http://davidbcalhoun.com/2010/sunspider-ios-3-1-3-versus-ios-4-gm">JavaScript SunSpider test: iOS 3.1.3 versus iOS 4 GM</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2010/javascript-sunspider-htc-evo-versus-htc-incredible-versus-nexus-one/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Video: John Resig &#8211; Testing, Performance Analysis, and jQuery 1.4</title>
		<link>http://davidbcalhoun.com/2009/video-john-resig-testing-performance-analysis-and-jquery-1-4</link>
		<comments>http://davidbcalhoun.com/2009/video-john-resig-testing-performance-analysis-and-jquery-1-4#comments</comments>
		<pubDate>Tue, 22 Dec 2009 03:54:01 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[john resig]]></category>
		<category><![CDATA[yahoo]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=78</guid>
		<description><![CDATA[In case you hadn&#8217;t seen it yet, John Resig was kind enough to stop by Yahoo! for the December Bayjax meetup. Here&#8217;s the video: Usually developers are more interested in just getting the dang code to work, and as a result actual the testing and maintenance of JavaScript isn&#8217;t talked about too much, so I&#8217;m [...]]]></description>
			<content:encoded><![CDATA[<p>In case you hadn&#8217;t seen it yet, <a href="http://ejohn.org/">John Resig</a> was kind enough to stop by Yahoo! for the December <a href="http://www.meetup.com/BayJax/">Bayjax</a> meetup.  Here&#8217;s the <a href="http://developer.yahoo.com/yui/theater/video.php?v=resig-testing">video</a>:</p>
<div class="video" style="width:576px; height: 324px;"><object width="576" height="324"><param name="movie" value="http://d.yimg.com/m/up/ypp/default/player.swf"></param><param name="flashVars" value="vid=17156941&#038;"></param><param name="allowfullscreen" value="true"></param><param name="wmode" value="transparent"></param><embed width="576" height="324" allowFullScreen="true" src="http://d.yimg.com/m/up/ypp/default/player.swf" type="application/x-shockwave-flash" flashvars="vid=17156941&#038;"></embed></object></div>
<p>Usually developers are more interested in just getting the dang code to work, and as a result actual the testing and maintenance of JavaScript isn&#8217;t talked about too much, so I&#8217;m sure this will be new territory for many developers.  And since it&#8217;s John Resig speaking, there was of course a bit about using <a href="http://testswarm.com/">TestSwarm</a>, a testing distributed framework-agnostic automated testing tool (that&#8217;s a mouthful!).</p>
<p>Included in the talk are good things to note while testing, such as the fact unless you&#8217;re running Firefox or Chrome on Windows, all test times have a margin of error of up to 15ms (not to be confused with <a href="http://www.quirksmode.org/blog/archives/2009/08/when_to_read_ou.html">PPK&#8217;s observation of the delay between JavaScript computation and browser rendering</a>).</p>
<p>(via <a href="http://www.yuiblog.com/blog/2009/12/16/video-resig-testing-and-jquery/">YUIBlog</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2009/video-john-resig-testing-performance-analysis-and-jquery-1-4/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript tidbit: special variables ($, $$, _, etc)</title>
		<link>http://davidbcalhoun.com/2009/javascript-tidbit-special-variables-_-etc</link>
		<comments>http://davidbcalhoun.com/2009/javascript-tidbit-special-variables-_-etc#comments</comments>
		<pubDate>Tue, 08 Dec 2009 11:24:54 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[$]]></category>
		<category><![CDATA[$$]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[_]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=61</guid>
		<description><![CDATA[You&#8217;re probably used to typical variables names such as the following: var personName = 'Joe'; You may not realize it, but there are some non-alphanumeric variables at your disposal. Using $ For instance, the $ variable has been made popular by several JavaScript libraries, most notably jQuery. You can use it to alias operations that [...]]]></description>
			<content:encoded><![CDATA[<p>You&#8217;re probably used to typical variables names such as the following:</p>
<pre name="code" class="JScript">var personName = 'Joe';</pre>
<p>You may not realize it, but there are some non-alphanumeric variables at your disposal.</p>
<h3>Using $</h3>
<p>For instance, the $ variable has been made popular by several JavaScript libraries, most notably <a href="http://jquery.com/">jQuery</a>.  You can use it to alias operations that are commonly performed, such as the following (1):</p>
<pre name="code" class="JScript">var $ = document.getElementById;
var myElement = $('targetElement');</pre>
<p>If you declare this variable outside of a function it will be a global variable and will compete with libraries that use the same global variable, so it&#8217;s probably best not to use it.</p>
<p>Interestingly, originally the dollar sign $ was originally intended for &#8220;mechanically generated code&#8221; (2), but as the usage of the symbol has become popular for other purposes, it looks like <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">the latest version of JavaScript (ECMAScript 5th edition) now officially &#8220;oks&#8221; its use</a>:</p>
<blockquote><p>This standard specifies specific character additions: The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.</p></blockquote>
<h3>Using $$</h3>
<p>Some have come up with the solution of simply using two or more $$ symbols in order to distinguish the variable from libraries that just use a single $:</p>
<pre name="code" class="JScript">var $$ = document.getElementById;
var myElement = $$('targetElement');</pre>
<h3>Using _</h3>
<p>You will find that you can use the underscore _ in the same way to alias variables and functions:</p>
<pre name="code" class="JScript">var _ = document.getElementById;
var myElement = _('targetElement');</pre>
<h3>Other symbols</h3>
<p>If you&#8217;re really getting adventurous, you can even try using other symbols such as square root √, which seems to work just fine, just as $ and _ above.  The only problem: it&#8217;s quite inconvenient using it, since it&#8217;s not available on any keyboards (except through some crazy key combinations perhaps).</p>
<p>Or you can put the symbol to use doing what you would naturally think it should do&#8230;</p>
<pre name="code" class="JScript">var √ = Math.sqrt;
alert(√(4));   // 2</pre>
<h3>References</h3>
<p>(1) <a href="http://www.amazon.com/Even-Faster-Web-Sites-Performance/dp/0596522304">Even Faster Web Sites</a>, p. 128<br />
(2) <a href="http://stackoverflow.com/questions/205853/why-would-a-javascript-variable-start-with-a-dollar-sign">Stackoverflow: Why would a javascript variable start with a dollar sign?</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2009/javascript-tidbit-special-variables-_-etc/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ways of passing data to functions in JavaScript</title>
		<link>http://davidbcalhoun.com/2009/passing-data-to-functions-in-javascript</link>
		<comments>http://davidbcalhoun.com/2009/passing-data-to-functions-in-javascript#comments</comments>
		<pubDate>Sat, 05 Dec 2009 03:41:48 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[beginner]]></category>
		<category><![CDATA[functions]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=51</guid>
		<description><![CDATA[Passing data is quite important in functional programming languages like JavaScript. When there are multiple functions (which is most of the time), there needs to be a way to pass data between the functions. This is done by passing values in parenthesis: myFunction(myData). Even when there is no data to be passed, we still have [...]]]></description>
			<content:encoded><![CDATA[<p>Passing data is quite important in functional programming languages like JavaScript.  When there are multiple functions (which is most of the time), there needs to be a way to pass data between the functions.  This is done by passing values in parenthesis: myFunction(myData).  Even when there is no data to be passed, we still have to declare and execute functions by using parenthesis: myFunction().</p>
<h3>Simple Passing</h3>
<p>I&#8217;ve already referred to the common method of passing data.  Here&#8217;s an example of the code in action:</p>
<pre name="code" class="JScript">function greeting (name) {
    alert('Hello ' + name);
}
var personsName = 'Joe';
greeting(personsName);  // Hello Joe</pre>
<p>This example only passes one variable to the function.  Note that we can get rid of the variable personsName and make the string on the fly.  So the code above is equivalent to this:</p>
<pre name="code" class="JScript">function greeting (name) {
    alert('Hello ' + name);
}
greeting('Joe');  // Hello Joe</pre>
<p>Ok.  Now let&#8217;s suppose we have a function that accepts two variables:</p>
<pre name="code" class="JScript">function greeting (firstName, lastName) {
    alert('Hello ' + firstName + ' ' + lastName);
}
greeting('Joe', 'Schmoe');  // Hello Joe Schmoe</pre>
<p>This doesn&#8217;t look too hard.  But there&#8217;s a catch &#8211; the person writing the code has to remember the order of variables to pass in.  Suppose they call the function like this:</p>
<pre name="code" class="JScript">function greeting (firstName, lastName) {
    alert('Hello ' + firstName + ' ' + lastName);
}
greeting('Schmoe', 'Joe');  // Hello Schmoe Joe</pre>
<p>That&#8217;s no good!  This isn&#8217;t the result we want.  In the case of firstName and lastName the variable order isn&#8217;t too hard to remember, but in other cases the variables aren&#8217;t in any logical order, which can cause confusion.</p>
<h3>No constructor overloading</h3>
<p>Unlike other languages such as C++ and Java, JavaScript has no <a href="http://en.wikipedia.org/wiki/Method_overloading">constructor overloading</a>.  In fact, JavaScript is (for better or worse) not even strict about enforcing the number of variables passed in.  For example, the following code works fine and throws no errors, even though we&#8217;re not passing in a variable in the function call:</p>
<pre name="code" class="JScript">function test (someVar) {
    // do stuff
};
test();  // no errors!</pre>
<p>It&#8217;s only when you try to use someVar within the function that causes the problems.  Otherwise, everything&#8217;s working just fine and dandy.</p>
<p>What happens if we try the other case scenario: pass in more variables than the function expects?  No errors again:</p>
<pre name="code" class="JScript">function test () {
    // do stuff
};
test('testing 123');  // again, no errors</pre>
<p>And what happens if we try to declare a function with the same name?  No errors, but the previous function is overwritten (this is generally to be avoided of course):</p>
<pre name="code" class="JScript">function test () {
    alert('First function');
};
function test () {
    alert('Second function');
};
test();  // Second function</pre>
<p>Because JavaScript has no function overloading, all it takes to overwrite another function is to redeclare a function with the same name.  It doesn&#8217;t matter what sort of arguments they accept.</p>
<h3>Passing with the help of the arguments object</h3>
<p>As it turns out, JavaScript has a handy arguments object to access the arguments passed into each function:</p>
<pre name="code" class="JScript">function test () {
    alert(arguments[0]);    // testing 123
};
test('testing 123');</pre>
<p>In the above case the arguments object acts just as an array &#8211; it&#8217;s 0-indexed and can be used to access any arbitrary number of arguments.  But arguments also has an interesting and useful property: length.  Using arguments.length, we can traverse the array of arguments.  This is handy in cases where we might want to add an arbitrary number of elements together:</p>
<pre name="code" class="JScript">function add () {
    var sumTotal = 0;                        // initialize total to 0

    for(var i=0; i&lt;arguments.length; i++) {  // for each argument
        sumTotal += arguments[i];            // add current argument to total
    }

    alert(sumTotal);
};
add(2, 3, 4);  // 9
add(1, 1, 1, 1, 1, 1, 1, 1, 1);  // 9
</pre>
<h3>Passing an object</h3>
<p>JavaScript libraries such as <a href="http://developer.yahoo.com/yui/">YUI</a> have already learned that the variable order is a common nuisance and an opportunity to introduce errors, so they&#8217;ve come up with a solution: pass a single object to the function.  It ends up looking something like this:</p>
<pre name="code" class="JScript">function greeting (obj) {
    alert('Hello ' + obj.first + ' ' + obj.last);
}
var nameObject = {};
nameObject.first = 'Joe';
nameObject.last = 'Schmoe';
greeting(nameObject);  // Hello Joe Schmoe</pre>
<p>Now the variables become properties of a single object which is passed into the function.  And it doesn&#8217;t matter which order the properties are declared in, which is a great relief to the developer.</p>
<p>Note that we can simplify the above code:</p>
<pre name="code" class="JScript">function greeting (obj) {
    alert('Hello ' + obj.first + ' ' + obj.last);
}
var nameObject = {
    first: 'Joe',
    last: 'Schmoe'
};
greeting(nameObject);  // Hello Joe Schmoe</pre>
<p>And we can simplify even further:</p>
<pre name="code" class="JScript">function greeting (obj) {
    alert('Hello ' + obj.first + ' ' + obj.last);
}
greeting({
    first: 'Joe',
    last: 'Schmoe'
});  // Hello Joe Schmoe</pre>
<p>This is the form commonly used in these JavaScript libraries.  It&#8217;s easy to copy-and-paste example code, but it might not always be so obvious what&#8217;s going on behind the scenes.  You can see that just as we bypassed declaring a named variable in the Simple Passing model (with greeting(&#8221;)), here we use the same shortcut and bypass declaring a named object (with greeting({})).</p>
<h3>call() and apply()</h3>
<p>These two methods have different techniques for passing data to functions, but I&#8217;m going to have to hold off on them for now.  It&#8217;s a bit complicated, since they&#8217;re only used to execute methods (functions) in other object contexts.  But seeing as I need to explain object context to get into that, I&#8217;ll save that for a future entry!</p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2009/passing-data-to-functions-in-javascript/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pitfalls of declaring variables in JavaScript</title>
		<link>http://davidbcalhoun.com/2009/pitfalls-of-declaring-variables-in-javascript</link>
		<comments>http://davidbcalhoun.com/2009/pitfalls-of-declaring-variables-in-javascript#comments</comments>
		<pubDate>Thu, 19 Nov 2009 05:01:58 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=29</guid>
		<description><![CDATA[Introduction One of the things that&#8217;s always been confusing to me is that there are multiple ways to declare variables in JavaScript, and some ways are better than others. For the beginner programmer, just getting to code to work means complete success, but for the intermediate or advanced programmer, this is just the first step. [...]]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>One of the things that&#8217;s always been confusing to me is that there are multiple ways to declare variables in JavaScript, and some ways are better than others.  For the beginner programmer, just getting to code to work means complete success, but for the intermediate or advanced programmer, this is just the first step.  The next step is to clean up the code and make sure everything is written in the best way it could have been written.</p>
<p>Declaring variables seems like such a basic thing that one almost feels insulted reading about it.  Yes, this whole post is about declaring variables.  But not just this &#8211; it&#8217;s about declaring variables <em>the right way</em>. This means declaring variables in the scope they were intended to be declared in.  For the most part this means writing variables in the scope of a function, as opposed to the global namespace (in which everything becomes a property of the window object).</p>
<h3>Global variables can be declared in functions</h3>
<p>When I was starting to learn JavaScript, I read about global and functional scope and <em>wrongly</em> assumed that functions <em>completely</em> protected the variables declared inside.  As it turns out, there&#8217;s many ways to create global variables in JavaScript, and none of them throw up red flags or sound off bells and whistles (unfortunately).</p>
<p>Here&#8217;s a few ways to create global variables in JavaScript:</p>
<pre name="code" class="JScript">// Creating global variables outside of a function
window.global1 = 1;
global2 = 2;
var global3 = 3;

// Created global variables within a function
function test() {
    global4 = 4;
    window.global5 = 5;
};
test();</pre>
<p>You can confirm this for yourself by checking the values of the variables with alert().</p>
<h3>Good practice: always precede variables with var</h3>
<p>What I learned was that it was essentially good practice to always write variables preceded by the var keyword.  For variables declared outside of functions this has no effect &#8211; they are still global, but for variables within functions this ensures that they&#8217;re kept within their functional scope:</p>
<pre name="code" class="JScript">var global = null;
function test() {
    var local = null;
}
test();</pre>
<h3>Same name, but different variables</h3>
<p>And check this out &#8211; confusingly, you can create a global variable, then create a variable with the <em>same name</em> that&#8217;s functionally-scoped, yet a completely different variable!</p>
<pre name="code" class="JScript">var number = 0;

function test() {
    var number = 1;
}
test();

alert(number);  // 0</pre>
<p>When you precede a variable with the var keyword, you&#8217;re essentially saying you&#8217;re creating the variable for the first time.  So when you create &#8220;number&#8221; inside the function, no error is thrown because the variable is created in the function&#8217;s scope.  If you try to create another variable with the name &#8220;number&#8221; inside the function, you will get an error because it&#8217;s already been defined within the scope.</p>
<h3>There&#8217;s no such thing as magical protective parenthesis</h3>
<p>For some reason I was further mistaken into believing that extra parenthesis around an anonymous function magically protected the variables within:</p>
<pre name="code" class="JScript">(function() {
    global = null;
    var local = null;
})();</pre>
<p>Nope &#8211; as you can see, the global variable is still global since it&#8217;s not preceded by the var keyword.  All the extra parenthesis do is execute the function immediately.  That&#8217;s it.</p>
<h3>Shorthand for declaring multiple functionally-scoped variables</h3>
<p>The following code ensures each variable is functionally (locally) scoped, even though only the first variable is preceded by the var keyword:</p>
<pre name="code" class="JScript">// shorthand #1
function test() {
    var local1 = null,
        local2 = null;
}
test();

// shorthand #2
function test2() {
    var local1, local2;  // these are of type 'undefined' but they have functional scope!
}
test2();
</pre>
<h3>Multiple variable declaration equivalence gives different scope to each variable</h3>
<p>Today I learned another way to mistakenly create a global variable.  Trying to be fancy and declare multiple variables to be the same value, I mistakenly created a global variable (again, no bells and whistles went off to warn me, unfortunately):</p>
<pre name="code" class="JScript">function test() {
    var local = global = null;
}
test();</pre>
<p>This  sets both variables to &#8220;null&#8221; using the shortest code possible, but  unfortunately only the variable &#8220;local&#8221; is preceded by the var keyword,  so only that variable will be in its proper functional scope.  Variable  &#8220;global&#8221;, on the other hand, is&#8230; well&#8230;  global.  (thanks to <a href="http://stackoverflow.com/questions/1758576/multiple-left-hand-assignment-with-javascript/1758912#1758912">Crescent  Fish</a>)</p>
<p>You learn something new every day&#8230;</p>
<h3>Update 1</h3>
<p>If you want to check your code for &#8220;accidental global variables&#8221; you can use <a href="http://www.jslint.com/">JSLint</a> or you can even use Firebug (click on the Script tab, then click on &#8220;New watch expression&#8230;&#8221;, then type &#8220;window&#8221;, which will display all the properties of the window object &#8211; these are all global variables!).  Thanks to Nick for the Firebug tip.</p>
<h3>Update 2</h3>
<p>Just another quick note to remind you that after a variable has been initially declared, the scope will not change.  Which is why you can redefine variables, but have them keep their scope:</p>
<pre name="code" class="JScript">function test() {
    var local = null;
    local = 5;    // still keeps its local/functional scope
}
test();</pre>
<p>As you can see above, the variable &#8220;local&#8221; keeps its local scope, even though it&#8217;s redefined to be 5.  This is one of the reasons it&#8217;s recommended to declare all variables at the start of a function &#8211; to make sure the scope gets set correctly.</p>
<p>One final note: var should only be used when declaring a variable for the first time.  If you try to use var for the same variable name in the same scope, it will result in an error:</p>
<pre name="code" class="JScript">function test() {
    var local = null;
    var local = 5;    // throws an error!
}
test();</pre>
<h3>Update 3: Graphical representation</h3>
<div id="attachment_115" class="wp-caption alignleft" style="width: 710px"><a href="http://davidbcalhoun.com/wp-content/uploads/2009/11/js-scope.png"><img src="http://davidbcalhoun.com/wp-content/uploads/2009/11/js-scope.png" alt="Scope in JavaScript" title="Scope in JavaScript" width="700" class="size-full wp-image-115" /></a><p class="wp-caption-text">Scope in JavaScript</p></div>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2009/pitfalls-of-declaring-variables-in-javascript/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript Tidbit: Block scope with &#8220;let&#8221;</title>
		<link>http://davidbcalhoun.com/2009/javascript-tidbit-block-scope-with-let</link>
		<comments>http://davidbcalhoun.com/2009/javascript-tidbit-block-scope-with-let#comments</comments>
		<pubDate>Tue, 15 Sep 2009 08:06:49 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[1.7]]></category>
		<category><![CDATA[block scope]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[let]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=23</guid>
		<description><![CDATA[JavaScript has functional scope. Meaning that if you (properly) define variables within functions, those variables remain accessible only inside the function. Block scope, on the other hand, defines scope within a block of code, usually defined by braces. JavaScript now has block scope as of version 1.7, which means it&#8217;s available in these browsers: Firefox [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript has <strong>functional scope</strong>.  Meaning that if you (properly) define variables within functions, those variables remain accessible only inside the function.</p>
<p><strong>Block scope</strong>, on the other hand, defines scope within a block of code, usually defined by braces.  JavaScript now has block scope as of version 1.7, which means it&#8217;s available in these browsers:</p>
<ul>
<li>Firefox 2+</li>
</ul>
<p>Block scope is enabled in JavaScript with the use of &#8220;let&#8221;:</p>
<pre name="code" class="JScript">let(x=100) {
    alert(x);
};</pre>
<p>It also works perfectly well on one line, without the use of braces:</p>
<pre name="code" class="JScript">let(x=100) alert(x);</pre>
<p>Note that we can define global variables with the same name outside the block scope and the variables won&#8217;t interfere with each other:</p>
<pre name="code" class="JScript">x = 200;
let(x=100) alert(x);
alert(x);
// result: 100, 200
</pre>
<p>Note that there&#8217;s a slight caveat &#8211; not only is this not available in any version of IE, but it also requires a special script type declaration in order to work (at least for Firefox): <strong>type=&#8221;text/javascript;version=1.7&#8243;</strong></p>
<p>References:<br />
<a href=" http://en.wikipedia.org/wiki/JavaScript#Versions">JavaScript Versions</a><br />
<a href="http://www.youtube.com/watch?v=0LKDImgRfrg#t=32m2s">Video: Best Practices in Javascript Library Design (John Resig)</a><br />
<a href="https://developer.mozilla.org/en/New_in_JavaScript_1.7">New in JavaScript 1.7</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2009/javascript-tidbit-block-scope-with-let/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>JavaScript Tidbit: Shorthand Object Instantiation</title>
		<link>http://davidbcalhoun.com/2009/javascript-tidbit-shorthand-object-instantiation</link>
		<comments>http://davidbcalhoun.com/2009/javascript-tidbit-shorthand-object-instantiation#comments</comments>
		<pubDate>Mon, 14 Sep 2009 07:26:23 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[john resig]]></category>
		<category><![CDATA[OO]]></category>

		<guid isPermaLink="false">http://davidbcalhoun.com/?p=10</guid>
		<description><![CDATA[The skinny: here&#8217;s a neat little trick to reduce the amount of code users have to type to instantiate objects: function jQuery(str, con){ if ( window == this ) return new jQuery(str, con); // ... } new jQuery("#foo"); // this is now equivalent... jQuery("#foo"); // ...to this! Explanation: when jQuery(&#8220;#foo&#8221;) is typed, the function first [...]]]></description>
			<content:encoded><![CDATA[<p>The skinny: here&#8217;s a neat little trick to reduce the amount of code users have to type to instantiate objects:</p>
<pre name="code" class="js">function jQuery(str, con){
    if ( window == this )
        return new jQuery(str, con);
    // ...
}

new jQuery("#foo"); // this is now equivalent...
jQuery("#foo");     // ...to this!</pre>
<p>Explanation: when jQuery(&#8220;#foo&#8221;) is typed, the function first determines if the context in which it&#8217;s being called is the global object (window).  If it is, it returns an instantiation of itself.  When it&#8217;s instantiated, the context in which it&#8217;s being called is inside its own function, and NOT within the global object (window), thus it bypasses our little instantiation trick and moves on to execute any remaining code in the function.</p>
<p>From John Resig&#8217;s <a href="http://www.youtube.com/watch?v=0LKDImgRfrg#t=32m2s">Best Practices in Javascript Library Design</a></p>
]]></content:encoded>
			<wfw:commentRss>http://davidbcalhoun.com/2009/javascript-tidbit-shorthand-object-instantiation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
