<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" version="2.0">

<channel>
	<title>James Padolsey</title>
	
	<link>http://james.padolsey.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Tue, 09 Mar 2010 13:46:10 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/JamesPadolsey" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="jamespadolsey" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">JamesPadolsey</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2FJamesPadolsey" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FJamesPadolsey" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2FJamesPadolsey" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bloglines.com/sub/http://feeds.feedburner.com/JamesPadolsey" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2FJamesPadolsey" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2FJamesPadolsey" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2FJamesPadolsey" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><item>
		<title>Conferences should be free</title>
		<link>http://james.padolsey.com/general/conferences-should-be-free/</link>
		<comments>http://james.padolsey.com/general/conferences-should-be-free/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 10:17:16 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Conference]]></category>
		<category><![CDATA[Opinion]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1465</guid>
		<description><![CDATA[First, by &#8220;free&#8221;, I mean that they should only cost the attendees what they cost to set up in the first place. There should be no profit involved whatsoever. The prices of some recent and upcoming industry conferences have been truly astounding to me. I&#8230;]]></description>
			<content:encoded><![CDATA[<p>First, by &#8220;free&#8221;, I mean that they should only cost the attendees what they cost to set up in the first place. There should be no profit involved whatsoever. The prices of some recent and upcoming industry conferences have been truly astounding to me. I can&#8217;t quite understand how people can be so greedy as to think that the sharing of knowledge and networking with other like-minded people warrants some kind of additional compensation.</p>

<p>I like to look upon the web development/design industry and see it for its openness and its community atmosphere. The voluntary sharing of knowledge could not be more evident, but yet there is an elephant in the room when it comes to openness, and that elephant is made up of all these greedy individuals and companies that only want to capitalize off our good intentions and our willingness to share knowledge. </p>
<p>Carsonified, the organisers of such prestigious events as <abbr title="Future Of Web Apps">FOWA</abbr> and <abbr title="Future Of Web Design">FOWD</abbr>, are a good example of a company that runs highly priced events. As I see it, there are several costly factors when organising such events:</p>

<ul>
<li>Speakers &#8211; payment for the talk plus accommodation and travel</li>
<li>Venue hire</li>
<li>Insurance</li>
<li>Equipment hire</li>
<li>Event merchandise</li>
<li>Staff costs &#8211; labour/travel/accommodation</li>
<li>Minor extraneous costs (website, flyers, hosting, wireless etc.)</li>
</ul>

<p>Let&#8217;s look at the numbers.</p>

<p>Tickets to the <a href="http://futureofwebapps.com/">upcoming <em>FOWA</em> conference in Dublin</a> cost €296.45 (~£244 excl VAT). You can also <a href="http://futureofwebapps.com/sponsors/">sponsor the event</a> &#8212; it&#8217;ll set you back anywhere between £1500 and £15,000. Let&#8217;s be generous and say there are only going to be 400 attendees, &#8212; assuming fifty of those are early-birds, that amounts to £86,500, and let&#8217;s say that all sponsor spots are taken (~£65,000) &#8212; that brings it to a rough total of £150,000.</p>

<p>I&#8217;ve tried my best to gather some numbers for the costs:</p>

<span id="more-1465"></span>

<ul>
    <li>Speaker &#8211; £1000 (there are <a href="http://futureofwebapps.com/speakers/">six speakers</a> = <strong>£6000</strong>)</li>
    <li>Speaker accommodation &#8211; one night &#8211; standard 4 star hotel &#8211; £150 (x6 = <strong>£900</strong>)</li>
    <li>Speaker travel &#8211; probably from US &#8211; ~£600 (x6 = ~<strong>£3600</strong>)</li>
    <li>Venue hire &#8211; University College Dublin &#8211; ~<strong>£4300</strong> (<a href="http://www.ucd.ie/conferences/hirerates.html">src</a>)</li>
    <li>Event insurance &#8211; ~<strong>£5000</strong></li>
    <li>Equipment hire
        <ul>
            <li>A/V &#8211; speakers/microphones/projector hire &#8211; ~<strong>£2000</strong></li>
            <li>Stands / boards &#8211; ~<strong>£1000</strong></li>
        </ul>
    </li>
    <li>Event merchandise
        <ul>
            <li>300+ badges &#8211; ~<strong>£1000</strong></li>
            <li>300+ paper/pens to give away &#8211; ~<strong>£3000</strong></li>
            <li>Other random stuff &#8211; ~<strong>£1000</strong></li>
        </ul>
    </li>
    <li>Staff costs &#8211; labour/travel/accommodation &#8211; no idea &#8211; probably around ~<strong>£3000</strong></li>
    <li>Minor extraneous costs (website, flyers, hosting, wireless etc.) &#8211; ~<strong>£3000</strong></li>
    <li>Food for speakers &#8211; ~£75 per head &#8211; <strong>£450</strong></li>
    <li>Coffee/biscuits for attendees &#8211; ~£15 per head &#8211; <strong>~£6000</strong></li>
</ul>

<p>That all comes to just over £40,000. Let&#8217;s be kind and say the entire event cost <strong>£50,000</strong>. So, £150,000 minus £50,000 and corporate-tax (~22%) amounts to a probable <em><strong>£78,000 take-home profit</strong></em> for the event organisers. I don&#8217;t claim to know (with absolute certainty) how much profit they&#8217;re making, but given the costs outlined above I would be surprised if it&#8217;s considerably less.</p>

<p>So, we&#8217;ve established that this particular event probably makes a rather beefy profit from our good intentions. Let&#8217;s have a look at another upcoming conference, this time an &#8220;online&#8221; conference, i.e. you simply buy a ticket and watch it online. It costs $150 to &#8220;attend&#8221; this conference &#8212; &#8220;<a href="http://carsonified.com/online-conferences/css3/">The CSS3 Online Conference</a>&#8221; (showing on 22nd March). Since it&#8217;s online, the costs for the event organisers   are minimal:</p>

<ul>
  <li>Speakers &#8211; five of them &#8211; ~£5000</li>
    <li>Speakers &#8211; travel/accommodation/food &#8211; ~£3000</li>
    <li>A/V equipment &#8211; ~£400</li>
</ul>

<p>The amount of tickets that they can sell is unlimited since they&#8217;re not limited by seat space. Let&#8217;s assume 300 this time &#8211; that&#8217;s $45,000 (~£30,000). Again, a sizable profit is probably being made here.</p>

<p>Making so much profit is, at best, ethically questionable. I&#8217;m not going to make solid claims about certain event organisiers, but I will say that if they truly care about the qualities of the web industry that they promote through their events, then they should seriously consider lowering the attendence fee. Again, conferences like these are about sharing knowledge and ideas &#8212; why should anyone be looking for additional compensation?</p>

<p>Ideally, conferences like these should cost around £15 for attendees, just to ensure that they&#8217;re sufficiently committed, and then the rest of the conference&#8217;s costs could be covered by sponsors.</p>

<p><em>Profiting from good intentions is immoral.</em></p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/general/conferences-should-be-free/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>jQuery: Novice to Ninja</title>
		<link>http://james.padolsey.com/javascript/jquery-novice-to-ninja/</link>
		<comments>http://james.padolsey.com/javascript/jquery-novice-to-ninja/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 16:17:19 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Book]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Review]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1442</guid>
		<description><![CDATA[<a href="http://www.sitepoint.com/books/jquery1/">Sitepoint&#8217;s latest title</a> is aimed at the average Front-end developer/designer and promises to teach her the intricate workings of the jQuery library. In fact, if we&#8217;re to go by the title, it promises to take her from &#8220;novice to ninja&#8221;. Let me be honest, I was&#8230;]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.sitepoint.com/books/jquery1/">Sitepoint&#8217;s latest title</a> is aimed at the average Front-end developer/designer and promises to teach her the intricate workings of the jQuery library. In fact, if we&#8217;re to go by the title, it promises to take her from &#8220;novice to ninja&#8221;. Let me be honest, I was originally skeptical of the book, but upon finding out that it was co-authored by the esteemed <a href="http://www.mrspeaker.net/">mrspeaker</a> my mind was quickly changed.</p>

<a href="http://www.sitepoint.com/books/jquery1/"><img class="main-img" src="http://james.padolsey.com/wp-content/uploads/jquerysp.jpg" alt="jQuery: Novice to Ninja" /> </a>

<p>As with any good book, the first chapter outlines what the subject matter is. The authors talk about jQuery itself and touch briefly on the DOM. You have to remember that this book is aimed at developers or designers that don&#8217;t really know that much about JavaScript, the DOM, or jQuery, so the book is accordingly written in such a way that it won&#8217;t confuse the pants off your typical photshop-wizz turned HTML/CSS guy.</p>

<p>So if you know JavaScript well, you might not enjoy this book &#8212; it will probably annoy you, simply because it doesn&#8217;t go into a huge amount of detail on what&#8217;s happening behind the scenes. But, for those that it&#8217;s aimed at, I think it does a pretty good job.</p>

<p>One thing that we have a problem with, in the jQuery community bubble, is the overzealous generation of redundant nomenclature. This book doesn&#8217;t fall short on this front. jQuery&#8217;s methods are referred to as &#8220;actions&#8221;.</p>

<p>The second chapter covers the most basic usages of jQuery, <em>selecting</em> stuff and <em>doing</em> stuff. It&#8217;s all innocent and simple &#8212; I like it. To keep the reader focused the authors have slipped in &#8220;A few tricks&#8221;. Among them is the popular add-a-class-on-mouseover, remove-it-on-mouseout. </p>

<p>Nearing the end of chapter two, the book has the following to say about the <code>hover</code> method:</p>

<span id="more-1442"></span>

<blockquote><p>&#8220;It requires two functions as parameters: one to handle the <code>mouseover</code> event, and one to handle the <code>mouseout</code> event.&#8221;</p></blockquote>

<p>The book <em>does</em> claim to be 1.4-ready, so I think it&#8217;s only right to mention this. In jQuery 1.4 the <a href="http://api.jquery.com/hover/">hover method</a> accepts either one or two parameters, and they don&#8217;t correspond to the <code>mouse(over|out)</code> events; they correspond to the <code>mouse(enter|leave)</code> events, although I guess they&#8217;re not totally incorrect on this because jQuery simulates these events using <code>mouse(over|out)</code> in non-supporting browsers, but it is nevertheless an oversight.</p>

<p>Not soon after that did I find what I consider to be a grievous anti-pattern:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'.spoiler'</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
$<span class="br0">&#40;</span><span class="st0">'&lt;span class=&quot;revealer&quot;&gt;Tell me!&lt;/span&gt;'</span><span class="br0">&#41;</span>
    .<span class="me1">insertBefore</span><span class="br0">&#40;</span><span class="st0">'.spoiler'</span><span class="br0">&#41;</span><span class="sy0">;</span>
$<span class="br0">&#40;</span><span class="st0">'.revealer'</span><span class="br0">&#41;</span>.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fadeIn</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>It seems that in their desperation to keep it simple, the authors have chosen to spare the readers of what would be some very good advice. When you add something to the DOM, and it doesn&#8217;t need to be referenced within a CSS StyleSheet, then giving it a class or ID, and then getting a reference to that element via that hook, is normally an almost-entirely redundant process. This would be a far better approach:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> spoilers <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'.spoiler'</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">,</span>
    buttons <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'&lt;span&gt;Tell me!&lt;/span&gt;'</span><span class="br0">&#41;</span>.<span class="me1">insertBefore</span><span class="br0">&#40;</span>spoilers<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
buttons.<span class="me1">click</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fadeIn</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Or, even:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'.spoiler'</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">before</span><span class="br0">&#40;</span>
    $<span class="br0">&#40;</span><span class="st0">'&lt;span/&gt;'</span><span class="sy0">,</span> <span class="br0">&#123;</span>
        text<span class="sy0">:</span> <span class="st0">'Tell me!'</span><span class="sy0">,</span>
        click<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
            $<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">hide</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">next</span><span class="br0">&#40;</span><span class="br0">&#41;</span>.<span class="me1">fadeIn</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span>
<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>If I was the author, I don&#8217;t know what I would have done in this situation&#8230; since it&#8217;s a teaching exercise, do you show the readers how it <em>should</em> be done, or do you show them in the simplest way possible, so as to not bulldoze them with complexities?</p>

<p>Moving on, the book is very <em>cookbook&#8217;y</em> and focuses heavily on an imagined client that has lots of strange requirements (quite typical of your average client). From the introduction to chapter three:</p>

<blockquote><p>&#8220;The client is extremely happy [...] he believes flashy animations will help boost sales.</p>
<p>&#8216;I think it needs some of that Web 2.0 that I&#8217;ve been hearing about,&#8217; he says confidently. &#8216;Can you make it look more like a Web 2.0?&#8217;</p>
<p>&#8216;Errrm, indeed we can,&#8217; you assure him, as he passes you his next wish list chock-full of exciting changes—a list that will allow us to move beyond simply hiding and showing, and closer to our goal of being a jQuery ninja.&#8221;</p></blockquote>

<p>I have a soft spot for these kind of tongue-in-cheek introductions, they&#8217;re quite popular in other programming books too, and I think it does well to mentally elevate the reader &#8212; and to develop that all-important reader-author relationship.</p>

<p>Before you know it, you&#8217;re into the third chapter. Chapter three exhaustively covers animation. The detail is great and the authors even take the time touch on jQuery UI&#8217;s animation capabilities in addition to covering all of the core methods. I liked this chapter, &#8212; it seemed to cover pretty much everything, although I found myself disappointed with some of the code again:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'&lt;div id=&quot;navigation_blob&quot;&gt;&lt;/div&gt;'</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    width<span class="sy0">:</span> $<span class="br0">&#40;</span><span class="st0">'#navigation li:first a'</span><span class="br0">&#41;</span>.<span class="me1">width</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">10</span><span class="sy0">,</span>
    height<span class="sy0">:</span> $<span class="br0">&#40;</span><span class="st0">'#navigation li:first a'</span><span class="br0">&#41;</span>.<span class="me1">height</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">10</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span>.<span class="me1">appendTo</span><span class="br0">&#40;</span><span class="st0">'#navigation'</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Let&#8217;s introduce some basic optimisations:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> nav <span class="sy0">=</span> $<span class="br0">&#40;</span><span class="st0">'#navigation'</span><span class="br0">&#41;</span><span class="sy0">,</span>
    firstAnchor <span class="sy0">=</span> nav.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">'li:first a'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
$<span class="br0">&#40;</span><span class="st0">'&lt;div id=&quot;navigation_blob&quot;&gt;&lt;/div&gt;'</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    width<span class="sy0">:</span> firstAnchor.<span class="me1">width</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">10</span><span class="sy0">,</span>
    height<span class="sy0">:</span> firstAnchor.<span class="me1">height</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="nu0">10</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span>.<span class="me1">appendTo</span><span class="br0">&#40;</span>nav<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Chapter four is all about images, and takes the reader through the process of creating a simple lightbox effect. It also shows you how to download and &#8220;install&#8221; your typical jQuery lightbox plugin (in this case, <a href="http://colorpowered.com/colorbox/">ColorBox</a>). It continues to show you how to create a simple slideshow.</p>

<p>The bulk of this book was obviously written before 1.4 was released. I get the impression that, upon 1.4&#8217;s release, the authors quickly went through adding in some &#8220;actions&#8221; only made possible with 1.4. They covered the new way of creating elements in jQuery 1.4, which involves passing an object as the second argument to <code>jQuery()</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'&lt;div/&gt;'</span><span class="sy0">,</span> <span class="br0">&#123;</span>
    text<span class="sy0">:</span> <span class="st0">'foo'</span><span class="sy0">,</span>
    click<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">,</span>
    id<span class="sy0">:</span> <span class="st0">'muah'</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>The book had the following to say:</p>

<blockquote><p>&#8220;If you use a jQuery method name like text, html, or val, it will use the jQuery methods to set the property. Everything else will be treated as an attribute, as done with the src property.&#8221;</p></blockquote>

<p>This isn&#8217;t entirely accurate. jQuery only uses certain methods &#8212; not all of them. Methods available for usage in this way are specified under <code>jQuery.attrFn</code>.</p>

<p>Continuing&#8230;</p>

<p>Chapter five brings with it a lot of UI icing&#8230; tooltips, menus, tabs and accordions. The book takes the reader through the processes involved in creating these interface elements. Just like all the other chapters, chapter five is littered with little titbits of useful information. For example, it covers event propagation and default event actions in pretty decent detail.</p>

<p>The next chapter is probably my favourite &#8212; it covers code construction and best practices, including namespacing, encapsulation, commenting, DRY, templating and feature detection! I was pleasantly surprised to see these topics covered in a book like this. Their templating techniques were a little lacking &#8212; I would&#8217;ve liked to see some custom-typed <code>&lt;script&gt;</code> elements (like in <a href="http://ejohn.org/blog/javascript-micro-templating/">Resig&#8217;s solution</a>), or perhaps some templates stuffed into HTML comments.</p>

<p>Chapter six continues by explaining Ajax and showing the reader what jQuery offers in the way of Ajax/XHR utilities. You&#8217;re also taken through the process of creating an Ajax-driven image gallery among some other gems.</p>

<p>I&#8217;m going to stop outlining the chapters now&#8230; I&#8217;m bored, and you&#8217;re probably not getting much from that (heck, you could probably just look through the <a href="http://www.sitepoint.com/books/jquery1/toc.php">TOC</a>). The rest of book covers forms, controls, dialogs, validation, lists, trees, tables, plugins, themes and some &#8220;advanced topics&#8221;. There&#8217;s also a hefty appendices that has bunch of useful information, including a brief JavaScript tutorial for those new to the language.</p>

<p>Just so we&#8217;re clear, this is a good book, and if you fit the bill of a front-end wizz that wants to pick up jQuery, then this book will easily deliver. There are a couple of oversights, but in a book over 400 pages long I&#8217;d say that&#8217;s pretty amazing! <a href="http://www.sitepoint.com/books/jquery1/authors.php">Earl Castledine and Craig Sharkie</a> have done a pretty awesome job!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/jquery-novice-to-ninja/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>In the eye of the beholder</title>
		<link>http://james.padolsey.com/javascript/in-the-eye-of-the-beholder/</link>
		<comments>http://james.padolsey.com/javascript/in-the-eye-of-the-beholder/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 20:10:56 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Readability]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1438</guid>
		<description><![CDATA[The readability of anything is entirely dependent on who&#8217;s doing the reading. This undoubtedly applies to the area of code readability. When you write code you&#8217;re hopefully considering the readability of that code and how other developers will see it, but what level of understanding&#8230;]]></description>
			<content:encoded><![CDATA[<p>The readability of anything is entirely dependent on who&#8217;s doing the reading. This undoubtedly applies to the area of code readability. When you write code you&#8217;re hopefully considering the readability of that code and how other developers will see it, but what level of understanding are you accommodating? How proficient is that imagined developer that you&#8217;re writing your code for?</p>

<p>For me, it&#8217;s all about the language &#8212; if it&#8217;s well defined within the language spec (or the API docs, if you&#8217;re using a library) then use it! When I write something in JavaScript I&#8217;m doing so under the presumption that the next developer that reads my code will be moderately proficient in the language and any dependencies that I&#8217;m utilising (i.e. libraries/frameworks/toolkits). Why should I presume anything less?</p>

<p>If the language or library is mainstream, it would make sense (in a perfect world) to assume that those tasked with coding in it are well versed in its syntax and rules.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// How to be annoyningly verbose</span>
<span class="kw2">function</span> showTheUserAMessage<span class="br0">&#40;</span> theMessageForTheUser <span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw3">alert</span><span class="br0">&#40;</span> theMessageForTheUser <span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>




<p>Gosh! That&#8217;s unreadable, isn&#8217;t it? Seriously, I don&#8217;t think Joe Bloggs Office Clerk could read that, do you?</p>

<p>If you&#8217;re going to say that some JavaScript code is unreadable, first consider whether you&#8217;re really the person it was written for. If you don&#8217;t know what <code>null || []</code> returns, then you certainly shouldn&#8217;t be claiming to know what is or isn&#8217;t unreadable.</p>

<p>Think of Perl! It&#8217;s like a mess of characters to most people, but to the lucky few that understand that Unicode soup, it&#8217;s as readable as the English language!</p>

<p><strong><em>Readability is in the eye of the beholder&#8230;</em></strong></p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/in-the-eye-of-the-beholder/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>Double bitwise NOT (~~)</title>
		<link>http://james.padolsey.com/javascript/double-bitwise-not/</link>
		<comments>http://james.padolsey.com/javascript/double-bitwise-not/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 18:45:52 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Cool Stuff]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1433</guid>
		<description><![CDATA[Who would have thought that JavaScript&#8217;s rare and mysterious bitwise operators could be so useful? I first discovered this trick a while ago from a <a href="http://www.slideshare.net/madrobby/extreme-javascript-performance">slideshow on JavaScript performance</a>, by Thomas Fuchs. The &#8220;trick&#8221; is as follows:


<pre class="javascript" style="font-family:monospace;">~~something<span class="sy0">;</span></pre>




The bitwise NOT operator (<code>~</code>) will take its&#8230;]]></description>
			<content:encoded><![CDATA[<p>Who would have thought that JavaScript&#8217;s rare and mysterious bitwise operators could be so useful? I first discovered this trick a while ago from a <a href="http://www.slideshare.net/madrobby/extreme-javascript-performance">slideshow on JavaScript performance</a>, by Thomas Fuchs. The &#8220;trick&#8221; is as follows:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">~~something<span class="sy0">;</span></pre></div></div>




<p>The bitwise NOT operator (<code>~</code>) will take its operand, convert it to a 32-bit integer, and will invert each bit so that each <code>0</code> becomes a <code>1</code> and vice versa.


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">00000000000000000000000000001001
...becomes
11111111111111111111111111110110</pre></div></div>




<p>The effect of this, given the expression <code>~foo</code> is <code>-(foo + 1)</code>. A double bitwise NOT operation (<code>~~foo</code>) will therefore result in <code>-(-(foo + 1) + 1)</code>. This only remains true for integers though; given all the potential operands, the real equivalent expression to <code>~~</code> is probably something like:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw1">typeof</span> foo <span class="sy0">===</span> <span class="st0">'number'</span> <span class="sy0">&amp;&amp;</span> <span class="sy0">!</span>isNaN<span class="br0">&#40;</span>foo<span class="br0">&#41;</span> <span class="sy0">&amp;&amp;</span> foo <span class="sy0">!==</span> Infinity
    <span class="sy0">?</span> foo <span class="sy0">&gt;</span> <span class="nu0">0</span> <span class="sy0">?</span> Math.<span class="me1">floor</span><span class="br0">&#40;</span>foo<span class="br0">&#41;</span> <span class="sy0">:</span> Math.<span class="me1">ceil</span><span class="br0">&#40;</span>foo<span class="br0">&#41;</span> <span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">;</span>
<span class="co1">// This is ONLY _effectively_ the same... this is</span>
<span class="co1">// NOT what happens internally!</span></pre></div></div>




<p>This is obviously a fair bit slower than <code>~~</code>.</p>

<p>If the operand is a number and it&#8217;s not <code>NaN</code> or <code>Infinity</code> then <code>~~</code> will have the effect of rounding it towards zero (<code>Math.ceil</code> for negative, <code>Math.floor</code> for positive). If it&#8217;s not a number, then I believe the internal <code>ToInt32</code> function converts it to zero.</p>

<p>Here are some examples of the double-bitwise NOT operation in all its glory:<p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">~~<span class="kw2">null</span><span class="sy0">;</span>      <span class="co1">// =&gt; 0</span>
~~undefined<span class="sy0">;</span> <span class="co1">// =&gt; 0</span>
~~<span class="nu0">0</span><span class="sy0">;</span>         <span class="co1">// =&gt; 0</span>
~~<span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">;</span>        <span class="co1">// =&gt; 0</span>
~~<span class="br0">&#91;</span><span class="br0">&#93;</span><span class="sy0">;</span>        <span class="co1">// =&gt; 0</span>
~~<span class="br0">&#40;</span><span class="nu0">1</span><span class="sy0">/</span><span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span>     <span class="co1">// =&gt; 0</span>
~~<span class="kw2">false</span><span class="sy0">;</span>     <span class="co1">// =&gt; 0</span>
~~<span class="kw2">true</span><span class="sy0">;</span>      <span class="co1">// =&gt; 1</span>
~~<span class="nu0">1.2543</span><span class="sy0">;</span>    <span class="co1">// =&gt; 1</span>
~~<span class="nu0">4.9</span><span class="sy0">;</span>       <span class="co1">// =&gt; 4</span>
~~<span class="br0">&#40;</span><span class="sy0">-</span><span class="nu0">2.999</span><span class="br0">&#41;</span><span class="sy0">;</span>  <span class="co1">// =&gt; 2</span></pre></div></div>




<p><code>~~</code>&#8217;s flooring capabilities make it a better alternative to <code>Math.floor</code> if you know you&#8217;re dealing with positives &#8212; it&#8217;s faster and takes up less characters. It&#8217;s not quite as readable though, but I&#8217;m hoping that <code>~~</code> will slowly become a very well-known technique in the JS arena, so we can all use it without fearing accusations.</p><span id="more-1433"></span>

<p>It&#8217;s quite useful for normalizing arguments that you expect to be integers too. Take this example from what the <a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Objects/Array/IndexOf">MDC recommends</a> for providing <code>Array.prototype.indexOf</code> to non-supporting browsers:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="coMULTI">/*Array.prototype.indexOf = function...*/</span>
<span class="kw2">var</span> from <span class="sy0">=</span> Number<span class="br0">&#40;</span>arguments<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span> <span class="sy0">||</span> <span class="nu0">0</span><span class="sy0">;</span>  
from <span class="sy0">=</span> <span class="br0">&#40;</span>from <span class="sy0">&lt;</span> <span class="nu0">0</span><span class="br0">&#41;</span>  
     <span class="sy0">?</span> Math.<span class="me1">ceil</span><span class="br0">&#40;</span>from<span class="br0">&#41;</span>  
     <span class="sy0">:</span> Math.<span class="me1">floor</span><span class="br0">&#40;</span>from<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>In goes <code>~~</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="coMULTI">/*Array.prototype.indexOf = function...*/</span>
<span class="kw2">var</span> from <span class="sy0">=</span> ~~arguments<span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span><span class="sy0">;</span></pre></div></div>




<p>Go forth and double-NOT!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/double-bitwise-not/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>I’m for comments</title>
		<link>http://james.padolsey.com/general/im-for-comments/</link>
		<comments>http://james.padolsey.com/general/im-for-comments/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 12:12:28 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1425</guid>
		<description><![CDATA[One of the best qualities of this industry, in my opinion, is that everyone loves to share their knowledge. Regardless of intent, whether it be ego, the pursuit of accuracy, or perhaps quasi-altruism, most of us make a point of making sure our voice is&#8230;]]></description>
			<content:encoded><![CDATA[<p>One of the best qualities of this industry, in my opinion, is that everyone loves to share their knowledge. Regardless of intent, whether it be ego, the pursuit of accuracy, or perhaps quasi-altruism, most of us make a point of making sure our voice is heard. The reason we&#8217;re so good at it in this industry is probably because we work on the internet, pretty much all of the time! And, let&#8217;s face it, us web developers, we&#8217;re pretty damn good at using the internet!</p>

<p>Anything that perpetuates this quality is something I can get behind. Communication is key!</p>

<p>Forgive the self-indulgent introspection but I think it&#8217;s important for me to encapsulate how I feel about this into prose, just so I &#8220;have it on file&#8221;.</p>

<p>I know for a fact, that if not for the comments left on various posts of mine, I would be less technically proficient than I am right now. Note that I&#8217;m not insinuating that my current level of proficiency is notably high; I&#8217;m merely stating that it would be notably lower if not for the feedback I&#8217;ve gained over the years.</p>

<p>In March of last year I posted a brief outline of how to use <a href="http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/">regular expressions in JavaScript</a>. This was at a time when I was pretty confident of my knowledge in the JavaScript arena, and I think that my confidence was shown in that post. I sold the information as if it were fact &#8212; there was no visible doubt in how I wrote it. I think this ill-founded confidence is inherent in this industry, partly due to its low barrier to entry. Within a month, you feel as if you&#8217;ve conquered HTML, and then three weeks later you&#8217;re a CSS guru etc.</p>

<p>I don&#8217;t see anything wrong with this confidence though &#8212; for one, it&#8217;s good practice to outline a subject in a professional manner, even if you&#8217;re not entirely correct about certain things. And, most importantly, the feedback that can be gained from those more experienced is infinitely helpful. Just to be clear though, I don&#8217;t think that putting people on pedestals is conducive to a good information-sharing atmosphere &#8212; if anything, it acts as a hindrance, because you presume their utterances to be entirely correct all the time, when nobody is truly omniscient!</p>

<p>This was what I had to say about the <code>RegExp.prototype.exec</code> method in that post:</p>

<blockquote><p>
this method is exactly the same as the <code>String.match()</code> method, the only difference being that you pass the string as the argument and the method is run as a member of the regular expression that you’re using to search the string.
</p></blockquote>

<p><a href="http://lucassmith.name/">Lucas Smith</a> chose to <a href="http://james.padolsey.com/javascript/regular-expressions-in-javascript-part-2/#comment-8624">leave a comment</a> telling me of my oversight. If not for him, who knows how long I would carry and &#8220;sell&#8221; this piece of incorrect information? I&#8217;m grateful to Luke and everyone else that has set me right when I have been incorrect.</p>

<p>If I didn&#8217;t enable comments on my blog then a casual reader would presume that my information is irrefutable, and here in lies a very dangerous prospect &#8212; not only would I not be set right by those more technically proficient, but my perception of correct information would be bound at best, by how I choose to interpret information from other sources, and at worst, by mere guesswork (as was the case in the <code>exec</code> oversight).</p>

<p>I believe that publically viewable feedback is essential to uphold accuracy. In other words, you should enable comments, so that people can tell you when you&#8217;re wrong. Obviously though, I&#8217;m talking about objective information, facts that cannot be disputed. I&#8217;m not talking about opinions or beliefs; you cannot correct someone for having the wrong opinion; it&#8217;s <em>their</em> opinion, and it&#8217;s likely to be founded on other pieces of scattered information and other opinions that they&#8217;ve accumulated &#8212; you can correct their understanding of the information, but not the opinion itself.</p>
<span id="more-1425"></span>

<p>Also, speaking of opinions, it is my opinion that in order to promote healthy commenting, the barrier to feedback must be low, otherwise you&#8217;re merely doing yourself a disservice &#8212; you will never get that valuable piece of information from Mrs. More Experienced because she can&#8217;t be arsed to fill out that damn &#8220;captcha&#8221;, and she certainly can&#8217;t be bothered to look up your email and send you one!</p>

<p>The comments have to be publically viewable because Mrs. More Experienced doesn&#8217;t really care about <em>your</em> understanding of the information; she cares about the danger of incorrect interpretation of the information you&#8217;ve provided, i.e. she cares about the people that will believe what you&#8217;ve written to be fact. By her commenting, she not only corrects you, but adds value to your content by making it more accurate, and thus halting the cascade of misinformation that affects subsequent readers.</p>

<p>I haven&#8217;t even touched on the benefits that can be derived from subjective debate. I can&#8217;t write a post that says, &#8220;multiple exits in functions are 100% okay&#8221;, and then disable commenting! It&#8217;s a subjective view in a technical arena &#8212; people mistake these for facts. It might be okay if I prefixed it with &#8220;It is my opinion that&#8230;&#8221;.</p>

<p>What I&#8217;ve realised over the previous year is that by having a blog or a similar medium, you can inadvertently create the illusion of authority in a particular area, and to ironically quote one highly authoritative figure, &#8220;<em><strong>Unthinking respect for authority is the greatest enemy of truth.</strong></em>&#8221; ~ <em>Albert Einstein</em>.</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/general/im-for-comments/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Under jQuery’s bonnet</title>
		<link>http://james.padolsey.com/javascript/under-jquerys-bonnet/</link>
		<comments>http://james.padolsey.com/javascript/under-jquerys-bonnet/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 22:44:35 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Cool Stuff]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1414</guid>
		<description><![CDATA[If there&#8217;s one thing all library users should be doing more, it&#8217;s peeling back the layer of abstraction and seeing what&#8217;s really happening underneath. This is the only way to gain a true understanding of what the library provides, and who knows, maybe you&#8217;ll find&#8230;]]></description>
			<content:encoded><![CDATA[<p>If there&#8217;s one thing all library users should be doing more, it&#8217;s peeling back the layer of abstraction and seeing what&#8217;s really happening underneath. This is the only way to gain a true understanding of what the library provides, and who knows, maybe you&#8217;ll find some gems that you didn&#8217;t know existed.</p>

<p>Libraries like jQuery aren&#8217;t very small when uncompressed. Traversing a long source file trying to look for a specific method&#8217;s implementation is far from ideal. I have frequently found myself in this situation with jQuery, so today I decided to do something about it, and the result is viewable at <strong><a href="http://james.padolsey.com/jquery">http://james.padolsey.com/jquery</a></strong>.</p>

<p class="video"><a href="http://james.padolsey.com/jquery/jQuery"><img alt="Preview of the jQuery Source Viewer" src="http://james.padolsey.com/wp-content/uploads/jquery-src-large.png" /></a></p>

<p>It allows you to study specific parts of jQuery&#8217;s source. You can type in a method name and you&#8217;ll see its implementation straight away, in all its syntax-highlighted glory! It will also link&#8217;ify all function names within the presented source:</p>

<p class="video"><a href="http://james.padolsey.com/jquery/css"><img alt="The css method's source, with certain function names as links" src="http://james.padolsey.com/wp-content/uploads/jquery-src-link.png" /></a></p>

<p>You can link directly to a method using the following URL pattern:</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://james.padolsey.com/jquery/[version/]methodName
&nbsp;
E.g.
http://james.padolsey.com/jquery/css
http://james.padolsey.com/jquery/1.3.2/attr
http://james.padolsey.com/jquery/jQuery.proxy
&nbsp;
-OR-
http://james.padolsey.com/jquery/#v=version&amp;fn=methodName</pre></div></div>




<p>If you don&#8217;t specify a version then 1.4 is assumed.</p>

<span id="more-1414"></span>

<p>The source that&#8217;s shown won&#8217;t be identical to the actual source, since this source viewer only works by <code>toString()</code>&#8216;ing the functions. I might eventually implement something a little more robust, but it&#8217;s going to take time &#8212; finding a specific function&#8217;s declaration in a source file is tricky, especially when they&#8217;re not all defined with the straightforward <code>method: function(){...}</code> or <code>function foo(){...}</code>.</p>

<p>I didn&#8217;t really intend it to be an accurate depiction of the real source anyway &#8212; just something I (and others) could use to see how certain things are implemented within jQuery.</p>

<p>I hope you find it useful&#8230; I know I will!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/under-jquerys-bonnet/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<item>
		<title>JavaScript’s Dark Alley</title>
		<link>http://james.padolsey.com/javascript/javascripts-dark-alley/</link>
		<comments>http://james.padolsey.com/javascript/javascripts-dark-alley/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 15:14:26 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1403</guid>
		<description><![CDATA[So, I happened upon an <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/8c75c6e8aff78806">interesting thread</a> this morning, concerning the recently released <a href="http://github.com/jamespadolsey/jQuery-Lint">jQuery Lint</a>. For those of you who have not dared to look, the <a href="http://groups.google.com/group/comp.lang.javascript/">comp.lang.javascript</a> newsgroup is truly the proverbial dark alley of JavaScript and DOM development. I can picture it vividly, a dingy dusty&#8230;]]></description>
			<content:encoded><![CDATA[<p>So, I happened upon an <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/8c75c6e8aff78806">interesting thread</a> this morning, concerning the recently released <a href="http://github.com/jamespadolsey/jQuery-Lint">jQuery Lint</a>. For those of you who have not dared to look, the <a href="http://groups.google.com/group/comp.lang.javascript/">comp.lang.javascript</a> newsgroup is truly the proverbial dark alley of JavaScript and DOM development. I can picture it vividly, a dingy dusty cobbled walkway blackened by the thick fog of regressive opinion and unprovoked flame wars, riddled with the unquestioned prerequisite of a newsgroup&#8217;s slow death &#8212; spam on every corner!</p>

<p>If you have a read through that thread you&#8217;ll notice none other than the infamous David Mark. This is the same person that <a href="http://groups.google.com/group/comp.lang.javascript/msg/37cb11852d7ca75c?">slammed jQuery</a> (oh, and MooTools) for not being able to &#8220;do *anything* 
right&#8221; and then, not too long ago, <a href="http://groups.google.com/group/jquery-dev/browse_thread/thread/baef5e91bd714033#msg_b8079000b547df15">threatened the jQuery project</a> with legal action over some DOM attributes tests. </p>

<p>David <a href="http://groups.google.com/group/comp.lang.javascript/browse_thread/thread/8c75c6e8aff78806">had the following to say</a> about jQuery Lint:</p>

<blockquote><p>
Well, sort of.  It&#8217;s not so much the Johnny-come-lastly advisor plug- 
in, but the fact that it is just another horrible plug-in for an 
equally bad pile of JS [he means jQuery].  Anyone who would use jQuery enough to want to 
write a plug-in is not going to be the best candidate to wtite a 
browser scripting &#8220;lint.&#8221;  I can&#8217;t see it. 
</p></blockquote>

<p>He continues:</p>

<blockquote><p>
The idea (which is sound) is to warn developers when they are doing 
something incorrect (or ill-advised).  I haven&#8217;t looked at the code 
for it (and I&#8217;m sure I never will).  I find I don&#8217;t have to do that 
anymore as the basic rules always apply.  <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  
</p></blockquote>

<p>David doesn&#8217;t stop! He still continues:</p>

<blockquote><p>
<strong>[Scott Sauyet]</strong><br/>
&gt; It seems to be a tool to help new users learn things like that although this code is legal:


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">&quot;selector&quot;</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="st0">&quot;color&quot;</span><span class="sy0">,</span> <span class="st0">&quot;red&quot;</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="st0">&quot;margin&quot;</span><span class="sy0">,</span> <span class="nu0">0</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>



&gt; This would be more efficient:


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">&quot;selector&quot;</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>color<span class="sy0">:</span> <span class="st0">&quot;red&quot;</span><span class="sy0">,</span> margin<span class="sy0">:</span> <span class="nu0">0</span><span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>



<strong>[David Mark]</strong><br/>
You dump it and replace it with something better (and faster).  In this 
case, something like:


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">el.<span class="me1">style</span>.<span class="me1">color</span> <span class="sy0">=</span> <span class="st0">&quot;red&quot;</span><span class="sy0">;</span> 
el.<span class="me1">style</span>.<span class="me1">margin</span> <span class="sy0">=</span> <span class="st0">'0'</span><span class="sy0">;</span></pre></div></div>



That&#8217;s smaller, faster, more readable, makes no function calls, 
creates no new objects and is impervious to upgrades to jQuery 
(typically poison).  And, as we all know, jQuery doesn&#8217;t work worth a 
shit anyway as the &#8220;logic&#8221; in the script is mostly a diary of confused 
browser watchers.  Get the picture? <br/>
&gt; That doesn&#8217;t indicate anything wrong with the library any more than 
&gt; any lint program indicates problems with its target environment. <br/>
Of course, you don&#8217;t need a lint to see what is wrong with the 
library.  It&#8217;s as obviously unsuitable as a dissertation written in 
crayon. 
</p></blockquote>

<p>According to David we should be sparing ourselves the obvious folly of abstraction and spend more time writing &#8220;smaller, faster, more readable&#8221; JavaScript:</p>
<span id="more-1403"></span>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// We say:</span>
$<span class="br0">&#40;</span><span class="st0">'#elem'</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    fontWeight<span class="sy0">:</span> <span class="nu0">700</span><span class="sy0">,</span>
    width<span class="sy0">:</span> <span class="nu0">200</span><span class="sy0">,</span>
    height<span class="sy0">:</span> <span class="nu0">100</span><span class="sy0">,</span>
    color<span class="sy0">:</span> <span class="st0">'#000'</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// He says:</span>
<span class="kw2">var</span> elem <span class="sy0">=</span> document.<span class="me1">getElementById</span><span class="br0">&#40;</span><span class="st0">'elem'</span><span class="br0">&#41;</span><span class="sy0">;</span>
elem.<span class="me1">style</span>.<span class="me1">fontWeight</span> <span class="sy0">=</span> <span class="st0">'700'</span><span class="sy0">;</span>
elem.<span class="me1">style</span>.<span class="me1">width</span> <span class="sy0">=</span> <span class="st0">'200px'</span><span class="sy0">;</span>
elem.<span class="me1">style</span>.<span class="me1">height</span> <span class="sy0">=</span> <span class="st0">'100px'</span><span class="sy0">;</span>
elem.<span class="me1">style</span>.<span class="me1">color</span> <span class="sy0">=</span> <span class="st0">'#000'</span><span class="sy0">;</span></pre></div></div>




<p>David, if you&#8217;re reading this, maybe you could offer us a response, detailing why you think jQuery &#8220;doesn&#8217;t work worth a 
shit&#8221; and also perhaps you could enlighten us why verbose and old-fashioned DOM scripting is miles better than progressive abstractions like the one jQuery offers? And if not too much trouble, I would appreciate if you could <strong>spare us your slander and ad hominem</strong>.</p>

<p>And when I say &#8220;detailing why you think jQuery doesn&#8217;t work worth a shit&#8221; I don&#8217;t mean having a go at <a href="http://groups.google.com/group/comp.lang.javascript/msg/37cb11852d7ca75c?">petty code inconsistencies and syntax peculiarities</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/javascripts-dark-alley/feed/</wfw:commentRss>
		<slash:comments>72</slash:comments>
		</item>
		<item>
		<title>jQuery Lint</title>
		<link>http://james.padolsey.com/javascript/jquery-lint/</link>
		<comments>http://james.padolsey.com/javascript/jquery-lint/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 14:38:21 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Plugins]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1380</guid>
		<description><![CDATA[<a href="http://github.com/jamespadolsey/jQuery-Lint"><em>jQuery Lint</em></a> is a simple script you can download and use with jQuery. It works over the top of jQuery and diligently reports errors and any incorrect usage of jQuery. It will also, to some extent, offer guidance on best practices and performance concerns.

Unlike <a href="http://www.jslint.com/">JSLint</a>,&#8230;]]></description>
			<content:encoded><![CDATA[<p><a href="http://github.com/jamespadolsey/jQuery-Lint"><em>jQuery Lint</em></a> is a simple script you can download and use with jQuery. It works over the top of jQuery and diligently reports errors and any incorrect usage of jQuery. It will also, to some extent, offer guidance on best practices and performance concerns.</p>

<p>Unlike <a href="http://www.jslint.com/">JSLint</a>, jQuery Lint is a <em>runtime</em> reporter. To use it, you need to include it, after jQuery, in your document:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="sy0">&lt;</span>script src<span class="sy0">=</span><span class="st0">&quot;jquery.js&quot;</span><span class="sy0">&gt;&lt;/</span>script<span class="sy0">&gt;</span>
<span class="sy0">&lt;</span>script src<span class="sy0">=</span><span class="st0">&quot;jquery.lint.js&quot;</span><span class="sy0">&gt;&lt;/</span>script<span class="sy0">&gt;</span></pre></div></div>




<p>jQuery lint&#8217;s main objective is to notify you of incorrect usages of jQuery&#8217;s API. So, if you pass incorrect arguments to any method then jQuery Lint will let you know. It compares your arguments to the argument signatures in jQuery&#8217;s API. It reports via Firebug, although you can quite easily plug-in your own console mechanism.</p>

<p>It has four different error-reporting levels (accessible via <code>jQuery.LINT.level</code>), zero reports nothing, three will report everything, including small things like using <code>css().css().css()</code> instead of <code>css({...})</code>. It&#8217;s quite configurable too. You can add your own checks. E.g.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span class="me1">LINT</span>.<span class="me1">special</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">jQuery</span> <span class="sy0">=</span> jQuery.<span class="me1">LINT</span>.<span class="me1">special</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">jQuery</span> <span class="sy0">||</span> <span class="br0">&#91;</span><span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Add check on error-reporting level one.</span>
<span class="co1">// Check jQuery method.</span>
jQuery.<span class="me1">LINT</span>.<span class="me1">special</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>.<span class="me1">jQuery</span>.<span class="me1">push</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>selector<span class="sy0">,</span> context<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
    <span class="kw1">if</span> <span class="br0">&#40;</span>selector <span class="sy0">===</span> <span class="st0">'*'</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="st0">&quot;Don't use the universal selector!&quot;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>jQuery Lint tries to help you in determining where the problem occurred in your code. It&#8217;s not much help to you if it just says, &#8220;Err, you called <code>css()</code> incorrectly!&#8221;. If it occurred as a result of an event then Lint will say so, and if you&#8217;re using a browser that provides a stack-trace as part of its Error object (like Firefox) then Lint will also provide you with the file-name and line number. E.g.</p>

<p><img src="http://img11.imageshack.us/img11/9862/20100118140644.png" alt="jquery Lint - Reporting line number and file name where problem occured." /></p>

<p>You can read more about jQuery Lint and download it at Github:</p>

<p class="video">
    <strong><a href="http://github.com/jamespadolsey/jQuery-Lint">jQuery Lint @ Github</a></strong>
</p>

<p>The idea of a lint-like script for jQuery has been floating around for some time. I want to thank Dave Methvin in particular, for it was <a href="http://markmail.org/message/wzkosk2s5jklpkv4">his idea</a> that sparked my interest originally.</p>

<p>This is quite a young project, so there will be bugs. Please <a href="http://github.com/jamespadolsey/jQuery-Lint/issues">report them</a>!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/jquery-lint/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Cross-domain requests with jQuery</title>
		<link>http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/</link>
		<comments>http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 13:44:41 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[YQL]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1365</guid>
		<description><![CDATA[Chris Heilmann <a href="http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/">recently posted</a> on how to use YQL to make cross-domain requests, which would usually be prohibited due to the same-domain-policy. I already knew about YQL, but I had no idea that it]]></description>
			<content:encoded><![CDATA[<p>Chris Heilmann <a href="http://www.wait-till-i.com/2010/01/10/loading-external-content-with-ajax-using-jquery-and-yql/">recently posted</a> on how to use YQL to make cross-domain requests, which would usually be prohibited due to the same-domain-policy. I already knew about YQL, but I had no idea that it allowed retrieval of HTML from other sites, via JSON, returned <strong>as a single string</strong>! </p>

<p>Instead of asking for <code>JSON</code> format, ask for <code>XML</code>, but also add a <code>callback</code> parameter to your query. <a href="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%20%3D%20%22http://google.com%22%20and%20xpath%3D%22*%22&#038;format=xml&#038;callback=cbfunc">Voila</a>!</p>

<p>So, in short, YQL allows us to make cross-domain GET requests!</p>

<p>Chris also posted <a href="http://icant.co.uk/articles/crossdomain-ajax-with-jquery/error-handling.html">a demo</a>!</p>

<p>With a bit of hacking, we can make jQuery work with YQL for all cross-domain GET requests. <strong>UPDATE</strong>: I&#8217;ve decided to put this in my &#8220;<a href="http://github.com/jamespadolsey/jQuery-Plugins/">jQuery Plugins</a>&#8221; repo at Github:</p>

<p class="video">
    <strong><a href="http://github.com/jamespadolsey/jQuery-Plugins/tree/master/cross-domain-ajax/">Cross-Domain Ajax mod @ Github</a></strong>
</p>

<p>With this mod, any GET request made via <code>jQuery.ajax</code> to another domain will work!</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span class="br0">&#40;</span><span class="st0">'#container'</span><span class="br0">&#41;</span>.<span class="me1">load</span><span class="br0">&#40;</span><span class="st0">'http://google.com'</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// SERIOUSLY!</span>
&nbsp;
$.<span class="me1">ajax</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
    url<span class="sy0">:</span> <span class="st0">'http://news.bbc.co.uk'</span><span class="sy0">,</span>
    type<span class="sy0">:</span> <span class="st0">'GET'</span><span class="sy0">,</span>
    success<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>res<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw2">var</span> headline <span class="sy0">=</span> $<span class="br0">&#40;</span>res.<span class="me1">responseText</span><span class="br0">&#41;</span>.<span class="me1">find</span><span class="br0">&#40;</span><span class="st0">'a.tsh'</span><span class="br0">&#41;</span>.<span class="me1">text</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="kw3">alert</span><span class="br0">&#40;</span>headline<span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Works with $.get too!</span></pre></div></div>




<p>Have fun!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>Encapsulation in JavaScript</title>
		<link>http://james.padolsey.com/javascript/encapsulation-in-javascript/</link>
		<comments>http://james.padolsey.com/javascript/encapsulation-in-javascript/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 14:24:35 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[OOP]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=1360</guid>
		<description><![CDATA[Encapsulation is a useful technique in programming which allows you to separate an abstraction&#8217;s implementation from its interface, thus enabling future changes to the implementation without affecting the interface. There are other benefits of encapsulation, such as:


    Being able to screen (validate) new properties, via&#8230;]]></description>
			<content:encoded><![CDATA[<p>Encapsulation is a useful technique in programming which allows you to separate an abstraction&#8217;s implementation from its interface, thus enabling future changes to the implementation without affecting the interface. There are other benefits of encapsulation, such as:</p>

<ul>
    <li>Being able to screen (validate) new properties, via setter methods.</li>
    <li>Being able to process properties before their &#8220;release&#8221;, via getter methods.</li>
    <li>Making your abstraction less prone to abuse by other developers &#8212; specifically, to protect private variables and states that could compromise the effectiveness of the abstraction.</li>
</ul>

<p>Grady Booch (author of &#8220;Object Oriented Analysis and Design&#8221;) describes encapsulation as:</p>

<blockquote><p>
&#8220;The process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation.&#8221;
</p></blockquote>

<p>JavaScript is often considered a toy that doesn&#8217;t know of complex design patterns or have the capacity to support techniques such as encapsulation, but, contrary to misinformed belief, JavaScript <em>does</em> have capacity for encapsulation. It&#8217;s just quite tricky, that&#8217;s all!</p>

<h3>Hiding your variables in closures</h3>

<p>It&#8217;s possible to hide variables in JavaScript. To make this happen we take advantage of the <a href="http://james.padolsey.com/javascript/closures-in-javascript/">closure</a>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> Person<span class="br0">&#40;</span><span class="kw3">name</span><span class="sy0">,</span> age<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
    <span class="kw1">this</span>.<span class="me1">toString</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="st0">'Name: '</span> <span class="sy0">+</span> <span class="kw3">name</span> <span class="sy0">+</span> <span class="st0">', Age: '</span> <span class="sy0">+</span> age<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="br0">&#125;</span></pre></div></div>




<p>If you&#8217;re not sure what a closure is just think of it as a function defined within another function. The &#8220;child&#8221; function has access to the &#8220;parent&#8221; function&#8217;s scope.</p>

<p>You can see that we&#8217;re defining the <code>toString</code> method within the constructor.</p>

<p>The only reason to define a constructor&#8217;s methods anywhere other than its prototype, is in the situation where that method needs access to variables defined in the constructor&#8217;s scope. Above, the <code>toString</code> method needs access to the variables, <code>name</code> and <code>age</code>.</p>

<h3>Setters (and getters)</h3>

<p>Once we instantiate this constructor (<code>new Person('Jim', 88);</code>), there&#8217;s no way to change the name or age &#8212; they&#8217;re &#8220;hard-coded&#8221; in the inaccessible &#8220;parent&#8221; scope.</p>

<p>We can make it possible to change these values by adding a couple of setter (AKA, mutator) methods:</p>

<span id="more-1360"></span>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> Person<span class="br0">&#40;</span><span class="kw3">name</span><span class="sy0">,</span> age<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
    <span class="kw1">this</span>.<span class="me1">toString</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="st0">'Name: '</span> <span class="sy0">+</span> <span class="kw3">name</span> <span class="sy0">+</span> <span class="st0">', Age: '</span> <span class="sy0">+</span> age<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
    <span class="kw1">this</span>.<span class="me1">setName</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>newName<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw3">name</span> <span class="sy0">=</span> newName<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
    <span class="kw1">this</span>.<span class="me1">setAge</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>newAge<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        age <span class="sy0">=</span> newAge<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
<span class="br0">&#125;</span>
&nbsp;
<span class="kw2">var</span> jimmy <span class="sy0">=</span> <span class="kw2">new</span> Person<span class="br0">&#40;</span><span class="st0">'Jim'</span><span class="sy0">,</span> <span class="nu0">88</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
jimmy.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// =&gt; &quot;Name: Jim, Age: 88&quot;</span>
&nbsp;
jimmy.<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">'Jimmy'</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
jimmy.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// =&gt; &quot;Name: Jimmy, Age: 88&quot;</span></pre></div></div>




<p>This works perfectly, although there are a couple of points to discuss.</p>

<p>First, defining a bunch of methods in the constructor takes quite a bit of room and with a few more setter methods (and getters) the constructor can easily transform into a hard-to-read and hard-to-maintain piece of code. Semantically, this solution fails &#8212; a constructor should only contain what&#8217;s needed to create an instance &#8212; ideally, this shouldn&#8217;t include a bunch of method definitions.</p>

<p>Secondly, it&#8217;s slow. Redefining a bunch of functions upon every single instantiation isn&#8217;t going to be as fast as keeping all the methods in the prototype.</p>

<p>So, either we reach a compromise or we forget about real encapsulation, and settle for something a little less secure. For example, we could define all the getters and setters in the prototype and store our &#8220;privates&#8221; as public properties, but prefix them with an underscore or some other symbol to signify their private nature:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> Person<span class="br0">&#40;</span><span class="kw3">name</span><span class="sy0">,</span> age<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
    <span class="kw1">this</span>._name <span class="sy0">=</span> <span class="kw3">name</span><span class="sy0">;</span>
    <span class="kw1">this</span>._age <span class="sy0">=</span> age<span class="sy0">;</span>
&nbsp;
<span class="br0">&#125;</span>
&nbsp;
Person.<span class="me1">prototype</span>.<span class="me1">toString</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">return</span> <span class="st0">'Name: '</span> <span class="sy0">+</span> <span class="kw1">this</span>._name <span class="sy0">+</span> <span class="st0">', Age: '</span> <span class="sy0">+</span> <span class="kw1">this</span>._age<span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
Person.<span class="me1">prototype</span>.<span class="me1">setName</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>newName<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">this</span>._name <span class="sy0">=</span> newName<span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
Person.<span class="me1">prototype</span>.<span class="me1">setAge</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>newAge<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">this</span>._age <span class="sy0">=</span> newAge<span class="sy0">;</span>
<span class="br0">&#125;</span><span class="sy0">;</span></pre></div></div>




<p>Our constructor is now a lot simpler and the setters work as required. The only issue with this approach is that there&#8217;s no real information-hiding going on. Developers using the abstraction can still access the &#8220;implementation&#8221; (the private state).</p>

<h3>Real encapsulation, the hard way</h3>

<p>For real encapsulation, the following criteria need to be met:</p>

<ul>
    <li>You must be able to define private properties within the constructor and methods.</li>
    <li>You must be able to access and manipulate the private properties within the constructor and methods.</li>
    <li>The private properties must not be directly accessible from outside (i.e. as properties on the instance).</li>
</ul>

<p>To fulfill these criteria we&#8217;re going to need to create a small abstraction that will enable us to define our constructor, its methods, and its properties in a plain object, which will then be processed to produce the required constructor and prototype.</p>

<p>In accordance with convention, this abstraction will be named <code>Class</code>. This is how it will typically work:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> Person <span class="sy0">=</span> <span class="kw2">new</span> <span class="kw2">Class</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
&nbsp;
    _name<span class="sy0">:</span> <span class="st0">'Blank'</span><span class="sy0">,</span>
    _age<span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">,</span>
&nbsp;
    init<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="sy0">,</span> age<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="kw3">name</span> <span class="sy0">=</span> <span class="kw3">name</span><span class="sy0">;</span>
        <span class="kw1">this</span>.<span class="me1">age</span> <span class="sy0">=</span> age<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
    toString<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="st0">'Name: '</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="kw3">name</span> <span class="sy0">+</span> <span class="st0">', Age: '</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">age</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
    setAge<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>age<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="me1">age</span> <span class="sy0">=</span> age<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
    setName<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="kw3">name</span> <span class="sy0">=</span> <span class="kw3">name</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>The underscore prefix tells <code>Class</code> that we want those properties to be private. Privacy is achieved by retaining a truly private (in a closure) <code>privates</code> array, each item within the array represents the private properties of each instance. This will be clearer once you&#8217;ve seen the source (below).</p>

<p><code>Class</code> looks for an <code>init</code> method to use as the constructor.</p>

<p>Here&#8217;s the source for <code>Class</code>, excluding the <a href="http://gist.github.com/272905"><code>merge</code> function</a>:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">function</span> <span class="kw2">Class</span><span class="br0">&#40;</span>o<span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp;
    <span class="kw2">var</span> id <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">,</span>
        privates <span class="sy0">=</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">,</span>
        instancePrivates <span class="sy0">=</span> <span class="br0">&#91;</span><span class="br0">&#93;</span><span class="sy0">,</span>
        constructor <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp;
            <span class="kw1">this</span>.__id <span class="sy0">=</span> id<span class="sy0">++;</span>
&nbsp;
            <span class="co1">// Copy privates over to new privates obj,</span>
            <span class="co1">// just for this instance.</span>
            instancePrivates<span class="br0">&#91;</span><span class="kw1">this</span>.__id<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="br0">&#123;</span>
                privates<span class="sy0">:</span> merge<span class="br0">&#40;</span><span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">,</span> privates<span class="br0">&#41;</span><span class="sy0">,</span>
                constructor<span class="sy0">:</span> <span class="kw1">this</span>
            <span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
            <span class="kw1">if</span> <span class="br0">&#40;</span><span class="kw1">this</span>.<span class="me1">init</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">this</span>.<span class="me1">init</span>.<span class="me1">apply</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="sy0">,</span> arguments<span class="br0">&#41;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
&nbsp;
            <span class="kw1">return</span> <span class="kw1">this</span><span class="sy0">;</span>
&nbsp;
        <span class="br0">&#125;</span><span class="sy0">,</span>
        m<span class="sy0">;</span>
&nbsp;
    <span class="kw2">function</span> method<span class="br0">&#40;</span><span class="kw3">name</span><span class="sy0">,</span> fn<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
        constructor.<span class="me1">prototype</span><span class="br0">&#91;</span><span class="kw3">name</span><span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
            <span class="kw2">var</span> i<span class="sy0">,</span> ret<span class="sy0">,</span>
                thisPrivates <span class="sy0">=</span> instancePrivates<span class="br0">&#91;</span><span class="kw1">this</span>.__id<span class="br0">&#93;</span> <span class="sy0">||</span> <span class="br0">&#123;</span><span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
            <span class="co1">// Check constructor</span>
            <span class="kw1">if</span> <span class="br0">&#40;</span>thisPrivates.<span class="me1">constructor</span> <span class="sy0">!==</span> <span class="kw1">this</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="co1">// this.__id has been changed, exit.</span>
                <span class="kw1">return</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
&nbsp;
            thisPrivates <span class="sy0">=</span> thisPrivates.<span class="me1">privates</span><span class="sy0">;</span>
&nbsp;
            <span class="kw1">for</span> <span class="br0">&#40;</span>i <span class="kw1">in</span> thisPrivates<span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">this</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="sy0">=</span> thisPrivates<span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
&nbsp;
            ret <span class="sy0">=</span> fn.<span class="me1">apply</span><span class="br0">&#40;</span><span class="kw1">this</span><span class="sy0">,</span> arguments<span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
            <span class="kw1">for</span> <span class="br0">&#40;</span>i <span class="kw1">in</span> thisPrivates<span class="br0">&#41;</span> <span class="br0">&#123;</span>
                thisPrivates<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="sy0">=</span> <span class="kw1">this</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="sy0">;</span>
                <span class="kw1">delete</span> <span class="kw1">this</span><span class="br0">&#91;</span>i<span class="br0">&#93;</span><span class="sy0">;</span>
            <span class="br0">&#125;</span>
&nbsp;
            <span class="kw1">return</span> ret<span class="sy0">;</span>
&nbsp;
        <span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
    <span class="br0">&#125;</span><span class="sy0">;</span>
&nbsp;
    <span class="kw1">for</span> <span class="br0">&#40;</span>m <span class="kw1">in</span> o<span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
        <span class="co1">// Test for privates</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="co2">/^_/</span>.<span class="me1">test</span><span class="br0">&#40;</span>m<span class="br0">&#41;</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp;
            privates<span class="br0">&#91;</span>m.<span class="me1">replace</span><span class="br0">&#40;</span><span class="co2">/^_/</span><span class="sy0">,</span> <span class="st0">''</span><span class="br0">&#41;</span><span class="br0">&#93;</span> <span class="sy0">=</span> o<span class="br0">&#91;</span>m<span class="br0">&#93;</span><span class="sy0">;</span>
&nbsp;
        <span class="br0">&#125;</span> <span class="kw1">else</span> <span class="br0">&#123;</span>
&nbsp;
            method<span class="br0">&#40;</span>m<span class="sy0">,</span> o<span class="br0">&#91;</span>m<span class="br0">&#93;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
        <span class="br0">&#125;</span>
&nbsp;
    <span class="br0">&#125;</span>
&nbsp;
    <span class="kw1">return</span> constructor<span class="sy0">;</span>
&nbsp;
<span class="br0">&#125;</span></pre></div></div>




<p>There are cleaner ways of implementing this, but just for readability&#8217;s sake, we&#8217;re identifying each instance with a simple <code>__id</code> property. Yes, this can be manipulated from the outside, but doing so would result in the methods not running.</p>

<p>It has some constraints, but it does provide us with encapsulation:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> Person <span class="sy0">=</span> <span class="kw2">new</span> <span class="kw2">Class</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
&nbsp;
    _name<span class="sy0">:</span> <span class="st0">'Blank'</span><span class="sy0">,</span>
    _age<span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">,</span>
&nbsp;
    init<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="sy0">,</span> age<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="kw3">name</span> <span class="sy0">=</span> <span class="kw3">name</span><span class="sy0">;</span>
        <span class="kw1">this</span>.<span class="me1">age</span> <span class="sy0">=</span> age<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
    toString<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="st0">'Name: '</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="kw3">name</span> <span class="sy0">+</span> <span class="st0">', Age: '</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">age</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
    setAge<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>age<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="me1">age</span> <span class="sy0">=</span> age<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
    setName<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="kw3">name</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="kw3">name</span> <span class="sy0">=</span> <span class="kw3">name</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// Testing:</span>
&nbsp;
<span class="kw2">var</span> jimmy <span class="sy0">=</span> <span class="kw2">new</span> Person<span class="br0">&#40;</span><span class="st0">'Jim'</span><span class="sy0">,</span> <span class="nu0">88</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
jimmy._name<span class="sy0">;</span> <span class="co1">// undefined</span>
jimmy.<span class="kw3">name</span><span class="sy0">;</span> <span class="co1">// undefined</span>
&nbsp;
jimmy.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// =&gt; &quot;Name: Jim, Age: 88&quot;</span>
&nbsp;
jimmy.<span class="me1">setName</span><span class="br0">&#40;</span><span class="st0">'Jimmy'</span><span class="br0">&#41;</span><span class="sy0">;</span>
jimmy.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// =&gt; &quot;Name: Jimmy, Age: 88&quot;</span>
&nbsp;
jimmy.<span class="me1">setAge</span><span class="br0">&#40;</span><span class="nu0">54</span><span class="br0">&#41;</span><span class="sy0">;</span>
jimmy.<span class="me1">toString</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// =&gt; &quot;Name: Jimmy, Age: 54&quot;</span>
&nbsp;
jimmy._age<span class="sy0">;</span> <span class="co1">// undefined</span>
jimmy.<span class="me1">age</span><span class="sy0">;</span> <span class="co1">// undefined</span></pre></div></div>




<p>You can also have private methods, e.g.</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="kw2">var</span> Box <span class="sy0">=</span> <span class="kw2">new</span> <span class="kw2">Class</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
&nbsp;
    _width<span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">,</span>
    _height<span class="sy0">:</span> <span class="nu0">0</span><span class="sy0">,</span>
&nbsp;
    init<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span>width<span class="sy0">,</span> height<span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">this</span>.<span class="me1">width</span> <span class="sy0">=</span> width<span class="sy0">;</span>
        <span class="kw1">this</span>.<span class="me1">height</span> <span class="sy0">=</span> height<span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
    info<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="st0">'Width: '</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">width</span> <span class="sy0">+</span>
                <span class="st0">',<span class="es0">\n</span>Height: '</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">height</span> <span class="sy0">+</span>
                <span class="st0">',<span class="es0">\n</span>Area: '</span> <span class="sy0">+</span> <span class="kw1">this</span>.<span class="me1">calcArea</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="sy0">,</span>
&nbsp;
    _calcArea<span class="sy0">:</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
        <span class="kw1">return</span> <span class="kw1">this</span>.<span class="me1">width</span> <span class="sy0">*</span> <span class="kw1">this</span>.<span class="me1">height</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
&nbsp;
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="kw2">var</span> myBox <span class="sy0">=</span> <span class="kw2">new</span> Box<span class="br0">&#40;</span><span class="nu0">100</span><span class="sy0">,</span> <span class="nu0">100</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
myBox.<span class="me1">calcArea</span><span class="sy0">;</span> <span class="co1">// undefined</span>
myBox._calcArea<span class="sy0">;</span> <span class="co1">// undefined</span>
&nbsp;
myBox.<span class="me1">info</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span> <span class="co1">// =&gt; &quot;Width: 100,\nHeight: 100,\nArea: 10000&quot;</span></pre></div></div>




<h3>Conclusion</h3>

<p>The method I used to achieve full encapsulation, while quite novel, isn&#8217;t going to be appropriate in all situations. The population and clearing of private variables has to happen on every method call, and so will likely cause a significant overhead in some applications.</p>

<p>That said, I don&#8217;t think the overhead will be all that significant, unless you&#8217;ve got a crazy amount of private properties/methods.</p>

<p>I hope this post has given you some insight into how you can achieve encapsulation and information-hiding in JavaScript.</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/encapsulation-in-javascript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 0.814 seconds --><!-- Cached page generated by WP-Super-Cache on 2010-03-15 18:12:29 -->
