<?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; jQuery</title>
	<atom:link href="http://davidbcalhoun.com/tag/jquery/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>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>
	</channel>
</rss>
