<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://devlicio.us/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Sergio Pereira : jQuery, GuidedTour</title><link>http://devlicio.us/blogs/sergio_pereira/archive/tags/jQuery/GuidedTour/default.aspx</link><description>Tags: jQuery, GuidedTour</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP1 (Build: 31106.3070)</generator><item><title>Guided Tour: jQuery - Array wannabes</title><link>http://devlicio.us/blogs/sergio_pereira/archive/2009/12/22/guided-tour-jquery-array-wannabes.aspx</link><pubDate>Wed, 23 Dec 2009 05:13:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:54712</guid><dc:creator>sergiopereira</dc:creator><slash:comments>0</slash:comments><description>	

	&lt;div class="note"&gt;
		This post is part of a series called the &lt;a href="http://devlicio.us/blogs/sergio_pereira/archive/tags/GuidedTour/default.aspx"&gt;Guided Tours&lt;/a&gt;.
	&lt;/div&gt;

	&lt;p&gt;
		In this second installment we are still looking inside the jQuery code.
		Trust me, even if it&amp;#39;s hard to digest, you can still learn enough
		if you focus on a little bit at a time.
	&lt;/p&gt;

	&lt;p&gt;
		The code we are interested in today is the following.
	&lt;/p&gt;

	&lt;pre name="code" class="js:nogutter"&gt;//from jQuery 1.3.2
get: function( num ) {
	return num === undefined ?

		// Return a &amp;#39;clean&amp;#39; array
		Array.prototype.slice.call( this ) : 

		// Return just the object
		this[ num ];
},&lt;/pre&gt;

	&lt;p&gt;
		That&amp;#39;s the code for the command &lt;code&gt;&lt;a href="http://docs.jquery.com/Core/get"&gt;jQuery.fn.get(index)&lt;/a&gt;&lt;/code&gt;, 
		which returns the element at the given index or the entire array if the index is
		omitted.
	&lt;/p&gt;

	&lt;p&gt;
		The JavaScript idiom that is really interesting in this function is that
		strangely long function call &lt;code&gt;Array.prototype.slice.call( this )&lt;/code&gt;.
	&lt;/p&gt;

	&lt;p&gt;
		That line is needed because we need to return an array and, even though they look like one, &lt;i&gt;the
		jQuery objects aren&amp;#39;t arrays&lt;/i&gt;. And they aren&amp;#39;t alone in that.
	&lt;/p&gt;

	&lt;h3&gt;If it walks like a duck...&lt;/h3&gt;

	&lt;p&gt;
		The &lt;code&gt;Array&lt;/code&gt; object is one that we can&amp;#39;t avoid becoming
		familiar with in JavaScript. They are everywhere. Data is passed
		to functions as arrays. Data is returned in arrays.
		&lt;b&gt;Or are they?&lt;/b&gt;
	&lt;/p&gt;

	&lt;p&gt;
		Aside from jQuery objects there are at least two other important occurrences
		of data structures that are used like arrays but really aren&amp;#39;t: the
		&lt;code&gt;&lt;a href="https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Functions_and_function_scope/arguments"&gt;arguments&lt;/a&gt;
		&lt;/code&gt; variable and the DOM &lt;code&gt;&lt;a href="https://developer.mozilla.org/En/DOM/NodeList"&gt;NodeList&lt;/a&gt;&lt;/code&gt;
		collections.
	&lt;/p&gt;

	&lt;p&gt;
		The &lt;code&gt;arguments&lt;/code&gt; variable is the list of parameters passed to 
		the current function and the &lt;code&gt;NodeList&lt;/code&gt; is what is returned from members of the the DOM API such as
		&lt;code&gt;document.getElementsByTagName()&lt;/code&gt; or &lt;code&gt;element.childNodes&lt;/code&gt;.
		Both of these types have a &lt;code&gt;length&lt;/code&gt; property and expose their
		items with indices, like &lt;code&gt;arguments[1]&lt;/code&gt; or &lt;code&gt;elements[0]&lt;/code&gt;.
	&lt;/p&gt;

	&lt;p&gt;
		But don&amp;#39;t let this small coincidence fool you. As soon as you stop paying attention
		and try to use another array method, like &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;shift&lt;/code&gt;, 
		&lt;code&gt;join&lt;/code&gt; you&amp;#39;ll have your dreams shattered and a 
               &lt;code&gt;&lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/TypeError"&gt;TypeError&lt;/a&gt;&lt;/code&gt;to handle.
	&lt;/p&gt;

	&lt;h3&gt;Help me, jQuery&lt;/h3&gt;

	&lt;p&gt;
		To solve the above problem and return a real array object instead of the jQuery
		object itself, the code used the technique we highlighted.
	&lt;/p&gt;

	&lt;pre name="code" class="js:nogutter"&gt;Array.prototype.slice.call( this )&lt;/pre&gt;

	&lt;p&gt;
		The idea is to use the 
		&lt;code&gt;&lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/slice"&gt;array.slice()&lt;/a&gt;&lt;/code&gt;
		instance method
		to create a new array. The &lt;code&gt;slice&lt;/code&gt; method returns a chunk of the array and it
		takes two optional parameters (the boundaries) that, when omitted, make the function return
		a copy of the array itself.
	&lt;/p&gt;

	&lt;p&gt;
		I&amp;#39;ve written about the 
		&lt;code&gt;&lt;a href="http://devlicio.us/blogs/sergio_pereira/archive/2009/06/12/javascript-not-your-father-s-inheritance-model-part-1.aspx"&gt;prototype&lt;/code&gt; 
		object&lt;/a&gt; and 
		&lt;a href="http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx"&gt;how to invoke functions with &lt;code&gt;call&lt;/code&gt;&lt;/a&gt; 
		in previous posts, so I won&amp;#39;t
		repeat myself here. I&amp;#39;ll just add a reminder that the &lt;code&gt;this&lt;/code&gt; object in the &lt;code&gt;get&lt;/code&gt;
		method is the jQuery instance we&amp;#39;re working with.
	&lt;/p&gt;

	&lt;p&gt;
		What&amp;#39;s going on in &lt;code&gt;call( this )&lt;/code&gt; is that &lt;code&gt;slice&lt;/code&gt; is 
		being invoked as if it was a method of the jQuery object (note: the jQuery object
		does have a method called &lt;code&gt;&lt;a href="http://docs.jquery.com/Traversing/slice"&gt;slice&lt;/a&gt;&lt;/code&gt; but it 
		does not return an array.) 
	&lt;/p&gt;
	&lt;p&gt;
		You may be wondering how can we pass a jQuery object to
		&lt;code&gt;slice.call()&lt;/code&gt; and not get any errors inside the method implementation. Well,
		it works because the &lt;code&gt;slice&lt;/code&gt; method 
               &lt;a href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array#Generic_methods"&gt;was built to be generic&lt;/a&gt; and it
		only needs that the current object has a &lt;code&gt;length&lt;/code&gt; property and items
		in indexed positions. That&amp;#39;s &lt;a href="http://en.wikipedia.org/wiki/Duck_typing"&gt;Duck
		Typing&lt;/a&gt; at work.
	&lt;/p&gt;

	&lt;h3&gt;A Freebie&lt;/h3&gt;
	&lt;p&gt;
		There&amp;#39;s a small variation of this technique that you may come across in JavaScript
		as well.
	&lt;/p&gt;

	&lt;pre name="code" class="js:nogutter"&gt;[].slice.call( this )&lt;/pre&gt;

	&lt;p&gt;
		It calls the same &lt;code&gt;slice&lt;/code&gt; method but in a tad more wasteful manner
		because a new &lt;code&gt;Array&lt;/code&gt; instance is created each time just to provide access to its &lt;code&gt;slice&lt;/code&gt; method.
	&lt;/p&gt;

	&lt;p&gt;
		Well, that&amp;#39;s it. I hope you don&amp;#39;t scratch your head any longer when you see
		an expression like &lt;code&gt;XYZ.prototype.someMethod.call(obj)&lt;/code&gt; from now on.
	&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=54712" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Series/default.aspx">Series</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/GuidedTour/default.aspx">GuidedTour</category></item><item><title>Guided Tour: jQuery - guard and default operators</title><link>http://devlicio.us/blogs/sergio_pereira/archive/2009/12/09/guided-tour-jquery-guard-and-default-operators.aspx</link><pubDate>Wed, 09 Dec 2009 10:50:00 GMT</pubDate><guid isPermaLink="false">40756a8b-6212-4073-9d98-6c26781577de:54358</guid><dc:creator>sergiopereira</dc:creator><slash:comments>5</slash:comments><description>	&lt;div class="note"&gt;
		This post is part of a series called the &lt;a href="http://devlicio.us/blogs/sergio_pereira/archive/tags/GuidedTour/default.aspx"&gt;Guided Tours&lt;/a&gt;.
	&lt;/div&gt;
	

	&lt;p&gt;I&amp;#39;m sure I&amp;#39;m not alone when I say that one of the best ways to improve our
	coding skills is by reading code written by someone else. Sometimes we
	don&amp;#39;t realize how lucky we are to have so much source code at our
	fingertips, namely Open Source Software.&lt;/p&gt;

	&lt;h3&gt;The guided tours&lt;/h3&gt;

	&lt;p&gt;With this post I&amp;#39;ll start another unbound series where I highlight 
	some interesting piece of code that I studied. I&amp;#39;ll share
	my notes and do my best to explain what I learned from it.&lt;/p&gt;

	&lt;h3&gt;jQuery: Eating the elephant one bite at a time&lt;/h3&gt;

	&lt;p&gt;I wouldn&amp;#39;t dare to start things off with a complete overview of jQuery. It&amp;#39;s
	a massive chunk of JavaScript and I&amp;#39;m afraid there isn&amp;#39;t an easy entry point
	in the source code.&lt;/p&gt;

	&lt;p&gt;I chose to find parts of it that are relatively easy to explain separately
	from the rest and that contain something worth explaining. I&amp;#39;ll do at least a
	few of those from jQuery but my plan is to not keep this series tied to
	jQuery or even JavaScript.&lt;/p&gt;

	&lt;h3&gt;The code under the microscope&lt;/h3&gt;

	&lt;p&gt;We&amp;#39;re going to take a look at &lt;code&gt;&lt;a href="http://docs.jquery.com/Attributes/text"&gt;jQuery.fn.text()&lt;/a&gt;&lt;/code&gt;,
	which returns the textual content of all the elements in the jQuery wrapped set,
	combined in a single string.&lt;/p&gt;

&lt;p&gt;	Here&amp;#39;s the code from jQuery version 1.3.2.
&lt;/p&gt;
	&lt;pre name="code" class="js:nogutter"&gt;text: function( text ) {
  if ( typeof text !== &amp;quot;object&amp;quot; &amp;amp;&amp;amp; text != null )
    return this.empty().append( 
	   (this[0] &amp;amp;&amp;amp; this[0].ownerDocument || document).createTextNode( text ) 
	);

  var ret = &amp;quot;&amp;quot;;

  jQuery.each( text || this, function(){
    jQuery.each( this.childNodes, function(){
      if ( this.nodeType != 8 )
        ret += this.nodeType != 1 ?
          this.nodeValue :
          jQuery.fn.text( [ this ] );
    });
  });

  return ret;
},&lt;/pre&gt;

	&lt;p&gt;The &lt;code&gt;text()&lt;/code&gt; function can be called with or without arguments, so 
	initially it tries to detect if a string argument was passed to it, in which
	case it will be made the content of the elements in the jQuery object.&lt;/p&gt;

	&lt;p&gt;Be aware that in all methods defined inside &lt;code&gt;jQuery.fn&lt;/code&gt; the
	value of &lt;code&gt;this&lt;/code&gt; will be the current jQuery object.&lt;/p&gt;

	&lt;p&gt;The first thing that caught my attention was the following expression:&lt;/p&gt;

	&lt;pre name="code" class="js:nogutter"&gt;(this[0] &amp;amp;&amp;amp; this[0].ownerDocument || document)&lt;/pre&gt;

	&lt;p&gt;In the context of the code it&amp;#39;s expected to return a DOM document object, but it&amp;#39;s
	a boolean expression, isn&amp;#39;t it? What gives?&lt;/p&gt;

	&lt;p&gt;Well, yes, it is a boolean expression. That leads me to explain a subtle but powerful
	difference between boolean operators in JavaScript to many other languages you may
	be more used to.&lt;/p&gt;

	&lt;h3&gt;Guard and Default operators&lt;/h3&gt;

	&lt;p&gt;The way I like to describe the boolean operators &lt;code&gt;&amp;amp;&amp;amp;&lt;/code&gt; and &lt;code&gt;||&lt;/code&gt;
	is: &lt;i&gt;They return the operand that short-circuited and resolved the expression.&lt;/i&gt; To
	put in a different way, when the JS interpreter detects that one of the operands has a 
	value that makes the remainder of the comparison irrelevant, it stops right there and 
	that operand (not necessarily &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;) becomes the
	result of the boolean expression.&lt;/p&gt;

	&lt;div class="note"&gt;&lt;span class="legend"&gt;Truthy and Falsy:&lt;/span&gt;
	In the context of a boolean expression, any value in JavaScript has a boolean meaning.
	It&amp;#39;s actually easy to memorize which mean which. The values that are treated as &lt;i&gt;false&lt;/i&gt;
	are: &lt;code&gt;false&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt;, 
	&lt;code&gt;&amp;quot;&amp;quot;&lt;/code&gt; (empty string), and &lt;code&gt;NaN&lt;/code&gt;. We call them &lt;i&gt;falsy&lt;/i&gt;.
	Everything else is treated as &lt;i&gt;true&lt;/i&gt; and we call them &lt;i&gt;truthy&lt;/i&gt;.
	&lt;/div&gt;

	&lt;p&gt;Here are some examples:&lt;/p&gt;

	&lt;table&gt;
		&lt;tr&gt;&lt;td&gt;
			&lt;table border="1"&gt;
				&lt;tr&gt;&lt;th&gt;A&lt;/th&gt;&lt;th&gt;B&lt;/th&gt;&lt;th&gt;A &amp;amp;&amp;amp; B&lt;/th&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;false&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;null&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;0&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;null&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;456&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;456&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;&amp;quot;text&amp;quot;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;&amp;quot;text&amp;quot;&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;&amp;quot;text&amp;quot;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;true&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
			&lt;/table&gt;
		&lt;/td&gt;
		&lt;td&gt;
			&lt;table border="1"&gt;
				&lt;tr&gt;&lt;th&gt;A&lt;/th&gt;&lt;th&gt;B&lt;/th&gt;&lt;th&gt;A || B&lt;/th&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;false&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;123&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;123&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;0&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;123&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;123&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;null&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;null&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;123&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;456&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;123&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;123&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;&amp;quot;text&amp;quot;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;123&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
				&lt;tr&gt;&lt;td&gt;&lt;code&gt;&amp;quot;text&amp;quot;&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;code&gt;true&lt;/code&gt;&lt;/td&gt;&lt;td&gt;&lt;b&gt;&lt;code&gt;&amp;quot;text&amp;quot;&lt;/code&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
			&lt;/table&gt;
		&lt;/td&gt;&lt;/tr&gt;
	&lt;/table&gt;

	&lt;p&gt;Because of the above behavior, these boolean operators are often used as &lt;b&gt;Guard&lt;/b&gt; or &lt;b&gt;Default&lt;/b&gt;
	operators. The &lt;i&gt;guard&lt;/i&gt; operation is commonly used when you want to avoid a null or undefined reference error:&lt;/p&gt;

	&lt;pre name="code" class="js:nogutter"&gt;//someObj can be null. text will also be null in that case.
var text = someObj &amp;amp;&amp;amp; someObj.toString();&lt;/pre&gt;

	&lt;p&gt;Which is a shorthand for:&lt;/p&gt;

	&lt;pre name="code" class="js:nogutter"&gt;var text = null;
if (someObj !== null) {
  text = someObj.toString();
}&lt;/pre&gt;

	&lt;p&gt;The &lt;i&gt;default&lt;/i&gt; operation is arguably a much more common occurrence. We see it a lot when
	functions support optional arguments with default values. When a value is not given for
	a function parameter, it becomes &lt;code&gt;undefined&lt;/code&gt;. We can detect that and give a
	default value like this:&lt;/p&gt;

&lt;pre name="code" class="js:nogutter"&gt;function addAll(numbersArray, step) {
  step = step || 1;
  var sum = 0;
  for (var i = 0; i &amp;lt; numbersArray.length; i += step) {
    sum += numbersArray[i];
  }
  return sum;
}
addAll([1, 2, 3, 4, 5, 6]); // ==&amp;gt; 21
addAll([1, 2, 3, 4, 5, 6], 3); // ==&amp;gt; 5&lt;/pre&gt;
	
	&lt;p&gt;In the above example, the &lt;code&gt;step&lt;/code&gt; parameter is optional. Not providing it
	would cause a problem if we hadn&amp;#39;t defaulted it to one right at the beginning of
	the function.&lt;/p&gt;

	&lt;p&gt;Wow. We sure covered a lot of stuff just to explain a simple boolean expression.
	The good news is that we will see a lot of that in the jQuery code (or in pretty
	much any decent JS code base) so it&amp;#39;s good to understand it well.&lt;/p&gt;

	&lt;p&gt;Back to our original expression.&lt;/p&gt;

	&lt;pre name="code" class="js:nogutter"&gt;(this[0] &amp;amp;&amp;amp; this[0].ownerDocument || document)&lt;/pre&gt;

	&lt;p&gt;Armed with our new understanding we can finally read this expression as:
	&lt;i&gt;If &lt;code&gt;this.ownerDocument&lt;/code&gt; exists I want that, otherwise just give me the
	global &lt;code&gt;document&lt;/code&gt; object.&lt;/i&gt; This returned DOM document object will
	be the owner document of new text value being inserted.
	&lt;/p&gt;

	&lt;h3&gt;What about the rest of that function&lt;/h3&gt;

	&lt;p&gt;It&amp;#39;s funny that a tiny bit of that function became this long post. But 
	the remainder of the function, in its majority, isn&amp;#39;t really all that interesting
	in terms of  JavaScript. It&amp;#39;s mostly boring DOM navigation, done recursively.
	If you know how to use the 
	&lt;code&gt;&lt;a href="http://docs.jquery.com/Utilities/jQuery.each"&gt;jQuery.each()&lt;/a&gt;&lt;/code&gt;
	utility function, you can figure out that code on your own.&lt;/p&gt;

	&lt;p&gt;Our time here is up and we have a whole lot more of
	code sightseeing to do.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://devlicio.us/aggbug.aspx?PostID=54358" width="1" height="1"&gt;</description><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/Series/default.aspx">Series</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/JavaScript/default.aspx">JavaScript</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/jQuery/default.aspx">jQuery</category><category domain="http://devlicio.us/blogs/sergio_pereira/archive/tags/GuidedTour/default.aspx">GuidedTour</category></item></channel></rss>