<?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:atom="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    
    <title>Jimmy Cuadra</title>
    <description>Writing and screencasts on Ruby, JavaScript, HTML, and CSS</description>
    <link>http://www.jimmycuadra.com/posts</link>
    <language>en</language>
    <pubDate>Fri, 02 Nov 2012 19:03:55 -0700</pubDate>
    <lastBuildDate>Fri, 02 Nov 2012 19:03:55 -0700</lastBuildDate>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/jimmycuadra" /><feedburner:info uri="jimmycuadra" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
      <title>GuerillaPatch: An interface for monkey patching objects for Ruby 1.9 and 2.0</title>
      <description>&lt;p&gt;At RubyConf this week, the first preview of the upcoming Ruby 2.0 was released. One of the new features is &lt;em&gt;refinements&lt;/em&gt;, a way of adding new behavior to an object that only takes place within a certain scope. This allows a safer way to extend existing objects without screwing up code that may be depending on the original behavior. A common example of this is ActiveSupport, which adds extensions to many of the core classes. With refinements, these extensions can be added to a refinement module, which can then be "used" in other namespaces without affecting the object globally.&lt;/p&gt;

&lt;p&gt;This is a powerful new feature, but I was curious how best to write library code that uses it in a way that is interoperable with Ruby 1.9. I created a gem called &lt;strong&gt;GuerillaPatch&lt;/strong&gt; which provides a single interface that will extend an object with a refinement if run under Ruby 2.0, and fall back to simply modifying the class globally if run under 1.9.&lt;/p&gt;

&lt;p&gt;Install with &lt;code&gt;gem install guerilla_patch&lt;/code&gt;. The &lt;a href="https://github.com/jimmycuadra/guerilla_patch"&gt;source code&lt;/a&gt; and usage examples are available on GitHub.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/9L9gAFUScyU" height="1" width="1"/&gt;</description>
      <pubDate>Fri, 02 Nov 2012 19:03:55 -0700</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/9L9gAFUScyU/guerillapatch-an-interface-for-monkey-patching-objects-for-ruby-1-9-and-2-0</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/guerillapatch-an-interface-for-monkey-patching-objects-for-ruby-1-9-and-2-0</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/guerillapatch-an-interface-for-monkey-patching-objects-for-ruby-1-9-and-2-0</feedburner:origLink></item>
    <item>
      <title>jQuery is not an architecture</title>
      <description>&lt;p&gt;There is no question about the enormous positive impact &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; has had on front end web development. It removes all the pain of the terrible DOM API and provides utility functions for just about everything small applications need. But when your application starts to grow, you need to structure your code in a more organized way to make sure things are testable, maintainable, and that features are discoverable to both new members of your team and your future self, who may not be able to discern the business logic of your application from looking at a slew of selectors and anonymous function callbacks.&lt;/p&gt;

&lt;p&gt;Although jQuery has many facilities, its main purpose is as an abstraction layer to create a single, developer-friendly API that hides the ugliness of bad APIs and browser implementation quirks. Because most of its methods involve selecting, traversing, and manipulating DOM elements, the basic unit in jQuery is the selector. While this has the benefit of making DOM interaction simple and easy for beginners to learn, it has the unfortunate side effect of making people think that anything but very trivial amounts of JavaScript should be organized around jQuery selectors.&lt;/p&gt;

&lt;p&gt;In a very simple application or basic web page, e.g. a WordPress blog, tiny snippets of jQuery or a plugin dropped into the page may be appropriate. But if you're building something with an amount of JavaScript even slightly larger than that, things will get difficult to maintain quickly.&lt;/p&gt;

&lt;p&gt;jQuery is no substitute for good application architecture. It's just another utility library in your toolbelt. Think of jQuery simply as an abstraction of the DOM API and a way to think about Internet Explorer less often.&lt;/p&gt;

&lt;p&gt;To illustrate just how backwards the jQuery-selector-as-basic-unit approach is for a non-trivial application, think about how it compares to the structure of an object in an object-oriented paradigm. At the most basic level, objects consist of members and methods – stateful data and functions that perform actions to manipulate that data. Methods take arguments on which they operate. In selector-as-basis jQuery, you're effectively starting with an argument (the selector), and &lt;em&gt;passing it a method&lt;/em&gt; in the form of anonymous functions. This is not to say that object-oriented software is the only correct approach to writing software. The problem is that jQuery tends to make the target of an action the focus and not the action itself.&lt;/p&gt;

&lt;h2&gt;Ways to improve architecture&lt;/h2&gt;

&lt;p&gt;There are a few simple ways to improve the architecture of your JavaScript code beyond what is shown in most jQuery literature.&lt;/p&gt;

&lt;h3&gt;Namespaces&lt;/h3&gt;

&lt;p&gt;Use a single global object to namespace all your code. Use more objects attached to your global object to separate your modules by their high-level purpose. This protects you from name collisions and organizes the parts of your application in a discoverable way. When your application grows very large, this makes it easier for people less familiar with the system to find where a particular widget or behavior is defined.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;window.FOO = {
  &lt;span class="key"&gt;Widgets&lt;/span&gt;: {},
  &lt;span class="key"&gt;Utilities&lt;/span&gt;: {},
};
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;Modules&lt;/h3&gt;

&lt;p&gt;Use function prototypes or the &lt;a href="http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript"&gt;module pattern&lt;/a&gt; to create pseudo classes for your modules. All the intelligence about what your application does should be encapsulated inside these discoverable modules. Use clear, straight-forward method names. By extracting your event handlers and other functions into named methods, they can be unit tested in isolation for a high level of confidence that your system behaves the way you expect. It's also much clearer what the module does when looking at code you haven't seen in a while.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;FOO.Widgets.&lt;span class="function"&gt;CommentBox&lt;/span&gt; = &lt;span class="keyword"&gt;function&lt;/span&gt; (containerSelector) {
  &lt;span class="local-variable"&gt;this&lt;/span&gt;.container = &lt;span class="predefined"&gt;$&lt;/span&gt;(containerSelector);
  &lt;span class="local-variable"&gt;this&lt;/span&gt;.container.on(&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;submit&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;form&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="local-variable"&gt;this&lt;/span&gt;.addComment.bind(&lt;span class="local-variable"&gt;this&lt;/span&gt;));
};

FOO.Widgets.CommentBox.prototype.&lt;span class="function"&gt;addComment&lt;/span&gt; = &lt;span class="keyword"&gt;function&lt;/span&gt; (event) {
  &lt;span class="comment"&gt;// More logic here...&lt;/span&gt;
};

&lt;span class="comment"&gt;// Many more methods...&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;Instantiate your modules&lt;/h3&gt;

&lt;p&gt;Initialize your modules by constructing new objects, passing in the appropriate selectors as arguments. Assign the newly created object to a property on your global object. A good convention is to use properties beginning with capital letters for modules, and properties beginning with lowercase letters for instances of those modules. Using this approach, you can have multiple instances of the same widget on one page with explicit control over each individual instance. This is useful both for interaction between widgets and for development in the web inspector in your browser.&lt;/p&gt;

&lt;p&gt;Deferring initialization until a module is instantiated programmaticaly gives you great flexibility when testing. You can isolate the effect of your module to a particular subtree of the DOM, which can be cleared out in a teardown phase after each test.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;FOO.comments = &lt;span class="keyword"&gt;new&lt;/span&gt; FOO.Widgets.CommentBox(&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;.comment-box&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;);
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;Just the beginning&lt;/h2&gt;

&lt;p&gt;These are just a few very simple examples of ways to structure your code as your application starts to grow. For even better object-oriented abstractions, consider using a library like &lt;a href="http://documentcloud.github.com/backbone/"&gt;Backbone&lt;/a&gt;, which, although usually associated with Gmail-style single page applications, is also very useful for writing well-organized JavaScript that focuses on behavior and application logic instead of selectors tying it to the markup and heavy chains of callbacks.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/oYoaVNl6oJA" height="1" width="1"/&gt;</description>
      <pubDate>Wed, 11 Jul 2012 01:26:23 -0700</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/oYoaVNl6oJA/jquery-is-not-an-architecture</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/jquery-is-not-an-architecture</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/jquery-is-not-an-architecture</feedburner:origLink></item>
    <item>
      <title>Please don't use Cucumber</title>
      <description>&lt;p&gt;&lt;a href="http://cukes.info/"&gt;Cucumber&lt;/a&gt; is by far my least favorite thing in the Ruby ecosystem, and also the worst example of &lt;a href="http://en.wikipedia.org/wiki/Cargo_cult_programming"&gt;cargo cult programming&lt;/a&gt;. Cucumber has almost no practical benefit over acceptance testing in pure Ruby with &lt;a href="https://github.com/jnicklas/capybara"&gt;Capybara&lt;/a&gt;. I understand the philosophical goals behind behavior driven development, but in the real world, Cucumber is a solution looking for a problem.&lt;/p&gt;

&lt;p&gt;The fact that Cucumber has gained the popularity it has in the Ruby community is outright baffling to me. All the reasons to use it that people give are theoretical, and I have never seen them matter or be remotely applicable in the real world. Cucumber aims to bridge the gap between software developers and non-technical stakeholders, but the reality is that product managers don't really care about Gherkin. Their time is better spent brainstorming all the various use cases for a feature and communicating this either verbally or in free form text. Reading (and especially writing) Gherkin is a waste of their time, because Gherkin is not English. It's extremely inflexible Ruby disguised as English. The more naturally it reads, the more difficult it is to translate it into reusable code via step definitions.&lt;/p&gt;

&lt;p&gt;There are basically two extremes of Cucumber:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Writing Gherkin describing the feature at a very high level, and reusing few of the step definitions between features.&lt;/li&gt;
&lt;li&gt;Reusing step definitions, resulting in a level of detail described in the Gherkin which is not useful for any of the stakeholders.&lt;/li&gt;
&lt;/ol&gt;&lt;p&gt;Everything in between is just a bad compromise of one or the other.&lt;/p&gt;

&lt;p&gt;Gherkin is really just glorified comments. If you simply write free form comments above Capybara scenarios, you can convey the same high level information about what the test is doing and what the acceptance criteria are, without any of the overhead, maintenance cost, and general technical debt of Cucumber. This doesn't allow for the real red-green-refactor cycle from the outside in of the BDD philosophy, but in my experience, developers tend to avoid the test-first approach with Cucumber simply because it's so painful to use. If you're not really following BDD practices, and your non-technical stakeholders are not reading or writing Gherkin, Cucumber is wasting your developers' time and bloating your test suite.&lt;/p&gt;

&lt;p&gt;The one advantage Cucumber offers over simply commenting Capybara scenarios is that, by tying the "English" directly to the implementation, it's impossible for the "comment" to rot. This is certainly a danger, as a misleading comment is worse than no comment at all. However, this benefit comes at an extremly heavy cost. I would argue that it should simply be the discipline of developers to make sure that any time a Capybara scenario is updated, the corresponding comment is read through and updated as necessary.&lt;/p&gt;

&lt;p&gt;Whenever someone writes a criticism of a particular piece of software, there is always a group of people who respond by saying, "It's just a tool. If it works for you, use it. If it doesn't, don't." While I agree in theory, this is where the effect of the cargo cult becomes real and damaging. Some guy somewhere came up with this idea that seemed great in theory, and everyone jumped on the bandwagon doing it because it sounded cool and it seemed like something they &lt;em&gt;should&lt;/em&gt; do. After a while, people choose to use it just because it became the status quo. They don't see that all the reasons a tool like Cucumber seemed like a good idea, based on some blog post they read 3 years ago, are not in tune with the real, practical needs of their project or their organization. And once that choice has been made, everyone has to live with the increasing technical debt and slowed, painful development it creates.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/nZQNTX_DLkc" height="1" width="1"/&gt;</description>
      <pubDate>Thu, 31 May 2012 19:52:56 -0700</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/nZQNTX_DLkc/please-don-t-use-cucumber</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/please-don-t-use-cucumber</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/please-don-t-use-cucumber</feedburner:origLink></item>
    <item>
      <title>Constraints and compromises in ECMAScript 6</title>
      <description>&lt;p&gt;This past week I attended a talk by &lt;a href="http://calculist.org/"&gt;Dave Herman&lt;/a&gt;, an employee of Mozilla and member of ECMA TC39, who are in charge of the standard for JavaScript. Dave talked about a few of the features and improvements coming to JS in ECMAScript 6, including, what I think is the most exciting and desperately needed: the new module system.&lt;/p&gt;

&lt;p&gt;I won't describe how the new module system actually works in this post, as that information is already available elsewhere (in particular at the &lt;a href="http://wiki.ecmascript.org/doku.php?id=harmony:modules"&gt;ES Wiki&lt;/a&gt;). While this new module system looks great, I was concerned with the fact that it eschews the current community built around Node and its CommonJS module loading system. I asked Dave what the reasoning for this was. It boiled down to wanting to provide features for module loading which would not be possible using Node's current CommonJS system. Interoperability between client and server side JavaScript programs will eventually be achieved by converting existing code targeted for Node to the new module system.&lt;/p&gt;

&lt;p&gt;While this may be a painful migration due to the amount of existing code using CommonJS, it seems like the best choice, given that the amount of JavaScript code targeting Node is dwarfed by the amount targeting the browser. Node will also be able to begin the conversion process fairly soon, as it only needs to wait for the implementation of ECMAScript 6 modules in V8.&lt;/p&gt;

&lt;p&gt;Another feature Dave talked about was variable interpolation in strings via so-called &lt;a href="http://wiki.ecmascript.org/doku.php?id=harmony:quasis"&gt;quasi-literals&lt;/a&gt;. This feature is something very common in other high level languages, but to date JavaScript has relied on the concatenation of strings and variables with the &lt;code&gt;+&lt;/code&gt; operator to achieve this. ES6, somewhat confusingly, uses the &lt;code&gt;`&lt;/code&gt; (backtick) to surround these quasi-literals and interpolates variables with &lt;code&gt;${varname}&lt;/code&gt; syntax. I was also curious about the reason for this awkward choice, given Ruby and CoffeeScript's precedence for using double-quoted strings with embedded expressions in &lt;code&gt;#{}&lt;/code&gt;, but Dave had the answer. Backticks were chosen for backwards compatibility. If existing strings were to suddenly gain this ability, existing code on the web that happened to have literal occurrences of &lt;code&gt;${&lt;/code&gt; in them would become syntax or reference errors. The most important constraint TC39 must embrace, unlike languages which compile to JavaScript, is that changes to the language must not break existing code on the web.&lt;/p&gt;

&lt;p&gt;Backticks, even though they are often used to execute shell commands in other languages, were chosen simply due to limited set of ASCII characters remaining for new syntax. Again, the syntax they chose here is not ideal, but given the constraints necessary for advancing JavaScript without breaking the web as it exists today, it's a pretty decent compromise.&lt;/p&gt;

&lt;p&gt;I'd like to thank Dave again for his talk. I'm looking forward to ES6!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/YY6lC3HtEVk" height="1" width="1"/&gt;</description>
      <pubDate>Sun, 22 Apr 2012 22:28:59 -0700</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/YY6lC3HtEVk/constraints-and-compromises-in-ecmascript-6</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/constraints-and-compromises-in-ecmascript-6</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/constraints-and-compromises-in-ecmascript-6</feedburner:origLink></item>
    <item>
      <title>New city, new job, new life</title>
      <description>&lt;p&gt;I haven't posted anything about myself in a while, so I'm happy to present this news: I have moved from San Diego to San Francisco, and am now happily employed by &lt;a href="http://www.change.org" title="Change.org"&gt;Change.org&lt;/a&gt;. This move was a long time coming. Many of my friends in college were from the bay area, as well as more friends I met through those friends. Everyone talked it up and seemed eager to return. In addition to the social motivation, San Francisco has a climate and culture that is much more akin to my taste. It's also one of the biggest tech hubs in the world, so career-wise it was a great choice as well.&lt;/p&gt;

&lt;p&gt;I just finished my third week as a software engineer for Change.org. I'm happy to be spending my day working with technologies I love (Ruby, Rails, JavaScript, etc.), and even more so to be doing it to support a company with an awesome mission. Change.org has been receiving a lot of press lately and is growing very fast, and it's exciting to work for a company whose success I have a real interest in.&lt;/p&gt;

&lt;p&gt;Other than &lt;a href="http://sfappeal.com/news/2012/01/sfps-responds-to-two-shootings-friday-night.php" title="Muni shooting"&gt;one scary incident&lt;/a&gt; on a Muni bus, San Francisco has been a blast to live in so far. I'm enjoying the overwhelming amount of places to eat, the ease of getting around, the cool weather, and the cool people I've met so far. Meeting people in the Ruby and open source communities I've only followed online has me a bit star struck.&lt;/p&gt;

&lt;p&gt;I will miss a few friends and the &lt;a href="http://sdruby.org/" title="SD Ruby"&gt;San Diego Ruby community&lt;/a&gt; I left behind, but San Francisco is clearly where I belong. Here's to a fantastic 2012 and beyond.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/pduHYfJB6Cs" height="1" width="1"/&gt;</description>
      <pubDate>Sat, 11 Feb 2012 22:09:42 -0800</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/pduHYfJB6Cs/new-city-new-job-new-life</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/new-city-new-job-new-life</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/new-city-new-job-new-life</feedburner:origLink></item>
    <item>
      <title>Bang goes 1.0, now with Hubot support</title>
      <description>&lt;p&gt;I just released version 1.0 of my command line utility, &lt;a href="https://github.com/jimmycuadra/bang"&gt;Bang&lt;/a&gt;. Bang is a simple key value store for quickly storing and retrieving text snippets. I &lt;a href="/posts/bang-text-snippets-on-the-command-line-with-nodejs"&gt;talked about it in detail&lt;/a&gt; last month, so be sure to check out that post for samples of usage.&lt;/p&gt;

&lt;p&gt;Version 1.0 merely fixes a few bugs and establishes the public API according to &lt;a href="http://semver.org/"&gt;Semantic Versioning&lt;/a&gt;. Install Bang via NPM: &lt;code&gt;npm install -g bang&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Perhaps the more exciting news is that you can now use Bang to smarten up your &lt;a href="http://hubot.github.com/"&gt;Hubot&lt;/a&gt;, GitHub's awesome chat bot! The Bang script for Hubot is available in the &lt;a href="https://github.com/github/hubot-scripts"&gt;hubot-scripts&lt;/a&gt; repository. Clone the the repository, copy &lt;code&gt;bang.coffee&lt;/code&gt; into your own Hubot's scripts, and add &lt;code&gt;bang&lt;/code&gt; and &lt;code&gt;shellwords&lt;/code&gt; to your package.json file. Deploy your Hubot and enjoy!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/dpO4WFuJHck" height="1" width="1"/&gt;</description>
      <pubDate>Thu, 15 Dec 2011 19:59:54 -0800</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/dpO4WFuJHck/bang-goes-10-now-with-hubot-support</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/bang-goes-10-now-with-hubot-support</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/bang-goes-10-now-with-hubot-support</feedburner:origLink></item>
    <item>
      <title>Bang: text snippets on the command line with Node.js</title>
      <description>&lt;p&gt;There's a great Ruby gem called &lt;a href="https://github.com/holman/boom"&gt;Boom&lt;/a&gt; by &lt;a href="https://github.com/holman"&gt;Zach Holman&lt;/a&gt; for managing text snippets via the command line. I've used it since it was released and have even contributed to it a few times. But after using it for a long time, I realize that I don't really need the "lists" feature – the ability to store snippets with keys two levels deep. Because of Boom's syntax, I often accidentally create lists because I can't quite remember the name of the key I'm looking for.&lt;/p&gt;

&lt;p&gt;I decided this was a good prompt for a new program, so I created &lt;a href="https://github.com/jimmycuadra/bang"&gt;Bang&lt;/a&gt;. Bang is a module for &lt;a href="http://nodejs.org"&gt;Node&lt;/a&gt; that gives you a very simple key value store with one level of depth. I use it to store all sorts of things: articles I refer to often, simple code snippets, Imgur links to animated GIFs, and strings with Unicode characters that are a pain to type.&lt;/p&gt;

&lt;p&gt;For those, like me, who are experimenting with Node, &lt;a href="http://coffeescript.org"&gt;CoffeeScript&lt;/a&gt;, testing using &lt;a href="http://pivotal.github.com/jasmine/"&gt;Jasmine&lt;/a&gt;, and annotated source using &lt;a href="http://jashkenas.github.com/docco/"&gt;Docco&lt;/a&gt;, you will want to check out the source on GitHub to see some examples of all of these.&lt;/p&gt;

&lt;h2&gt;Install Bang&lt;/h2&gt;

&lt;p&gt;Install &lt;a href="http://nodejs.org/"&gt;Node&lt;/a&gt; and &lt;a href="http://npmjs.org/"&gt;npm&lt;/a&gt;.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;$ npm install -g bang
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;Set a key&lt;/h2&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;$ bang apples oranges
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;Get a key&lt;/h2&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;$ bang apples
oranges
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;"oranges" is now copied to your clipboard.&lt;/p&gt;

&lt;h2&gt;Delete a key&lt;/h2&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;$ bang -d apples
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;List keys&lt;/h2&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;$ bang
  foo: bar
 blah: bleh
jimmy: cuadra
 bang: is rad
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;Feedback&lt;/h2&gt;

&lt;p&gt;I hope you enjoy Bang and find it useful. If you have a problem or suggestion, feel free to open an issue or send a pull request.&lt;/p&gt;

&lt;h2&gt;TL;DR&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm install -g bang&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Check out &lt;a href="https://github.com/jimmycuadra/bang"&gt;Bang&lt;/a&gt; on GitHub&lt;/li&gt;
&lt;li&gt;Read the &lt;a href="http://jimmycuadra.github.com/bang/"&gt;annotated source code&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/iqqaFx8-9AQ" height="1" width="1"/&gt;</description>
      <pubDate>Mon, 14 Nov 2011 01:33:45 -0800</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/iqqaFx8-9AQ/bang-text-snippets-on-the-command-line-with-nodejs</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/bang-text-snippets-on-the-command-line-with-nodejs</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/bang-text-snippets-on-the-command-line-with-nodejs</feedburner:origLink></item>
    <item>
      <title>CoffeeScript classes: under the hood</title>
      <description>&lt;p&gt;CoffeeScript has a very elegant mechanism for creating classes. If you're new to JavaScript, you may not be aware that there are no native classes, and that CoffeeScript classes are actually syntactic sugar for JavaScript's constructor functions and object prototypes. Most people are more familiar with the syntax offered by CoffeeScript, but it's a good idea to know what's happening behind the scenes.&lt;/p&gt;

&lt;h2&gt;The prototypal model&lt;/h2&gt;

&lt;p&gt;In JavaScript, there are no classes, in the classical sense. Objects are created and inherit directly from other objects. Functions are a type of object, and when invoked with the &lt;code&gt;new&lt;/code&gt; operator, create a new object which use the function as a constructor. This new object has a hidden link to the &lt;strong&gt;prototype&lt;/strong&gt; property of the constructor function that establishes its inheritance. If you attempt to access a property on the new object that doesn't exist, JavaScript will follow the inheritance chain to the constructor's prototype and use the value it finds there.&lt;/p&gt;

&lt;p&gt;When you create a class in CoffeeScript, you're really creating a constructor function. Each instance method in the class is actually a property of the constructor's prototype.&lt;/p&gt;

&lt;h2&gt;A simple class&lt;/h2&gt;

&lt;p&gt;Let's take a look at an example class, &lt;code&gt;Bear&lt;/code&gt;. A bear is very simple. It has one property, &lt;code&gt;name&lt;/code&gt;, and one method, &lt;code&gt;attack&lt;/code&gt;. A class delcared with the &lt;code&gt;class&lt;/code&gt; keyword creates an empty constructor function. The class definition is followed by an object defining the properties of the constructor's prototype. Each key in the object becomes a property on the prototype. The special key &lt;code&gt;constructor&lt;/code&gt; becomes the body of the constructor function itself.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;class Bear
  constructor: (@name) -&amp;gt;

  attack: -&amp;gt;
    "#{@name} the bear attacks with his bare paws!"

oswalt = new Bear "Oswalt"
console.log oswalt.attack()
# Oswalt the bear attacks with his bare paws!
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;code&gt;Bear&lt;/code&gt; defines a simple constructor which takes one argument, the bear's name, and assigns it to the &lt;code&gt;name&lt;/code&gt; property of the instance. The &lt;code&gt;attack&lt;/code&gt; method simply returns a string saying the bear is attacking. We instantiate a new &lt;code&gt;Bear&lt;/code&gt; named "Oswalt" and log the results of his &lt;code&gt;attack&lt;/code&gt; method. This outputs "Oswalt the bear attacks with his bare paws!" to the console. Let's take a look at how CoffeeScript translates this into JavaScript. (I've left out some closures and added whitespace for clarity.)&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;var Bear, oswalt;

function Bear(name) {
  this.name = name;
}

Bear.prototype.attack = function() {
  return "" + this.name + " the bear attacks with his bare paws!";
};

oswalt = new Bear("Oswalt");
console.log(oswalt.attack());
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;As we can see, the &lt;code&gt;Bear&lt;/code&gt; class is really just a function named &lt;code&gt;Bear&lt;/code&gt;. It takes one argument and assigns it to the new instance's &lt;code&gt;name&lt;/code&gt; property. The &lt;code&gt;attack&lt;/code&gt; method is just a function assigned to the &lt;code&gt;attack&lt;/code&gt; property of &lt;code&gt;Bear&lt;/code&gt;'s prototype. We instantiate an object by calling &lt;code&gt;new Bear&lt;/code&gt; and passing it the bear's name. When we attempt to access the &lt;code&gt;attack&lt;/code&gt; property of the new object, JavaScript sees there there is no such property, and travels up the hidden link to the class's prototype, where it finds the method we want and executes it.&lt;/p&gt;

&lt;h2&gt;Inheritance&lt;/h2&gt;

&lt;p&gt;CoffeeScript's class syntax is a bit cleaner than the compiled JavaScript, but ultimately not that different. Where CoffeeScript classes really shine is in abstracting away the clunky steps required to produce classical inhertiance via the prototypal model. Let's extend &lt;code&gt;Bear&lt;/code&gt; to see how it works.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;class BearWithChainsawPaws extends Bear
  attack: -&amp;gt;
    super + " BY THE WAY, HIS PAWS ARE CHAINSAWS."

rodrigo = new BearWithChainsawHands "Rodrigo"
console.log rodrigo.attack()
# Rodrigo the bear attacks with his bare paws! BY THE WAY, HIS PAWS ARE CHAINSAWS.
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;This new &lt;code&gt;BearWithChainsawPaws&lt;/code&gt; class is a child class of &lt;code&gt;Bear&lt;/code&gt;. It overrides the &lt;code&gt;attack&lt;/code&gt; method but does not change the constructor. Note that the new &lt;code&gt;attack&lt;/code&gt; method uses &lt;code&gt;super&lt;/code&gt; to call &lt;code&gt;Bear&lt;/code&gt;'s version of &lt;code&gt;attack&lt;/code&gt;. This is very useful, because there isn't a direct equivalent of &lt;code&gt;super&lt;/code&gt; in JavaScript, as you can see in the compiled code (again modified for clarity):&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;var&lt;/span&gt; Bear, BearWithChainsawHands, rodrigo;

&lt;span class="keyword"&gt;var&lt;/span&gt; __hasProp = Object.prototype.hasOwnProperty;

&lt;span class="keyword"&gt;var&lt;/span&gt; &lt;span class="function"&gt;__extends&lt;/span&gt; = &lt;span class="keyword"&gt;function&lt;/span&gt;(child, parent) {
  &lt;span class="keyword"&gt;for&lt;/span&gt; (&lt;span class="keyword"&gt;var&lt;/span&gt; key &lt;span class="keyword"&gt;in&lt;/span&gt; parent) {
    &lt;span class="keyword"&gt;if&lt;/span&gt; (__hasProp.call(parent, key)) {
      child[key] = parent[key];
    }
  }

  &lt;span class="keyword"&gt;function&lt;/span&gt; &lt;span class="function"&gt;ctor&lt;/span&gt;() { &lt;span class="local-variable"&gt;this&lt;/span&gt;.constructor = child; }

  ctor.prototype = parent.prototype;
  child.prototype = &lt;span class="keyword"&gt;new&lt;/span&gt; ctor;
  child.__super__ = parent.prototype;

  &lt;span class="keyword"&gt;return&lt;/span&gt; child;
};

&lt;span class="keyword"&gt;function&lt;/span&gt; &lt;span class="function"&gt;Bear&lt;/span&gt;(name) {
  &lt;span class="local-variable"&gt;this&lt;/span&gt;.name = name;
}

Bear.prototype.&lt;span class="function"&gt;attack&lt;/span&gt; = &lt;span class="keyword"&gt;function&lt;/span&gt;() {
  &lt;span class="keyword"&gt;return&lt;/span&gt; &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt; + &lt;span class="local-variable"&gt;this&lt;/span&gt;.name + &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt; the bear attacks with his bare paws!&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;;
};

__extends(BearWithChainsawHands, Bear);

&lt;span class="keyword"&gt;function&lt;/span&gt; &lt;span class="function"&gt;BearWithChainsawHands&lt;/span&gt;() {
  BearWithChainsawHands.__super__.constructor.apply(&lt;span class="local-variable"&gt;this&lt;/span&gt;, &lt;span class="local-variable"&gt;arguments&lt;/span&gt;);
}

BearWithChainsawHands.prototype.&lt;span class="function"&gt;attack&lt;/span&gt; = &lt;span class="keyword"&gt;function&lt;/span&gt;() {
  &lt;span class="keyword"&gt;return&lt;/span&gt; BearWithChainsawHands.__super__.attack.apply(&lt;span class="local-variable"&gt;this&lt;/span&gt;, &lt;span class="local-variable"&gt;arguments&lt;/span&gt;) + &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt; BY THE WAY, HIS PAWS ARE CHAINSAWS.&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;;
};

rodrigo = &lt;span class="keyword"&gt;new&lt;/span&gt; BearWithChainsawHands(&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;Rodrigo&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;);
console.log(rodrigo.attack());
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;There's a lot happening here, so let's take it a bit at a time.&lt;/p&gt;

&lt;p&gt;The first thing to notice here is that CoffeeScript has generated some boilerplate above the classes themselves that wasn't there in the first example. &lt;code&gt;__hasProp&lt;/code&gt; is just a shortcut for &lt;code&gt;Object.prototype.hasOwnProperty&lt;/code&gt;. Since it's used in a loop in the following function, this is a bit more performant. It's not particularly important in understanding how inheritance works, however. The real meat is the next function.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;__extends&lt;/code&gt; is a helper function that sets up one class to inherit from another. The &lt;code&gt;for...in&lt;/code&gt; loop copies all the class-level methods from the parent to the child. In this particular case, our classes have only instance methods, but if we were to have defined a class property, say &lt;code&gt;Bear.awesome = true&lt;/code&gt;, then the loop would copy &lt;code&gt;true&lt;/code&gt; into &lt;code&gt;BearWithChainsawPaws.awesome&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The second half of &lt;code&gt;__extends&lt;/code&gt; sets up the prototype link (which contains the instance properties and methods) from the child to the parent. At its simplest, prototypal inheritance is achieved by assigning an instance of the parent to the child's prototype. &lt;code&gt;__extends&lt;/code&gt; uses a bit of indirection to both establish this link, and to correctly assign the &lt;code&gt;constructor&lt;/code&gt; property for all instances of the child. This property points back at the constructor function itself, and is necessary for the &lt;code&gt;instanceof&lt;/code&gt; operator to give the desired result. The intermediate &lt;code&gt;ctor&lt;/code&gt; method is used to for this purpose.&lt;/p&gt;

&lt;p&gt;Lastly, a &lt;code&gt;__super__&lt;/code&gt; property is added to the child class, which establishes a reference to the parent's prototype. Without this, achieving &lt;code&gt;super&lt;/code&gt; would require manually calling the parent class by name, which is error prone and not particularly maintainable. Inside &lt;code&gt;BearWithChainsawHands&lt;/code&gt;'s methods, we can see this reference to &lt;code&gt;__super__&lt;/code&gt; being used to call &lt;code&gt;Bear&lt;/code&gt;'s methods through the magic of &lt;code&gt;apply&lt;/code&gt; – a method which invokes one object's method within the context of another object.&lt;/p&gt;

&lt;h2&gt;The point&lt;/h2&gt;

&lt;p&gt;While I do think CoffeeScript is pretty rad, the point of this post is to aid in understanding of what CoffeeScript is doing behind the scenes to provide its niceties. It uses good, clean JavaScript to abstract away what, to many, is an awkward approach, and in doing so, teaches a great deal about how JavaScript works.&lt;/p&gt;

&lt;h2&gt;Further reading&lt;/h2&gt;

&lt;p&gt;This post covers a lot of ground in a short span, and many parts of it could be explained in more depth. Here are some links to the &lt;a href="https://developer.mozilla.org/en/JavaScript"&gt;MDC docs&lt;/a&gt; with more detail that should be helpful:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/new"&gt;new&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/constructor"&gt;constructor&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en/JavaScript/Guide/Inheritance_constructor_prototype"&gt;prototype and inheritance&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/Special/instanceof"&gt;instanceof&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call"&gt;call&lt;/a&gt; and &lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply"&gt;apply&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/YjZuaCSisAk" height="1" width="1"/&gt;</description>
      <pubDate>Fri, 14 Oct 2011 04:13:07 -0700</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/YjZuaCSisAk/coffeescript-classes-under-the-hood</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/coffeescript-classes-under-the-hood</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/coffeescript-classes-under-the-hood</feedburner:origLink></item>
    <item>
      <title>ECMAScript 5: Array methods</title>
      <description>&lt;p&gt;&lt;aside&gt;This is the third part in a series of posts about ECMAScript 5. Be sure to check out the &lt;a href="/posts/ecmascript-5-object-creation-and-property-definition"&gt;first part&lt;/a&gt;, and &lt;a href="/posts/ecmascript-5-tamper-proofing-objects"&gt;second part&lt;/a&gt; if you missed them.&lt;/aside&gt;&lt;/p&gt;

&lt;p&gt;This is the third and final part of my series on ECMAScript 5. In &lt;a href="/posts/ecmascript-5-object-creation-and-property-definition"&gt;part one&lt;/a&gt;, we looked at new methods for object creation and property definition. &lt;a href="/posts/ecmascript-5-tamper-proofing-objects"&gt;Part two&lt;/a&gt; focused on tamper proofing objects. I'll now provide a quick overview of the new high level array methods.&lt;/p&gt;

&lt;p&gt;Unlike the new methods discussed in the first two parts, the methods here are all reproducible using JavaScript itself. Native implementations are simply faster and more convenient. Having a uniform API for these operations also promotes their usage, making code clearer when shared between developers.&lt;/p&gt;

&lt;h2&gt;Search methods&lt;/h2&gt;

&lt;h3&gt;Array.prototype.indexOf&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;indexOf&lt;/code&gt; provides an easy way to determine whether or not an object is in an array. It returns the first index at which the item was found, or &lt;code&gt;-1&lt;/code&gt; if it was not found at all. Strict equality is used to determine that an item is present in the array. An optional second argument can start the search at an index other than &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;var&lt;/span&gt; arr = [&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;banana&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;carrot&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;];

arr.indexOf(&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;); &lt;span class="comment"&gt;// 0&lt;/span&gt;
arr.indexOf(&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;daikon&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;); &lt;span class="comment"&gt;// -1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;Array.prototype.lastIndexOf&lt;/h3&gt;

&lt;p&gt;Identical to &lt;code&gt;indexOf&lt;/code&gt;, but returns the last index at which an item is found, if at all.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;var&lt;/span&gt; arr = [&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;banana&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;carrot&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;];

arr.lastIndexOf(&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;); &lt;span class="comment"&gt;// 3&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;Iteration methods&lt;/h2&gt;

&lt;h3&gt;Array.prototype.forEach&lt;/h3&gt;

&lt;p&gt;Ever seen this before?&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;for&lt;/span&gt; (&lt;span class="keyword"&gt;var&lt;/span&gt; i = &lt;span class="integer"&gt;0&lt;/span&gt;, l = arr.length; i &amp;lt; l; i++) {
  doSomething(arr[i], i, arr);
}
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;There is now a clean, high level way to do this with &lt;code&gt;forEach&lt;/code&gt;. The method accepts a callback which will be executed once for each item in the array. The callback receives three arguments: the value of the current iteration, the index of the current iteration, and the array itself. The following is equivalent to the above:&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;arr.forEach(doSomething);
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;An optional second argument specifies the value of &lt;code&gt;this&lt;/code&gt; within the callback.&lt;/p&gt;

&lt;h3&gt;Array.prototype.every&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;every&lt;/code&gt; checks every element in an array against a condition, by means of a passed in function. If any of the items in the array cause the function to return a falsy value, &lt;code&gt;every&lt;/code&gt; returns &lt;code&gt;false&lt;/code&gt;. If they all return truthy values, &lt;code&gt;every&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt;.  Like &lt;code&gt;forEach&lt;/code&gt;, an optional second argument supplies a value for the callback's &lt;code&gt;this&lt;/code&gt;, and the callback itself will receive the same three arguments.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;var&lt;/span&gt; arr = [&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;banana&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;carrot&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;];

arr.every(&lt;span class="keyword"&gt;function&lt;/span&gt; (value, index, array) { &lt;span class="keyword"&gt;return&lt;/span&gt; value.length &amp;gt; &lt;span class="integer"&gt;1&lt;/span&gt;; }); &lt;span class="comment"&gt;// true&lt;/span&gt;
arr.every(&lt;span class="keyword"&gt;function&lt;/span&gt; (value, index, array) { &lt;span class="keyword"&gt;return&lt;/span&gt; value.length &amp;lt; &lt;span class="integer"&gt;6&lt;/span&gt;; }); &lt;span class="comment"&gt;// false&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;Array.prototype.some&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;some&lt;/code&gt; is similar to &lt;code&gt;every&lt;/code&gt;, but the passing condition is that &lt;em&gt;at least one&lt;/em&gt; callback returns true, rather than all of them.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;var&lt;/span&gt; arr = [&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;banana&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;carrot&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;apple&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;];

arr.some(&lt;span class="keyword"&gt;function&lt;/span&gt; (value, index, array) { &lt;span class="keyword"&gt;return&lt;/span&gt; value.length &amp;lt; &lt;span class="integer"&gt;6&lt;/span&gt;; }); &lt;span class="comment"&gt;// true&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;Transformation methods&lt;/h2&gt;

&lt;h3&gt;Array.prototype.map&lt;/h3&gt;

&lt;p&gt;Mapping is the most common transformation. It loops through an array, running a function and creating a new array built from the return values of each iteration. &lt;code&gt;map&lt;/code&gt; takes the same optional second argument and receives the same callback arguments as the iteration methods.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;var&lt;/span&gt; names = [&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;Abigail&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;Bongo&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;Carlitos&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;];

names.map(&lt;span class="keyword"&gt;function&lt;/span&gt; (value, index, array) {
  &lt;span class="keyword"&gt;return&lt;/span&gt; value + &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt; Jones&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;;
});
&lt;span class="comment"&gt;// ["Abigal Jones", "Bongo Jones", "Carlitos Jones"]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;Array.prototype.filter&lt;/h3&gt;

&lt;p&gt;Filtering is like mapping, but creates a new array containing only the items of the original array that return a truthy value from the callback. &lt;code&gt;filter&lt;/code&gt; takes the familiar optional argument and its callback receives the usual suspects.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span class="keyword"&gt;var&lt;/span&gt; names = [&lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;Abigail&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;Bongo&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;, &lt;span class="string"&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;span class="content"&gt;Carlitos&lt;/span&gt;&lt;span class="delimiter"&gt;"&lt;/span&gt;&lt;/span&gt;];

names.filter(&lt;span class="keyword"&gt;function&lt;/span&gt; (value, index, array) {
  &lt;span class="keyword"&gt;return&lt;/span&gt; value.length &amp;gt; &lt;span class="integer"&gt;5&lt;/span&gt;
});
&lt;span class="comment"&gt;// ["Abigail", "Carlitos"]&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;Array.prototype.reduce&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;reduce&lt;/code&gt; is used to melt the items in an array down to a single value by the operations performed in its callback function. The callback takes the usual three iteration arguments, but is prepended with an extra argument, the value of the previous iteration's result. By default, the first iteration is effectively a no-op, so the calculation begins with the array's first value as the accumulated result and the array's second value as the current iteration. This is probably best illustrated with an example:&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;[&lt;span class="integer"&gt;10&lt;/span&gt;, &lt;span class="integer"&gt;20&lt;/span&gt;, &lt;span class="integer"&gt;30&lt;/span&gt;, &lt;span class="integer"&gt;40&lt;/span&gt;, &lt;span class="integer"&gt;50&lt;/span&gt;].reduce(&lt;span class="keyword"&gt;function&lt;/span&gt; (accum, value, index, array) {
  &lt;span class="keyword"&gt;return&lt;/span&gt; accum + value;
});
&lt;span class="comment"&gt;// 150&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;On the first iteration, &lt;code&gt;accum&lt;/code&gt; is &lt;code&gt;10&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; is &lt;code&gt;20&lt;/code&gt;. The return value of &lt;code&gt;accum + value&lt;/code&gt; becomes &lt;code&gt;accum&lt;/code&gt; on the next iteration of the loop.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;reduce&lt;/code&gt; can also take an initial value for &lt;code&gt;accum&lt;/code&gt; as a second argument. When invoked this way, the first &lt;code&gt;value&lt;/code&gt; in the callback is the first item in the array:&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;[&lt;span class="integer"&gt;10&lt;/span&gt;, &lt;span class="integer"&gt;20&lt;/span&gt;, &lt;span class="integer"&gt;30&lt;/span&gt;, &lt;span class="integer"&gt;40&lt;/span&gt;, &lt;span class="integer"&gt;50&lt;/span&gt;].reduce(&lt;span class="keyword"&gt;function&lt;/span&gt; (accum, value, index, array) {
  &lt;span class="keyword"&gt;return&lt;/span&gt; accum + value;
}, &lt;span class="integer"&gt;50&lt;/span&gt;);
&lt;span class="comment"&gt;// 200&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;Array.prototype.reduceRight&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;reduce&lt;/code&gt;'s sibling, which performs the same behavior and accepts the same arguments, except it iterates beginning with the rightmost item in the array and moves left. If not specified with a argument, the initial value of &lt;code&gt;accum&lt;/code&gt; is the last value in the array, and the initial iteration value is the second to last.&lt;/p&gt;

&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;[&lt;span class="integer"&gt;10&lt;/span&gt;, &lt;span class="integer"&gt;20&lt;/span&gt;, &lt;span class="integer"&gt;30&lt;/span&gt;, &lt;span class="integer"&gt;40&lt;/span&gt;, &lt;span class="integer"&gt;50&lt;/span&gt;].reduceRight(&lt;span class="keyword"&gt;function&lt;/span&gt; (accum, value, index, array) {
  &lt;span class="keyword"&gt;return&lt;/span&gt; accum - value;
});
&lt;span class="comment"&gt;// -50&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;ECMAScript 5 is full of goodness&lt;/h2&gt;

&lt;p&gt;ECMAScript 5 packs lots of new, useful features into JavaScript which speed up both development and performance of code. The new APIs are implented in the most recent versions of Chrome, Firefox, Safari, and (*gasp!*) Internet Explorer, so I encourage you to start using these new APIs today!&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/8tUM-WleUoY" height="1" width="1"/&gt;</description>
      <pubDate>Wed, 12 Oct 2011 00:02:57 -0700</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/8tUM-WleUoY/ecmascript-5-array-methods</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/ecmascript-5-array-methods</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/ecmascript-5-array-methods</feedburner:origLink></item>
    <item>
      <title>Extractor.js: simple global object management for JavaScript</title>
      <description>&lt;p&gt;I was messing around with CoffeeScript and Jasmine this evening and put together a very small library called &lt;strong&gt;Extractor&lt;/strong&gt;. Here's the idea:&lt;/p&gt;

&lt;p&gt;Everyone hates the global object in JavaScript, and care must be taken to manage potential variable name conflicts. Extractor works as a proxy for the global object, allowing library developers to register their libraries without touching the global object. Developers who use the library can then extract it into a variable name of their choice, possibly never touching the global object at all. The effect is similar to jQuery's &lt;code&gt;noConflict&lt;/code&gt; function, but is used more like Node's &lt;code&gt;require&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For more information (including usage examples), check out &lt;a href="https://github.com/jimmycuadra/extractor"&gt;Extractor on GitHub&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/jimmycuadra/~4/QmmnUYoZpQo" height="1" width="1"/&gt;</description>
      <pubDate>Fri, 16 Sep 2011 01:19:10 -0700</pubDate>
      <link>http://feedproxy.google.com/~r/jimmycuadra/~3/QmmnUYoZpQo/extractorjs-simple-global-object-management-for-javascript</link>
      <guid isPermaLink="false">http://www.jimmycuadra.com/posts/extractorjs-simple-global-object-management-for-javascript</guid>
    <feedburner:origLink>http://www.jimmycuadra.com/posts/extractorjs-simple-global-object-management-for-javascript</feedburner:origLink></item>
  </channel>
</rss>
