<?xml version='1.0' encoding='utf-8' ?>
<feed xmlns='http://www.w3.org/2005/Atom'>
<title type='text'>Design Pepper</title>
<generator uri='http://nestacms.com'>Nesta</generator>
<id>tag:designpepper.com,2009:/</id>
<link href='http://designpepper.com/articles.xml' rel='self' />
<link href='http://designpepper.com/' rel='alternate' />
<subtitle type='text'>Musings about Design, Programming, and the Web</subtitle>
<updated>2013-05-19T00:00:00+00:00</updated>
<author>
<name>Joshua Clanton</name>
<uri>http://www.designpepper.com</uri>
<email>josh@designpepper.com</email>
</author>
<entry>
<title>Testing Array Contents with Array</title>
<link href='http://designpepper.com/blog/drips/testing-array-contents-with-array-some' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-05-19:/blog/drips/testing-array-contents-with-array-some</id>
<content type='html'>&lt;p&gt;&lt;em&gt;Originally published in &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When working with arrays, it is quite common to perform different actions based on whether or not the array contains a particular item. A fairly straightforward implementation of this pattern can be found below:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;planets = [&amp;#x000A;    &amp;quot;mercury&amp;quot;,&amp;#x000A;    &amp;quot;venus&amp;quot;,&amp;#x000A;    &amp;quot;earth&amp;quot;,&amp;#x000A;    &amp;quot;mars&amp;quot;,&amp;#x000A;    &amp;quot;jupiter&amp;quot;,&amp;#x000A;    &amp;quot;saturn&amp;quot;,&amp;#x000A;    &amp;quot;uranus&amp;quot;,&amp;#x000A;    &amp;quot;neptune&amp;quot;&amp;#x000A;];&amp;#x000A;&amp;#x000A;// Default to false&amp;#x000A;var containsPluto = false;&amp;#x000A;&amp;#x000A;for (var i = 0; i &amp;lt; planets.length &amp;amp;&amp;amp; !containsPluto; i++) {&amp;#x000A;    if (planets[i] === &amp;quot;pluto&amp;quot;) {&amp;#x000A;        containsPluto = true;&amp;#x000A;    }&amp;#x000A;}&amp;#x000A;&amp;#x000A;// Outputs: false&amp;#x000A;console.log(containsPluto);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Here our for loop is doing the work of iterating over the array, and a simple if statement checks to see if our condition is met yet. This for loop is a little more complicated than most because it is set up to stop iterating as soon as a matching element is found in the array.&lt;/p&gt;

&lt;p&gt;While it is not that difficult to understand, manually keeping track of the iteration and the &amp;quot;short circuiting&amp;quot; logic just isn&amp;#39;t the problem that we&amp;#39;re trying to solve. The fewer such things we have to keep in our head, the better. We could fix this by moving the loop into it&amp;#39;s own function (probably a good idea), but fortunately the latest version of the JavaScript standard includes such a function for us.&lt;/p&gt;

&lt;p&gt;The function is called &lt;code&gt;some&lt;/code&gt; and it is available on all arrays. Here is an example of how it would apply to our problem.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function isPluto(element) {&amp;#x000A;    return (element === &amp;quot;pluto&amp;quot;);&amp;#x000A;}&amp;#x000A;&amp;#x000A;// Outputs: false&amp;#x000A;console.log(planets.some(isPluto));&amp;#x000A;&amp;#x000A;dwarfPlanets = [&amp;#x000A;    &amp;quot;ceres&amp;quot;,&amp;#x000A;    &amp;quot;pluto&amp;quot;,&amp;#x000A;    &amp;quot;haumea&amp;quot;,&amp;#x000A;    &amp;quot;makemake&amp;quot;,&amp;#x000A;    &amp;quot;eris&amp;quot;&amp;#x000A;];&amp;#x000A;&amp;#x000A;// Outputs: true&amp;#x000A;console.log(dwarfPlanets.some(isPluto));&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All we have to do to use &lt;code&gt;some&lt;/code&gt; is to pass it a callback function. The callback will be executed once for each element until the callback returns true. At that point the &lt;code&gt;some&lt;/code&gt; method itself will return true. If the callback never returns true, then &lt;code&gt;some&lt;/code&gt; will return false.&lt;/p&gt;

&lt;p&gt;The great thing about &lt;code&gt;some&lt;/code&gt;, though is that you don&amp;#39;t have to think about that unless you want to. You just have to pass in your callback.&lt;/p&gt;

&lt;p&gt;In addition to the array element itself, the callback function has access to two other parameters: the current index, and the entire array. This can be useful in situations where you are comparing the current element against other members of the array. Consider this example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function isLessThanPrev(el, index, arr) {&amp;#x000A;    // The first element doesn&amp;#39;t have a predecessor,&amp;#x000A;    // so don&amp;#39;t evaluate it.&amp;#x000A;    if (index === 0) {&amp;#x000A;        return;&amp;#x000A;    } else {&amp;#x000A;        return (el &amp;lt; arr[index - 1]);&amp;#x000A;    }&amp;#x000A;}&amp;#x000A;&amp;#x000A;evens = [2, 4, 6, 8];&amp;#x000A;randoms = [0, 9, 2, 5];&amp;#x000A;&amp;#x000A;// Outputs: false&amp;#x000A;console.log(evens.some(isLessThanPrev));&amp;#x000A;&amp;#x000A;// Outputs: true&amp;#x000A;console.log(randoms.some(isLessThanPrev));&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As with other features of &lt;a href=&quot;http://en.wikipedia.org/wiki/ECMAScript_5#ECMAScript.2C_5th_Edition&quot;&gt;ES5&lt;/a&gt; that we&amp;#39;ve talked about, &lt;code&gt;some&lt;/code&gt; is only supported in IE9 and higher. If you&amp;#39;d like to use it in older browsers, you will need to use a library like &lt;a href=&quot;http://underscorejs.org/#some&quot;&gt;Underscore&lt;/a&gt; which has an equivalent method or a &lt;a href=&quot;https://github.com/kriskowal/es5-shim&quot;&gt;compatibility shim&lt;/a&gt; which ports &lt;code&gt;Array#some&lt;/code&gt; back into older browsers.&lt;/p&gt;

&lt;p&gt;Soon we&amp;#39;ll be taking a look at what it takes to write your own compatibility shims.&lt;/p&gt;

&lt;p&gt;Josh Clanton&lt;br /&gt;
&lt;a href=&quot;http://designpepper.com&quot;&gt;http://designpepper.com&lt;/a&gt;&lt;/p&gt;
</content>
<published>2013-05-19T00:00:00+00:00</published>
<updated>2013-05-19T00:00:00+00:00</updated>
</entry>
<entry>
<title>Using Dispatch Tables to Avoid Conditionals in JavaScript</title>
<link href='http://designpepper.com/blog/drips/using-dispatch-tables-to-avoid-conditionals-in-javascript' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-05-12:/blog/drips/using-dispatch-tables-to-avoid-conditionals-in-javascript</id>
<content type='html'>&lt;p&gt;&lt;em&gt;Originally published in &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When writing code, one of the surest ways to keep things simple and straightforward is to avoid conditionals when possible. Unfortunately, it is fairly common to see code with a lot of &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;switch&lt;/code&gt;, and &lt;code&gt;case&lt;/code&gt; statements like the following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function processUserInput(command) {&amp;#x000A;    switch (command) {&amp;#x000A;        case &amp;quot;north&amp;quot;:&amp;#x000A;            movePlayer(&amp;quot;north&amp;quot;);&amp;#x000A;            break;&amp;#x000A;        case &amp;quot;east&amp;quot;:&amp;#x000A;            movePlayer(&amp;quot;east&amp;quot;);&amp;#x000A;            break;&amp;#x000A;        case &amp;quot;south&amp;quot;:&amp;#x000A;            movePlayer(&amp;quot;south&amp;quot;);&amp;#x000A;            break;&amp;#x000A;        case &amp;quot;west&amp;quot;:&amp;#x000A;            movePlayer(&amp;quot;west&amp;quot;);&amp;#x000A;            break;&amp;#x000A;        case &amp;quot;look&amp;quot;:&amp;#x000A;            describeLocation();&amp;#x000A;            break;&amp;#x000A;        case &amp;quot;backpack&amp;quot;:&amp;#x000A;            showBackpack();&amp;#x000A;            break;&amp;#x000A;    }&amp;#x000A;}&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Above we have a function for processing user input from a text adventure game. While it isn&amp;#39;t terribly difficult to understand, it is more complicated than necessary. And as the number of commands grow, the function can quickly become unwieldy. So what can we do to simplify it?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;var commandTable = {&amp;#x000A;    north:    function() { movePlayer(&amp;quot;north&amp;quot;); },&amp;#x000A;    east:     function() { movePlayer(&amp;quot;east&amp;quot;);  },&amp;#x000A;    south:    function() { movePlayer(&amp;quot;south&amp;quot;); },&amp;#x000A;    west:     function() { movePlayer(&amp;quot;west&amp;quot;);  },&amp;#x000A;    look:     describeLocation,&amp;#x000A;    backpack: showBackpack&amp;#x000A;}&amp;#x000A;&amp;#x000A;function processUserInput(command) {&amp;#x000A;    commandTable[command]();&amp;#x000A;}&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this refactored version we are using a &lt;a href=&quot;https://en.wikipedia.org/wiki/Dispatch_table&quot;&gt;dispatch table&lt;/a&gt; to hold all the possible commands a user can give, and the functions the program should call. That changes the &lt;code&gt;processUserInput&lt;/code&gt; function into a single line, and eliminates all conditionals.&lt;/p&gt;

&lt;p&gt;If you are unfamiliar with bracket notation, it is just an alternate way of accessing a function&amp;#39;s properties, with the advantage that you can use variables for the property&amp;#39;s name. For example, if &lt;code&gt;command&lt;/code&gt; is &amp;quot;north&amp;quot;, then &lt;code&gt;commandTable[command]&lt;/code&gt; is equivalent to &lt;code&gt;commandTable.north&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fundamental change we made is transforming the conditionals into a data structure. And data is much easier to manipulate than conditionals. In the future, if we want to add a new command, all we have to do is add it to &lt;code&gt;commandTable&lt;/code&gt;. No messing around with &lt;code&gt;case&lt;/code&gt; or &lt;code&gt;break&lt;/code&gt; statements required.&lt;/p&gt;

&lt;p&gt;If you enjoyed this issue of &lt;em&gt;A Drip of JavaScript&lt;/em&gt;, &lt;a href=&quot;https://www.facebook.com/sharer/sharer.php?u=designpepper.com/a-drip-of-javascript&quot;&gt;share it on Facebook&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Josh Clanton&lt;br /&gt;&lt;a href=&quot;http://www.designpepper.com&quot;&gt;http://www.designpepper.com&lt;/a&gt;&lt;/p&gt;
</content>
<published>2013-05-12T00:00:00+00:00</published>
<updated>2013-05-12T00:00:00+00:00</updated>
</entry>
<entry>
<title>What are Prototype Properties&nbsp;and&nbsp;Methods?</title>
<link href='http://designpepper.com/blog/drips/what-are-prototype-properties-and-methods' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-05-05:/blog/drips/what-are-prototype-properties-and-methods</id>
<content type='html'>&lt;p&gt;&lt;em&gt;Originally published in &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;A &lt;a href=&quot;http://designpepper.com/blog/drips/constructors-in-javascript&quot;&gt;couple of weeks ago&lt;/a&gt;, I mentioned prototype properties in passing. But what exactly are they? Let&amp;#39;s take a look.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Book(title, author) {&amp;#x000A;    this.title = title;&amp;#x000A;    this.author = author;&amp;#x000A;}&amp;#x000A;&amp;#x000A;Book.prototype.getFormatted = function () {&amp;#x000A;    return this.title + &amp;quot; - &amp;quot; + this.author;&amp;#x000A;}&amp;#x000A;&amp;#x000A;var hobbit = new Book(&amp;quot;The Hobbit&amp;quot;, &amp;quot;Tolkien&amp;quot;);&amp;#x000A;&amp;#x000A;// Outputs: &amp;quot;The Hobbit - Tolkien&amp;quot;&amp;#x000A;console.log(hobbit.getFormatted());&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What is this magical &lt;code&gt;getFormatted&lt;/code&gt;? And how did it get attached to my object? It&amp;#39;s all through the power of prototypes.&lt;/p&gt;

&lt;p&gt;Whenever you create an object using a constructor, that constructor has a property called &lt;code&gt;prototype&lt;/code&gt; which points to an object. If you haven&amp;#39;t done anything special, that prototype property is just an empty object. But it&amp;#39;s an empty object with superpowers.&lt;/p&gt;

&lt;p&gt;Let&amp;#39;s see how that works.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Book(title, author) {&amp;#x000A;    this.title = title;&amp;#x000A;    this.author = author;&amp;#x000A;}&amp;#x000A;&amp;#x000A;Book.prototype.pubYear = &amp;quot;unknown&amp;quot;;&amp;#x000A;&amp;#x000A;var hobbit = new Book(&amp;quot;The Hobbit&amp;quot;, &amp;quot;Tolkien&amp;quot;);&amp;#x000A;&amp;#x000A;var caspian = new Book(&amp;quot;Prince Caspian&amp;quot;, &amp;quot;Lewis&amp;quot;);&amp;#x000A;caspian.pubYear = 1951;&amp;#x000A;&amp;#x000A;// Outputs: &amp;quot;unknown&amp;quot;&amp;#x000A;console.log(hobbit.pubYear);&amp;#x000A;&amp;#x000A;// Outputs: 1951&amp;#x000A;console.log(caspian.pubYear);&amp;#x000A;&amp;#x000A;// Outputs: &amp;quot;unknown&amp;quot;&amp;#x000A;console.log(Book.prototype.pubYear);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the example above, we create a &lt;code&gt;Book&lt;/code&gt; constructor, and then create a &lt;code&gt;pubYear&lt;/code&gt; property &amp;quot;unknown&amp;quot; on it&amp;#39;s prototype object.&lt;/p&gt;

&lt;p&gt;When we try to access &lt;code&gt;hobbit.pubYear&lt;/code&gt;, the JavaScript interpreter realizes that the &lt;code&gt;hobbit&lt;/code&gt; object doesn&amp;#39;t have a matching property, so it then checks to see if there is a matching property on the prototype object. Since there is, it will give us the value of &lt;code&gt;Book.prototype.pubYear&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;But because the &lt;code&gt;caspian&lt;/code&gt; object has a &lt;code&gt;pubYear&lt;/code&gt; property of it&amp;#39;s own, the interpreter never has to go look at the prototype object.&lt;/p&gt;

&lt;p&gt;While ordinary properties on a prototype can be useful, methods are more useful still. Let&amp;#39;s go back to our original example.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Book(title, author) {&amp;#x000A;    this.title = title;&amp;#x000A;    this.author = author;&amp;#x000A;}&amp;#x000A;&amp;#x000A;Book.prototype.getFormatted = function () {&amp;#x000A;    return this.title + &amp;quot; - &amp;quot; + this.author;&amp;#x000A;}&amp;#x000A;&amp;#x000A;var hobbit = new Book(&amp;quot;The Hobbit&amp;quot;, &amp;quot;Tolkien&amp;quot;);&amp;#x000A;&amp;#x000A;// Outputs: &amp;quot;The Hobbit - Tolkien&amp;quot;&amp;#x000A;console.log(hobbit.getFormatted());&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you should know how the property lookup works. The interpreter realizes that there is no &lt;code&gt;getFormatted&lt;/code&gt; property on &lt;code&gt;hobbit&lt;/code&gt; itself, so looks for it on the prototype.&lt;/p&gt;

&lt;p&gt;But you might notice something else special. The &lt;code&gt;getFormatted&lt;/code&gt; method makes use of &lt;code&gt;this.title&lt;/code&gt; and &lt;code&gt;this.author&lt;/code&gt;. That works because when an object&amp;#39;s method is invoked, the object itself becomes the &lt;code&gt;this&lt;/code&gt; for the method. And that&amp;#39;s even if the method itself belongs to the prototype rather than directly to the object.&lt;/p&gt;

&lt;p&gt;Why bother with all of this, though? Why not define &lt;code&gt;getFormatted&lt;/code&gt; directly on the object, like so?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Book(title, author) {&amp;#x000A;    this.title = title;&amp;#x000A;    this.author = author;&amp;#x000A;&amp;#x000A;    this.getFormatted = function () {&amp;#x000A;        return this.title + &amp;quot; - &amp;quot; + this.author;&amp;#x000A;    }&amp;#x000A;}&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Because if you do it like that, rather than defining a single method which can be used by all &lt;code&gt;Book&lt;/code&gt; objects, each object has its own copy of the method. Now imagine a scenario where you are producing lots of &lt;code&gt;Book&lt;/code&gt; objects.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;var bookArray = [ ]&amp;#x000A;&amp;#x000A;for (var i = 0; i &amp;lt; 100; i++) {&amp;#x000A;    bookArray[i] = new Book(&amp;quot;Some Title&amp;quot;, &amp;quot;Some Author&amp;quot;);&amp;#x000A;}&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now you have have 100 copies of &lt;code&gt;getFormatted&lt;/code&gt; taking up space in memory. In addition, if you need to change how &lt;code&gt;getFormatted&lt;/code&gt; works, you&amp;#39;ll need to manually update each instance of &lt;code&gt;Book&lt;/code&gt;. Compare that to how simple it is to update the prototype method.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Book(title, author) {&amp;#x000A;    this.title = title;&amp;#x000A;    this.author = author;&amp;#x000A;}&amp;#x000A;&amp;#x000A;Book.prototype.getFormatted = function () {&amp;#x000A;    return this.title + &amp;quot; - &amp;quot; + this.author;&amp;#x000A;}&amp;#x000A;&amp;#x000A;var hobbit = new Book(&amp;quot;The Hobbit&amp;quot;, &amp;quot;Tolkien&amp;quot;);&amp;#x000A;&amp;#x000A;// Outputs: &amp;quot;The Hobbit - Tolkien&amp;quot;&amp;#x000A;console.log(hobbit.getFormatted());&amp;#x000A;&amp;#x000A;// Oops! We need to use commas, not hyphens&amp;#x000A;Book.prototype.getFormatted = function () {&amp;#x000A;    return this.title + &amp;quot;, &amp;quot; + this.author;&amp;#x000A;}&amp;#x000A;&amp;#x000A;// Outputs: &amp;quot;The Hobbit, Tolkien&amp;quot;&amp;#x000A;console.log(hobbit.getFormatted());&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Updating the method on the prototype means that all instances of &lt;code&gt;Book&lt;/code&gt; get the updated method immediately, and you don&amp;#39;t have the possibility of some instances having the outdated method.&lt;/p&gt;

&lt;p&gt;So those are the basics of how prototype properties and methods work.&lt;/p&gt;

&lt;p&gt;Whew! That&amp;#39;s a bit wordier than normal, but the newsletter should be back to a normal length next week.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;Josh Clanton&lt;br /&gt;&lt;a href=&quot;http://designpepper.com&quot;&gt;http://designpepper.com&lt;/a&gt;&lt;/p&gt;
</content>
<published>2013-05-05T00:00:00+00:00</published>
<updated>2013-05-05T00:00:00+00:00</updated>
</entry>
<entry>
<title>Dealing with the Dangers of `this` in Constructors</title>
<link href='http://designpepper.com/blog/drips/dealing-with-the-dangers-of-this-in-constructors' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-04-26:/blog/drips/dealing-with-the-dangers-of-this-in-constructors</id>
<content type='html'>&lt;p&gt;&lt;em&gt;Originally published in &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As I mentioned in the &lt;a href=&quot;http://designpepper.com/blog/drips/constructors-in-javascript&quot;&gt;last issue&lt;/a&gt;, invoking a constructor without &lt;code&gt;new&lt;/code&gt; can be dangerous. Let&amp;#39;s go over why.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Color(r, g, b) {&amp;#x000A;    this.r = r;&amp;#x000A;    this.g = g;&amp;#x000A;    this.b = b;&amp;#x000A;}&amp;#x000A;&amp;#x000A;// Safe invocation&amp;#x000A;var red = new Color(255, 0, 0);&amp;#x000A;&amp;#x000A;// Dangerous invocation&amp;#x000A;var blue = Color(0, 0, 255);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When a constructor is invoked with the &lt;code&gt;new&lt;/code&gt; keyword, the &lt;code&gt;this&lt;/code&gt; value of the constructor is set to the new object that you are creating. But when a constructor is invoked like a normal function, its &lt;code&gt;this&lt;/code&gt; defaults to the same &lt;code&gt;this&lt;/code&gt; variable that any other function gets. And normally that is the global object (&lt;code&gt;window&lt;/code&gt; in the browser.)&lt;/p&gt;

&lt;p&gt;Here is an illustration of the problem.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;// Global variable&amp;#x000A;r = &amp;quot;Rodent Of Unusual Size&amp;quot;;&amp;#x000A;&amp;#x000A;function Color(r, g, b) {&amp;#x000A;    this.r = r;&amp;#x000A;    this.g = g;&amp;#x000A;    this.b = b;&amp;#x000A;}&amp;#x000A;&amp;#x000A;// Dangerous invocation&amp;#x000A;// Means `this` is the global object&amp;#x000A;var blue = Color(0, 0, 255);&amp;#x000A;&amp;#x000A;// Outputs: 0&amp;#x000A;console.log(r);&amp;#x000A;&amp;#x000A;// Outputs: undefined&amp;#x000A;console.log(blue);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the example above, there is a global variable named &lt;code&gt;r&lt;/code&gt;. Or to put it another way, the global object has a property named &lt;code&gt;r&lt;/code&gt;. When the &lt;code&gt;Color&lt;/code&gt; constructor is invoked without &lt;code&gt;new&lt;/code&gt;, the constructor&amp;#39;s &lt;code&gt;this&lt;/code&gt; is set to the global object (in most cases). Which means that the constructor function has just overwritten the global &lt;code&gt;r&lt;/code&gt; variable with something that was intended to be a property of the &lt;code&gt;blue&lt;/code&gt; object.&lt;/p&gt;

&lt;p&gt;Furthermore, because &lt;code&gt;Color&lt;/code&gt; was invoked as an ordinary function, it didn&amp;#39;t automatically return a new object, which means that &lt;code&gt;blue&lt;/code&gt; is also undefined.&lt;/p&gt;

&lt;p&gt;As you can imagine, debugging an issue like this can be time consuming and frustrating. So how do we prevent these sorts of problems? Fortunately the answer is pretty straightforward.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;// Global variable&amp;#x000A;r = &amp;quot;Rodent Of Unusual Size&amp;quot;;&amp;#x000A;&amp;#x000A;function Color(r, g, b) {&amp;#x000A;    // Check whether `this` is a&amp;#x000A;    // `Color` object.&amp;#x000A;    if (this instanceof Color) {&amp;#x000A;        this.r = r;&amp;#x000A;        this.g = g;&amp;#x000A;        this.b = b;&amp;#x000A;    } else {&amp;#x000A;        // If not, then we should invoke&amp;#x000A;        // the constructor correctly.&amp;#x000A;        return new Color(r, g, b);&amp;#x000A;    }&amp;#x000A;}&amp;#x000A;&amp;#x000A;// Dangerous invocation&amp;#x000A;// Means `this` is the global object&amp;#x000A;var blue = Color(0, 0, 255);&amp;#x000A;&amp;#x000A;// Outputs: &amp;quot;Rodent Of Unusual Size&amp;quot;&amp;#x000A;console.log(r);&amp;#x000A;&amp;#x000A;// Outputs: Color {r: 0, g: 0, b: 255}&amp;#x000A;console.log(blue);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the updated &lt;code&gt;Color&lt;/code&gt; constructor, the first thing we do is check whether &lt;code&gt;this&lt;/code&gt; is an instance of &lt;code&gt;Color&lt;/code&gt;. It works because the &lt;code&gt;new&lt;/code&gt; keyword will have already created the new object as an instance of the constructor before the constructor function begins running.&lt;/p&gt;

&lt;p&gt;If it isn&amp;#39;t a &lt;code&gt;Color&lt;/code&gt; object, then we know the constructor was invoked incorrectly, so we skip all the construction logic and have &lt;code&gt;Color&lt;/code&gt; return the results of correctly invoking itself with &lt;code&gt;new&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This means that the constructor is no longer in danger of clobbering the global object&amp;#39;s properties.&lt;/p&gt;

&lt;p&gt;Of course, using this approach also means that developers may get into the bad habit of invoking constructors without &lt;code&gt;new&lt;/code&gt;. If you&amp;#39;d rather just force them to always use &lt;code&gt;new&lt;/code&gt;, you could throw an error instead, like so:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Color(r, g, b) {&amp;#x000A;    // Check whether `this` is a&amp;#x000A;    // `Color` object.&amp;#x000A;    if (this instanceof Color) {&amp;#x000A;        this.r = r;&amp;#x000A;        this.g = g;&amp;#x000A;        this.b = b;&amp;#x000A;    } else {&amp;#x000A;        // If not, throw error.&amp;#x000A;        throw new Error(&amp;quot;`Color` invoked without `new`&amp;quot;);&amp;#x000A;    }&amp;#x000A;}&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And that&amp;#39;s how you can make your custom constructors safely deal with a missing &lt;code&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

&lt;p&gt;Josh Clanton&lt;/p&gt;
</content>
<published>2013-04-26T00:00:00+00:00</published>
<updated>2013-04-26T00:00:00+00:00</updated>
</entry>
<entry>
<title>Constructors in JavaScript</title>
<link href='http://designpepper.com/blog/drips/constructors-in-javascript' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-04-19:/blog/drips/constructors-in-javascript</id>
<content type='html'>&lt;p&gt;&lt;em&gt;Originally published in &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Despite the fact that they are very powerful, constructors are one of the most underused features of JavaScript. (Probably because they have a &lt;a href=&quot;http://crockford.com/&quot;&gt;very influential detractor&lt;/a&gt;.) But if you want to really know JavaScript, you&amp;#39;ll need to learn how they work.&lt;/p&gt;

&lt;p&gt;What is a constructor? It&amp;#39;s an ordinary function that is used with the &lt;code&gt;new&lt;/code&gt; operator to produce a specialized type of object.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;// `Color` is a constructor&amp;#x000A;var red = new Color(255, 0, 0);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In this example, red is a new &amp;quot;Color&amp;quot; object. But how does that work?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Color(r, g, b) {&amp;#x000A;    this.r = r;&amp;#x000A;    this.g = g;&amp;#x000A;    this.b = b;&amp;#x000A;}&amp;#x000A;&amp;#x000A;var red = new Color(255, 0, 0);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, the Color constructor is merely taking the arguments given to it and attaching them to the &lt;code&gt;this&lt;/code&gt; object. That&amp;#39;s because when the constructor is invoked by &lt;code&gt;new&lt;/code&gt;, the constructor&amp;#39;s &lt;code&gt;this&lt;/code&gt; is set to the object that &lt;code&gt;new&lt;/code&gt; will return.&lt;/p&gt;

&lt;p&gt;Which means that the code above is roughly equivalent to:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;var red = {&amp;#x000A;    r: 255,&amp;#x000A;    g: 0,&amp;#x000A;    b: 0&amp;#x000A;};&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So why would we use a constructor? There are a couple of important reasons. &lt;/p&gt;

&lt;p&gt;First, using a constructor means that all of these objects will be created with the same basic structure, and we are less likely to accidentally make a mistake than if we were creating a whole bunch of generic objects by hand. (Especially if we make the constructor throw errors on invalid input.)&lt;/p&gt;

&lt;p&gt;Second, using a constructor means that the object is explicitly marked as an instance of &lt;code&gt;Color&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;var red = new Color(255, 0, 0);&amp;#x000A;&amp;#x000A;var blue = { r: 255, g: 0, b: 0 };&amp;#x000A;&amp;#x000A;// Outputs: true&amp;#x000A;console.log(red instanceof Color);&amp;#x000A;&amp;#x000A;// Outputs: false&amp;#x000A;console.log(blue instanceof Color);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That gives us a means to ensure that we are receiving the correct type of data to process.&lt;/p&gt;

&lt;p&gt;Third, using a constructor means that we can easily assign specialized methods to the constructor&amp;#39;s prototype, and they will instantly be available to all objects created by the constructor.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;function Color(r, g, b) {&amp;#x000A;    this.r = r;&amp;#x000A;    this.g = g;&amp;#x000A;    this.b = b;&amp;#x000A;}&amp;#x000A;&amp;#x000A;Color.prototype.getAverage = function () {&amp;#x000A;    var total = this.r + this.g + this.b;&amp;#x000A;    var avg = total / 3;&amp;#x000A;    return parseInt(avg, 10);&amp;#x000A;};&amp;#x000A;&amp;#x000A;var red = new Color(255, 0, 0);&amp;#x000A;var white = new Color(255, 255, 255);&amp;#x000A;&amp;#x000A;// Outputs: 85&amp;#x000A;console.log(red.getAverage());&amp;#x000A;&amp;#x000A;// Outputs: 255&amp;#x000A;console.log(white.getAverage());&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It is important to &lt;strong&gt;always&lt;/strong&gt; use the new keyword when invoking the constructor. Otherwise the constructor may clobber the &lt;code&gt;this&lt;/code&gt; which was accidentally passed to it. In most cases that is the global object (&lt;code&gt;window&lt;/code&gt; in the browser or &lt;code&gt;global&lt;/code&gt; in Node.)&lt;/p&gt;

&lt;p&gt;Because of this danger, it is customary to capitalize a constructor&amp;#39;s name so that others know to invoke it with &lt;code&gt;new&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An upcoming article will deal with how to further mitigate the danger inherent in working with constructors.&lt;/p&gt;

&lt;p&gt;Josh Clanton&lt;/p&gt;
</content>
<published>2013-04-19T00:00:00+00:00</published>
<updated>2013-04-19T00:00:00+00:00</updated>
</entry>
<entry>
<title>Test-Driven Development with Jasmine Presentation</title>
<link href='http://designpepper.com/blog/tdd-with-jasmine' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-04-13:/blog/tdd-with-jasmine</id>
<content type='html'>&lt;p&gt;A couple of weeks ago, I gave a presentation to the NoVa Web Development meetup on TDD with Jasmine.&lt;/p&gt;

&lt;p&gt;You can find the slides &lt;a href=&quot;http://joshuacc.github.io/tdd-with-jasmine/&quot;&gt;on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Video of the presentation is included below.&lt;/p&gt;

&lt;div class=&quot;flex-video&quot;&gt;&lt;iframe width=&quot;640&quot; height=&quot;360&quot; src=&quot;http://www.youtube.com/embed/T76rwjqJk3w?rel=0&quot; frameborder=&quot;0&quot; allowfullscreen&gt;&lt;/iframe&gt;&lt;/div&gt;

&lt;p&gt;You may also want to check out my in-progress book on Jasmine: &lt;em&gt;&lt;a href=&quot;http://jasminetesting.com&quot;&gt;Jasmine Testing - A Cloak and Dagger Guide&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;
</content>
<published>2013-04-13T00:00:00+00:00</published>
<updated>2013-04-13T00:00:00+00:00</updated>
</entry>
<entry>
<title>Transforming Arrays with Array</title>
<link href='http://designpepper.com/blog/drips/transforming-arrays-with-array-map' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-04-12:/blog/drips/transforming-arrays-with-array-map</id>
<content type='html'>&lt;p&gt;&lt;em&gt;Originally published in &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;One of the most common tasks that developers perform in any language is taking an array of values and transforming those values. Up until recently, doing that in JavaScript took a fair bit of boilerplate code. For instance, here is some code for darkening RGB colors:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;var colors = [&amp;#x000A;    {r: 255, g: 255, b: 255 }, // White&amp;#x000A;    {r: 128, g: 128, b: 128 }, // Gray&amp;#x000A;    {r: 0,   g: 0,   b: 0   }  // Black&amp;#x000A;];&amp;#x000A;&amp;#x000A;var newColors = [];&amp;#x000A;&amp;#x000A;for (var i = 0; i &amp;lt; colors.length; i++) {&amp;#x000A;    transformed = {&amp;#x000A;        r: Math.round( colors[i].r / 2 ),&amp;#x000A;        g: Math.round( colors[i].g / 2 ),&amp;#x000A;        b: Math.round( colors[i].b / 2 )&amp;#x000A;    };&amp;#x000A;&amp;#x000A;    newColors.push(transformed);&amp;#x000A;}&amp;#x000A;&amp;#x000A;// Outputs:&amp;#x000A;// [&amp;#x000A;//    {r: 128, g: 128, b: 128 },&amp;#x000A;//    {r: 64,  g: 64,  b: 64  },&amp;#x000A;//    {r: 0,   g: 0,   b: 0   }&amp;#x000A;// ];&amp;#x000A;console.log(newColors);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, there&amp;#39;s quite a bit going on in that code that isn&amp;#39;t really about what we want to accomplish, but is keeping track of trivial things like the current index and moving the values into the new array. What if we didn&amp;#39;t have to do all of that?&lt;/p&gt;

&lt;p&gt;Fortunately in ECMAScript 5 (the latest version of JavaScript), we don&amp;#39;t. Here is the same example rewritten to take advantage of the &lt;code&gt;map&lt;/code&gt; method:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;var newColors = colors.map(function(val) {&amp;#x000A;    return {&amp;#x000A;        r: Math.round( val.r / 2 ),&amp;#x000A;        g: Math.round( val.g / 2 ),&amp;#x000A;        b: Math.round( val.b / 2 )&amp;#x000A;    };&amp;#x000A;});&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Much nicer isn&amp;#39;t it? Invoking &lt;code&gt;map&lt;/code&gt; returns a new array created by running a transformation function over each element of the original array.&lt;/p&gt;

&lt;p&gt;Now the only thing you need to keep track of is the logic of the transformation itself.&lt;/p&gt;

&lt;p&gt;Of course, &lt;code&gt;map&lt;/code&gt; isn&amp;#39;t limited to simple transformations like this. Your function can also make use of two additional parameters, the current index and the array itself. Consider the following example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;var starter = [1, 5, 5];&amp;#x000A;&amp;#x000A;function multiplyByNext (val, index, arr) {&amp;#x000A;    var next = index + 1;&amp;#x000A;&amp;#x000A;    // If at the end of array&amp;#x000A;    // use the first element&amp;#x000A;    if (next === arr.length) {&amp;#x000A;        next = 0;&amp;#x000A;    }&amp;#x000A;&amp;#x000A;    return val * arr[next];&amp;#x000A;}&amp;#x000A;&amp;#x000A;var transformed = starter.map(multiplyByNext);&amp;#x000A;&amp;#x000A;// Outputs: [5, 25, 5]&amp;#x000A;console.log(transformed);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, the additional parameters make it easy to create transformation functions which use the array element&amp;#39;s neighbors. This can be useful in implementing something like &lt;a href=&quot;https://en.wikipedia.org/wiki/Conway&amp;#x27;s_Game_of_Life&quot;&gt;Conway&amp;#39;s Game of Life&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Browser support for &lt;code&gt;map&lt;/code&gt; is pretty good, but not universal. It isn&amp;#39;t supported in IE 8 and below. You have a few options for dealing with this.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Don&amp;#39;t use &lt;code&gt;map&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use something like &lt;a href=&quot;https://github.com/kriskowal/es5-shim&quot;&gt;es5-shim&lt;/a&gt; to make older IE&amp;#39;s support &lt;code&gt;map&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use the &lt;code&gt;_.map&lt;/code&gt; method in &lt;a href=&quot;http://underscorejs.org/#map&quot;&gt;Underscore&lt;/a&gt; or &lt;a href=&quot;http://lodash.com/docs#map&quot;&gt;Lodash&lt;/a&gt; for an equivalent utility function.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One of the most powerful techniques for avoiding programming bugs is to reduce the number of things that you are keeping track of manually. Array&amp;#39;s &lt;code&gt;map&lt;/code&gt; method is one more tool to help you do exactly that.&lt;/p&gt;

&lt;p&gt;Thanks for reading! If you enjoyed this article, consider sending it to a friend.&lt;/p&gt;

&lt;p&gt;Josh Clanton&lt;/p&gt;
</content>
<published>2013-04-12T00:00:00+00:00</published>
<updated>2013-04-12T00:00:00+00:00</updated>
</entry>
<entry>
<title>What is an Array?</title>
<link href='http://designpepper.com/blog/drips/what-is-an-array' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-04-05:/blog/drips/what-is-an-array</id>
<content type='html'>&lt;p&gt;&lt;em&gt;Originally published in &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For an experienced developer, that question may seem simplistic, but in the case of JavaScript the answer is rather interesting.&lt;/p&gt;

&lt;p&gt;Conceptually, an array is just a list of values which can be accessed by using an integer as the &amp;quot;key&amp;quot;. The list starts at &lt;code&gt;0&lt;/code&gt; and goes up from there. If we were to describe that with JavaScript&amp;#39;s object notation, it would look like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;fakeArray = {&amp;#x000A;    0: &amp;quot;value 1&amp;quot;,&amp;#x000A;    1: &amp;quot;value 2&amp;quot;&amp;#x000A;}&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I feel like we&amp;#39;re missing something. Oh, I know. It&amp;#39;s that pesky &amp;quot;length&amp;quot; property.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;fakeArray = {&amp;#x000A;    0: &amp;quot;value 1&amp;quot;,&amp;#x000A;    1: &amp;quot;value 2&amp;quot;,&amp;#x000A;    length: 2&amp;#x000A;}&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;That&amp;#39;s looking suspiciously array-like. And, as &lt;a href=&quot;http://designpepper.com/blog/drips/arbitrary-parameters-with-the-arguments-object&quot;&gt;I&amp;#39;ve mentioned before&lt;/a&gt;, this is precisely how the &lt;code&gt;arguments&lt;/code&gt; object works. What I haven&amp;#39;t mentioned yet is that this is also how real arrays work &amp;quot;under the hood&amp;quot;.&lt;/p&gt;

&lt;p&gt;That&amp;#39;s right, in JavaScript arrays are just a specialized type of object built into the language. It&amp;#39;s easy to tell this by looking at the behavior of objects and arrays.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;fakeArray = {&amp;#x000A;    0: &amp;quot;value 1&amp;quot;,&amp;#x000A;    1: &amp;quot;value 2&amp;quot;,&amp;#x000A;    length: 2&amp;#x000A;};&amp;#x000A;&amp;#x000A;// Outputs &amp;quot;value 1&amp;quot;&amp;#x000A;console.log(fakeArray[0]);&amp;#x000A;&amp;#x000A;realArray = [&amp;quot;value 1&amp;quot;, &amp;quot;value 2&amp;quot;];&amp;#x000A;realArray.text = &amp;quot;some text&amp;quot;;&amp;#x000A;&amp;#x000A;// Outputs &amp;quot;some text&amp;quot;&amp;#x000A;console.log(realArray.text);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, you can access an object&amp;#39;s properties in exactly the same way as an array&amp;#39;s. And you can give an array additional properties just like any other object.&lt;/p&gt;

&lt;p&gt;What about all those special array methods like &lt;code&gt;indexOf&lt;/code&gt;, &lt;code&gt;slice&lt;/code&gt;, and &lt;code&gt;sort&lt;/code&gt;? Turns out, they&amp;#39;re just functions attached to the array object. (More specifically, they belong to the Array prototype, but that&amp;#39;s getting ahead of ourselves.)&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;realArray = [&amp;quot;value 1&amp;quot;, &amp;quot;value 2&amp;quot;];&amp;#x000A;&amp;#x000A;// Outputs &amp;quot;0&amp;quot;&amp;#x000A;console.log(realArray.indexOf(&amp;quot;value 1&amp;quot;));&amp;#x000A;&amp;#x000A;realArray.indexOf = function() {&amp;#x000A;    return &amp;quot;I&amp;#39;ll never tell.&amp;quot;;&amp;#x000A;};&amp;#x000A;&amp;#x000A;// Outputs &amp;quot;I&amp;#39;ll never tell.&amp;quot;&amp;#x000A;console.log(realArray.indexOf(&amp;quot;value 1&amp;quot;));&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In fact, given enough time, it is possible to reimplement almost all of the native array functionality purely in terms of manipulating objects.&lt;/p&gt;

&lt;p&gt;Here&amp;#39;s an example of reimplementing the &lt;code&gt;push&lt;/code&gt; method:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;fakeArray = {&amp;#x000A;    length: 0,&amp;#x000A;    push: function (val) {&amp;#x000A;        // Place the new value into the&amp;#x000A;        // next available integer key&amp;#x000A;        this[this.length] = val;&amp;#x000A;&amp;#x000A;        // Update the length property&amp;#x000A;        this.length = this.length + 1;&amp;#x000A;&amp;#x000A;        // Return the updated length&amp;#x000A;        return this.length;&amp;#x000A;    }&amp;#x000A;};&amp;#x000A;&amp;#x000A;fakeArray.push(&amp;quot;first value&amp;quot;);&amp;#x000A;fakeArray.push(&amp;quot;second value&amp;quot;);&amp;#x000A;&amp;#x000A;// Outputs &amp;quot;first value&amp;quot;&amp;#x000A;console.log(fakeArray[0]);&amp;#x000A;&amp;#x000A;// Outputs &amp;quot;second value&amp;quot;&amp;#x000A;console.log(fakeArray[1]);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The one important thing that can&amp;#39;t be reimplemented is the convenient array literal syntax for creating arrays. (The square brackets.) But you can always use a constructor instead. In fact, the following two ways of creating an array are exactly equivalent.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;literalWay = [&amp;quot;value 1&amp;quot;];&amp;#x000A;&amp;#x000A;constructorWay = new Array(&amp;quot;value 1&amp;quot;);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If you&amp;#39;re willing to give up the literal syntax, you can rebuild the concept of arrays in JavaScript entirely from scratch to achieve something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;myCustomArray = new CustomArray(&amp;quot;value 1&amp;quot;);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And now you know (if you didn&amp;#39;t already) how arrays work in JavaScript.&lt;/p&gt;

&lt;p&gt;If you have other subjects you&amp;#39;d like to see covered in the future, &lt;a href=&quot;http://designpepper.com/drip-suggestions&quot;&gt;drop me a suggestion&lt;/a&gt;. Thanks for reading!&lt;/p&gt;

&lt;p&gt;Josh Clanton&lt;/p&gt;
</content>
<published>2013-04-05T00:00:00+00:00</published>
<updated>2013-04-05T00:00:00+00:00</updated>
</entry>
<entry>
<title>The Drip Discussion Group</title>
<link href='http://designpepper.com/blog/drip-discussion-group' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-04-04:/blog/drip-discussion-group</id>
<content type='html'>&lt;p&gt;Have you ever wanted to discuss the concepts from an issue of &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;? Well now you can!&lt;/p&gt;

&lt;p&gt;There is now an official &lt;a href=&quot;https://groups.google.com/forum/?fromgroups#!forum/js-drip-discussions&quot;&gt;Drip Discussion Group&lt;/a&gt; on Google Groups.&lt;/p&gt;

&lt;p&gt;While it is focused on the contents of the newsletter, other JavaScript questions are welcome as well. I hope you&amp;#39;ll &lt;a href=&quot;https://groups.google.com/forum/?fromgroups#!forum/js-drip-discussions&quot;&gt;join me there&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Josh Clanton&lt;/p&gt;
</content>
<published>2013-04-04T00:00:00+00:00</published>
<updated>2013-04-04T00:00:00+00:00</updated>
</entry>
<entry>
<title>Reordering Arrays with Array</title>
<link href='http://designpepper.com/blog/drips/reordering-arrays-with-array-sort' rel='alternate' type='text/html' />
<id>tag:designpepper.com,2013-03-29:/blog/drips/reordering-arrays-with-array-sort</id>
<content type='html'>&lt;p&gt;&lt;em&gt;Originally published in &lt;a href=&quot;http://designpepper.com/a-drip-of-javascript&quot;&gt;A Drip of JavaScript&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In &lt;a href=&quot;http://designpepper.com/blog/drips/arbitrary-parameters-with-the-arguments-object&quot;&gt;last week&amp;#39;s issue&lt;/a&gt; I briefly mentioned the standard &lt;code&gt;sort&lt;/code&gt; method available on all JavaScript arrays. It does just what you&amp;#39;d expect.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;// Produces [1, 2, 3]&amp;#x000A;[3, 2, 1].sort();&amp;#x000A;&amp;#x000A;// Produces [&amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;c&amp;quot;]&amp;#x000A;[&amp;quot;c&amp;quot;, &amp;quot;b&amp;quot;, &amp;quot;a&amp;quot;].sort();&amp;#x000A;&amp;#x000A;// Produces [1, 2, &amp;quot;a&amp;quot;, &amp;quot;b&amp;quot;]&amp;#x000A;[&amp;quot;b&amp;quot;, 2, &amp;quot;a&amp;quot;, 1].sort();&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see, it sorts by dictionary order (numbers first, followed by letters). But what if you want to sort your array differently? For example, listing &amp;quot;better&amp;quot; cars first? How would you go about that? Fortunately, &lt;code&gt;sort&lt;/code&gt; accepts a custom comparison function.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;:::javascript&amp;#x000A;var cars = [&amp;#x000A;    &amp;quot;Honda Civic&amp;quot;,&amp;#x000A;    &amp;quot;Ford Taurus&amp;quot;,&amp;#x000A;    &amp;quot;Chevy Malibu&amp;quot;&amp;#x000A;]&amp;#x000A;&amp;#x000A;cars.sort(function(a, b) {&amp;#x000A;    // Default to 0, which indicates&amp;#x000A;    // no sorting is necessary&amp;#x000A;    var returnVal = 0;&amp;#x000A;&amp;#x000A;    // If `a` is a Chevy, subtract 1&amp;#x000A;    // to move `a` &amp;quot;up&amp;quot; in the sort order&amp;#x000A;    // because Chevys are awesome.&amp;#x000A;    if (a.match(/Chevy/)) {&amp;#x000A;        returnVal = returnVal - 1;&amp;#x000A;    }&amp;#x000A;&amp;#x000A;    // If `b` is a Chevy, add 1&amp;#x000A;    // to move `b` &amp;quot;up&amp;quot; in the sort order&amp;#x000A;    if (b.match(/Chevy/)) {&amp;#x000A;        returnVal = returnVal + 1;&amp;#x000A;    }&amp;#x000A;&amp;#x000A;    return returnVal;&amp;#x000A;});&amp;#x000A;&amp;#x000A;// Outputs:&amp;#x000A;// [&amp;quot;Chevy Malibu&amp;quot;, &amp;quot;Honda Civic&amp;quot;, &amp;quot;Ford Taurus&amp;quot;]&amp;#x000A;console.log(cars);&amp;#x000A;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The comparison function compares two values and returns a number.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If it returns a negative number, &lt;code&gt;a&lt;/code&gt; will be sorted to a lower index in the array.&lt;/li&gt;
&lt;li&gt;If it returns a positive number, &lt;code&gt;a&lt;/code&gt; will be sorted to a higher index.&lt;/li&gt;
&lt;li&gt;And if it returns &lt;code&gt;0&lt;/code&gt; no sorting is necessary.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When you pass &lt;code&gt;sort&lt;/code&gt; a comparison function, that function will be called repeatedly and given different values from the array until the array has been completely sorted.&lt;/p&gt;

&lt;p&gt;It is important to note that returning &lt;code&gt;0&lt;/code&gt; does not guarantee that &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; will remain in the same order. It merely means that sorting is not necessary. JavaScript engines may choose to keep them in the same order, but that is not part of the language spec.&lt;/p&gt;

&lt;p&gt;Thanks for reading! If you enjoyed this issue, why not subscribe to the newsletter?&lt;/p&gt;
</content>
<published>2013-03-29T00:00:00+00:00</published>
<updated>2013-03-29T00:00:00+00:00</updated>
</entry>
</feed>

<!-- page cached: 2013-19-05 00:02:51 -->

