<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" xml:lang="en" xml:base="http://yehudakatz.com/wp-atom.php">
	<title type="text">Katz Got Your Tongue?</title>
	<subtitle type="text">Random Geek-Related Thoughts</subtitle>

	<updated>2012-04-13T19:50:27Z</updated>

	<link rel="alternate" type="text/html" href="http://yehudakatz.com" />
	<id>http://yehudakatz.com/feed/atom/</id>
	

			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/KatzGotYourTongue" /><feedburner:info uri="katzgotyourtongue" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><link rel="license" type="text/html" href="http://creativecommons.org/licenses/by-sa/2.0/" /><entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[Tokaido: My Hopes and Dreams]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/nvgFbXOt8tQ/" />
		<id>http://yehudakatz.com/?p=719</id>
		<updated>2012-04-13T19:50:27Z</updated>
		<published>2012-04-13T07:46:26Z</published>
		<category scheme="http://yehudakatz.com" term="Other" />		<summary type="html"><![CDATA[A few weeks ago, I started a kickstarter project to fund work on a project to make a long-term, sustainable binary build of Ruby. The outpouring of support was great, and I have far exceeded my original funding goal. First, I&#8217;d like to thank everyone in the community who contributed large and small donations. This kickstarter [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2012/04/13/tokaido-my-hopes-and-dreams/">&lt;p&gt;A few weeks ago, I started a kickstarter project to fund work on a project to make a long-term, sustainable binary build of Ruby. The outpouring of support was great, and I have far exceeded my original funding goal. First, I&amp;#8217;d like to thank everyone in the community who contributed large and small donations. This kickstarter couldn&amp;#8217;t have been as successful as it has been without the hundreds (650 at latest count!) of individual donations by interested Rubyists.&lt;/p&gt;
&lt;p&gt;In this post, I want to talk about what my goals are for this project, and why I think it will be a tool that everyone, myself included, will use.&lt;/p&gt;
&lt;h2&gt;What is Tokaido&lt;/h2&gt;
&lt;p&gt;The name &amp;#8220;Tokaido&amp;#8221; (東海道 in Japanese) comes from the &lt;a href="http://en.wikipedia.org/wiki/T%C5%8Dkaid%C5%8D_Shinkansen"&gt;Tōkaidō Shinkansen&lt;/a&gt; bullet train line in Japan.&lt;/p&gt;
&lt;h3&gt;Precompiled, Static Ruby&lt;/h3&gt;
&lt;p&gt;At its core, Tokaido is a binary distribution of Ruby without any external dependencies on your system. This means that Ruby itself, as well as all of the compiled elements of the standard library, will come in a self-contained directory with no additional requirements.&lt;/p&gt;
&lt;p&gt;The binary will also have no hardcoded paths, so it will be possible to move the directory anywhere on the file system and have it continue to work as long as its &lt;code&gt;bin&lt;/code&gt; directory is on the &lt;code&gt;$PATH&lt;/code&gt;. This is an important goal, as Tokaido will ship as a downloadable &lt;code&gt;.app&lt;/code&gt; which should work wherever it is dropped on the file system.&lt;/p&gt;
&lt;h3&gt;Precompiled Binary Gems&lt;/h3&gt;
&lt;p&gt;Tokaido will come with all of the necessary gems to use Rails and common Rails extensions. Because some of these gems have external dependencies (for example, &lt;code&gt;nokogori&lt;/code&gt; depends on &lt;code&gt;libxml2&lt;/code&gt;), Tokaido will come with precompiled versions of these gems built for OSX that do not depend on external libraries (they will be statically compiled, not dynamically linked).&lt;/p&gt;
&lt;p&gt;As part of this project, I plan to work with people who ship common native extensions to help them build and ship binary versions of their gems for OSX without external dependencies. Luis Lavena has been doing amazing work with &lt;a href="https://github.com/luislavena/rake-compiler"&gt;rake-compiler&lt;/a&gt; to make this process easy on gem developers, and Wayne E. Seguin and Michał Papis have been doing great work with &lt;a href="https://github.com/sm/sm"&gt;sm&lt;/a&gt;, which makes it easy to precompile the needed dependencies for inclusion in the precompiled gems. These tools will be essential in the effort to make dependency-free precompiled gems a standard part of the OSX Ruby ecosystem.&lt;/p&gt;
&lt;p&gt;I anticipate that gem authors will, in general, start distributing precompiled binary versions of their gems. If, by the time I ship the first version of Tokaido, some important gems do not yet ship as precompiled binaries, Tokaido will bootstrap the process by including the binaries.&lt;/p&gt;
&lt;h3&gt;Terminal-Based Workflow&lt;/h3&gt;
&lt;p&gt;Tokaido does not aim to replace the Terminal as the main way to work with a Rails app. It will ship an isolated environment with no external dependencies &lt;strong&gt;designed for use in the Terminal&lt;/strong&gt;. The application UI will supplement, rather than replace, the normal application workflow. This is a crucial part of the overall goal to make Tokaido an excellent tool for both experienced Rails developers, intermediate Rails developers, and totally new Rails developers.&lt;/p&gt;
&lt;p&gt;Because many gems come with executables, and Tokaido couldn&amp;#8217;t abstract every possible executable even if it wanted to, it is essential that new developers get used to using the Terminal as early as possible in their Rails experience, but in a way that minimizes unnecessary errors.&lt;/p&gt;
&lt;h3&gt;Extras: Code and App Health&lt;/h3&gt;
&lt;p&gt;A number of really great tools exist to help Rails applications remain healthy:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;bundle outdated&lt;/code&gt; lets you know when new versions of gems are available&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&lt;a href="https://github.com/railsbp/rails_best_practices"&gt;rails_best_practices&lt;/a&gt;&lt;/code&gt; helps find common mistakes in Rails applications&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reek&lt;/code&gt;, &lt;code&gt;flog&lt;/code&gt;, &lt;code&gt;flay&lt;/code&gt;, &lt;code&gt;roodi&lt;/code&gt; and other metrics tools help identify Ruby smells and prioritizes areas where you can reduce your technical debt&lt;/li&gt;
&lt;li&gt;&lt;code&gt;simplecov&lt;/code&gt; does coverage analysis on your test suite to identify areas lacking good coverage&lt;/li&gt;
&lt;li&gt;&lt;code&gt;bullet&lt;/code&gt; identifies N+1 queries and unused eager loading&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ActiveSupport::Notifications&lt;/code&gt; provides introspection information about Rails applications&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The Tokaido UI will attempt to centralize this useful information into a health check, and help you drill in if you want to do more with the information. Because they are so useful, it will encourage you to use these tools in new application, and provide immediate rewards if you do.&lt;/p&gt;
&lt;h3&gt;Extras: yourapp.dev&lt;/h3&gt;
&lt;p&gt;The &lt;a href="http://pow.cx/"&gt;pow&lt;/a&gt; dev server and &lt;a href="https://github.com/Fingertips/passengerpane"&gt;PassengerPane&lt;/a&gt; make it possible to avoid needing to manually load a Rails server &lt;strong&gt;and&lt;/strong&gt; gives you a friendly alias for your application at &lt;code&gt;yourapp.dev&lt;/code&gt; instead of having to type in a port on &lt;code&gt;localhost&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Tokaido will come with integrated support for this, so every app you manage through Tokaido will have a aliased development server out of the box.&lt;/p&gt;
&lt;h3&gt;Extras: rails:// (or ruby://) Protocol Handler&lt;/h3&gt;
&lt;p&gt;Tokaido will come with a protocol handler that will allow &lt;code&gt;.gem&lt;/code&gt; files hosted on rubygems.org or other locations to be added to an app managed by Tokaido. It may also allow web sites to host installation templates that could execute against a maintained application. This will require coordination with rubygems.org, and the specifics of it may change over time, but the basic idea is to enable communication between web sites and the Tokaido app.&lt;/p&gt;
&lt;p&gt;This idea came from a number of community members, and the specifics (especially around security and the protocol specification) definitely need fleshing out, but it seems extremely promising.&lt;/p&gt;
&lt;h3&gt;Extras: Ruby Toolbox Integration?&lt;/h3&gt;
&lt;p&gt;Ruby Toolbox has emerged as an amazing directory of Ruby libraries, categorized by type. It also has up-to-date information on Github activity and other useful indicators when evaluating tools. Several people have asked for integration with Ruby Toolbox, and assuming the author is willing, Tokaido will make it easy to use the information in Ruby Toolbox to bootstrap an app and add new functionality as your app grows.&lt;/p&gt;
&lt;p&gt;Finding the right gem for the job is easy if you know what you&amp;#8217;re looking for, but even experienced developers find the Ruby Toolbox useful when researching tools in an unfamiliar area.&lt;/p&gt;
&lt;h2&gt;Goals of Tokaido&lt;/h2&gt;
&lt;h3&gt;Eliminate Failure Scenarios&lt;/h3&gt;
&lt;p&gt;The primary goal of Tokaido is to build a distribution of Ruby that eliminates the main failure scenarios that people encounter when trying to install Ruby. In general, things fail because of conflicts with the existing system environment (or the user&amp;#8217;s environment). These failures do not happen to everyone, but when they happen, they are extremely difficult to recover from. This has happened to me personally and to people I have tried to get started with Ruby.&lt;/p&gt;
&lt;p&gt;This sort of thing does not necessarily happen on every installation, but once you start going down the rabbit hole, it can be very difficult to find your way out. The environment difficulties can be caused by anything from an old MacPorts installation to a mistaken attempt to install something to the system once upon a time to something failing during a previous step in the installation process.&lt;/p&gt;
&lt;p&gt;It also may not be enough to install a precompiled Ruby into the system, because the system environment itself may be corrupted with things like erroneous executables on the &lt;code&gt;$PATH&lt;/code&gt; or bad dynamic libraries that get linked during native compilation. Also, later installations may do damage to the system-installed Ruby. Tokaido is a standalone environment that is loaded on top of an existing session, and therefore minimizes the possible damage that load order or subsequent installs can do.&lt;/p&gt;
&lt;h3&gt;Precompile Everything&lt;/h3&gt;
&lt;p&gt;In order to eliminate a certain class of failure scenarios, Tokaido will ship with a precompiled Ruby, which will eliminate the possibility of compilation errors when installing Ruby. This precompiled Ruby will also come with all of the dependencies it needs, like &lt;code&gt;zlib&lt;/code&gt;, &lt;code&gt;yaml&lt;/code&gt; and others, instead of allowing the system to try to find them at runtime. This will eliminate the possibility that changes to the system environment will cause a normally working version of Ruby to fail in some scenarios.&lt;/p&gt;
&lt;p&gt;As above, Tokaido will also use this technique for commonly used native gems. For example, Tokaido will ship with a precompiled version of Nokogiri that comes with &lt;code&gt;libxml&lt;/code&gt;, instead of relying on the system&amp;#8217;s copy of &lt;code&gt;libxml&lt;/code&gt; (incidentally, the system&amp;#8217;s &lt;code&gt;libxml&lt;/code&gt; has occasionally been subtly broken, necessitating installation via homebrew). I expect that this will happen because gem authors will start shipping precompiled versions of their gems for OSX. If there are still a few common gems straggling by the time Tokaido ships, we&amp;#8217;ll bootstrap the process by shipping with binary gems we compile ourselves.&lt;/p&gt;
&lt;h3&gt;Use Tokaido Myself&lt;/h3&gt;
&lt;p&gt;Since Tokaido does not fundamentally alter a developer&amp;#8217;s relationship with Ruby, I expect to be able to start using it for day-to-day Rails development. Some of the additional extras, like app health and built-in server support, are things I already do manually and will enjoy having as part of a larger tool. I&amp;#8217;m also extremely interested in ideas for other extras that can add value for experienced developers without altering how people work with Ruby today.&lt;/p&gt;
&lt;h3&gt;Integration Testing&lt;/h3&gt;
&lt;p&gt;One of the coolest, unheralded things about the Rails project is the &lt;a href="http://intertwingly.net/projects/dashboard.html"&gt;integration suite&lt;/a&gt; for Rails put together by Sam Ruby. It essentially transcribes the Agile Web Development With Rails book into a series of very complete integration tests for Rails. When refactoring Rails for Rails 3, this suite was extremely useful, and helped keep the number of unintentional changes to Rails to a tiny number. It also kept us honest about intentional changes.&lt;/p&gt;
&lt;p&gt;This suite is perfect for Tokaido, as it tests every aspect of Rails, which is itself touches a wide swath of Ruby itself. To start, Tokaido will use this suite for integration testing.&lt;/p&gt;
&lt;h3&gt;Collaboration&lt;/h3&gt;
&lt;p&gt;There are several other projects, notably &lt;code&gt;rvm&lt;/code&gt;, trying to solve problems in a similar space. Especially when it comes to precompilation, there is no reason not to work together on the core infrastructure that powers these efforts. I have already started to work with Michał Papis, who also works on &lt;code&gt;sm&lt;/code&gt; and &lt;code&gt;rvm&lt;/code&gt; on shared work that can be useful for both projects. He has been teaching me all about the work that folks associated with rvm have done to simplify things like downloading and compiling &lt;code&gt;libz.a&lt;/code&gt;, a prerequisite for precompiled Rubies.&lt;/p&gt;
&lt;p&gt;I will also work with authors of native gems to start shipping precompiled binary gems for OSX. I have already started working with Aaron Patterson, who works on the &lt;code&gt;sqlite&lt;/code&gt; and &lt;code&gt;nokogiri&lt;/code&gt; gems, and have started reaching out to several others.&lt;/p&gt;
&lt;p&gt;I am very interested in working together with anyone working on a similar area so that the tools we are all working on can be shared between projects. This is a core mission of the Tokaido project, and something that the extra funding I got will allow me to prioritize.&lt;/p&gt;
&lt;h3&gt;Migration to &amp;#8220;System&amp;#8221;&lt;/h3&gt;
&lt;p&gt;Also through Michał Papis, I learned about an awesome new (still experimental) feature in &lt;code&gt;rvm&lt;/code&gt; called mount that will mount an external Ruby into the rvm system. I will make sure that the &lt;code&gt;Tokaido&lt;/code&gt; ruby can be added to an &lt;code&gt;rvm&lt;/code&gt; installation. This will mean that if someone wants to migrate from Tokaido to a more advanced rvm setup, they can take their Ruby environment with them with no trouble.&lt;/p&gt;
&lt;p&gt;I would be open to other approaches to migrating a Tokaido environment to the system as well. The goal would be to seamlessly migrate an environment to the system without introducing opportunities for failure during that process.&lt;/p&gt;
&lt;h2&gt;Looking Forward&lt;/h2&gt;
&lt;p&gt;I&amp;#8217;m really excited to the work I&amp;#8217;ve been doing to prepare for this project, and looking forward to shipping a great environment for OSX Ruby users. I have also really enjoyed reaching out to others working in similar areas and the prospect of collaborating with so many smart people on a shared goal.&lt;/p&gt;
&lt;p&gt;Thanks so much!&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2012%2F04%2F13%2Ftokaido-my-hopes-and-dreams%2F';
  addthis_title  = 'Tokaido%3A+My+Hopes+and+Dreams';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=nvgFbXOt8tQ:Pa8clZyMJio:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/nvgFbXOt8tQ" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2012/04/13/tokaido-my-hopes-and-dreams/#comments" thr:count="2" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2012/04/13/tokaido-my-hopes-and-dreams/feed/atom/" thr:count="2" />
		<thr:total>2</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2012/04/13/tokaido-my-hopes-and-dreams/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[JavaScript Needs Blocks]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/v1ctIJnejg4/" />
		<id>http://yehudakatz.com/?p=698</id>
		<updated>2012-01-10T19:46:36Z</updated>
		<published>2012-01-10T05:36:37Z</published>
		<category scheme="http://yehudakatz.com" term="Other" />		<summary type="html"><![CDATA[While reading Hacker News posts about JavaScript, I often come across the misconception that Ruby&#8217;s blocks are essentially equivalent to JavaScript&#8217;s &#8220;first class functions&#8221;. Because the ability to pass functions around, especially when you can create them anonymously, is extremely powerful, the fact that both JavaScript and Ruby have a mechanism to do so makes [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2012/01/10/javascript-needs-blocks/">&lt;p&gt;While reading Hacker News posts about JavaScript, I often come across the misconception that Ruby&amp;#8217;s blocks are essentially equivalent to JavaScript&amp;#8217;s &amp;#8220;first class functions&amp;#8221;. Because the ability to pass functions around, especially when you can create them anonymously, is extremely powerful, the fact that both JavaScript and Ruby have a mechanism to do so makes it natural to assume equivalence.&lt;/p&gt;
&lt;p&gt;In fact, when people talk about why Ruby&amp;#8217;s blocks are different from &lt;strong&gt;Python&lt;/strong&gt;&amp;#8216;s functions, they usually talk about anonymity, something that Ruby and JavaScript share, but Python does not have. At first glance, a Ruby block is an &amp;#8220;anonymous function&amp;#8221; (or colloquially, a &amp;#8220;closure&amp;#8221;) just as a JavaScript function is one.&lt;/p&gt;
&lt;p&gt;This impression, which I admittedly shared in my early days as a Ruby/JavaScript developer, misses an important subtlety that turns out to have large implications. This subtlety is often referred to as &amp;#8220;Tennent&amp;#8217;s Correspondence Principle&amp;#8221;. In short, Tennent&amp;#8217;s Correspondence Principle says:&lt;/p&gt;
&lt;blockquote style="border-left: 3px solid #999; padding-left: 1em; margin-left: 0"&gt;&lt;p&gt;
&amp;#8220;For a given expression &lt;code&gt;expr&lt;/code&gt;, &lt;code&gt;lambda expr&lt;/code&gt; should be equivalent.&amp;#8221;
&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;This is also known as the principle of abstraction, because it means that it is easy to refactor common code into methods that take a block. For instance, consider the common case of file resource management. Imagine that the block form of &lt;code&gt;File.open&lt;/code&gt; didn&amp;#8217;t exist in Ruby, and you saw a lot of the following in your code:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;begin&lt;/span&gt;
  f = &lt;span style="color:#CC00FF; font-weight:bold;"&gt;File&lt;/span&gt;.&lt;span style="color:#CC0066; font-weight:bold;"&gt;open&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;filename, &lt;span style="color:#996600;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#008000; font-style:italic;"&gt;# do something with f&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;ensure&lt;/span&gt;
  f.&lt;span style="color:#9900CC;"&gt;close&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In general, when you see some code that has the same beginning and end, but a different middle, it is natural to refactor it into a method that takes a block. You would write a method like this:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; read_file&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;filename&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  f = &lt;span style="color:#CC00FF; font-weight:bold;"&gt;File&lt;/span&gt;.&lt;span style="color:#CC0066; font-weight:bold;"&gt;open&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;filename, &lt;span style="color:#996600;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;yield&lt;/span&gt; f
&lt;span style="color:#9966CC; font-weight:bold;"&gt;ensure&lt;/span&gt;
  f.&lt;span style="color:#9900CC;"&gt;close&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;And you&amp;#8217;d refactor instances of the pattern in your code with:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;read_file&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;filename&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color:#9966CC; font-weight:bold;"&gt;do&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;|&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;|&lt;/span&gt;
  &lt;span style="color:#008000; font-style:italic;"&gt;# do something with f&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order for this strategy to work, it&amp;#8217;s important that the code inside the block look the same after refactoring as before. We can restate the correspondence principle &lt;em&gt;in this case&lt;/em&gt; as:&lt;/p&gt;
&lt;blockquote style="border-left: 3px solid #999; padding-left: 1em; margin-left: 0"&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#008000; font-style:italic;"&gt;# do something with f&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;should be equivalent to:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;do&lt;/span&gt;
  &lt;span style="color:#008000; font-style:italic;"&gt;# do something with&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;/blockquote&gt;
&lt;p&gt;At first glance, it looks like this is true in Ruby and JavaScript. For instance, let&amp;#8217;s say that what you&amp;#8217;re doing with the file is printing its mtime. You can easily refactor the equivalent in JavaScript:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;try&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// imaginary JS file API&lt;/span&gt;
  &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; f &lt;span style="color: #339933;"&gt;=&lt;/span&gt; File.&lt;span style="color: #000066;"&gt;open&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;filename&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  sys.&lt;span style="color: #000066;"&gt;print&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f.&lt;span style="color: #660066;"&gt;mtime&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;finally&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  f.&lt;span style="color: #000066;"&gt;close&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Into this:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;read_file&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  sys.&lt;span style="color: #000066;"&gt;print&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f.&lt;span style="color: #660066;"&gt;mtime&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In fact, cases like this, which are in fact quite elegant, give people the mistaken impression that Ruby and JavaScript have a roughly equivalent ability to refactor common functionality into anonymous functions.&lt;/p&gt;
&lt;p&gt;However, consider a slightly more complicated example, first in Ruby. We&amp;#8217;ll write a simple class that calculates a File&amp;#8217;s mtime and retrieves its body:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;class&lt;/span&gt; FileInfo
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; initialize&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;filename&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#0066ff; font-weight:bold;"&gt;@name&lt;/span&gt; = filename
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#008000; font-style:italic;"&gt;# calculate the File's +mtime+&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; mtime
    f = &lt;span style="color:#CC00FF; font-weight:bold;"&gt;File&lt;/span&gt;.&lt;span style="color:#CC0066; font-weight:bold;"&gt;open&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;@name, &lt;span style="color:#996600;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    mtime = mtime_for&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#0000FF; font-weight:bold;"&gt;return&lt;/span&gt; &lt;span style="color:#996600;"&gt;&amp;quot;too old&amp;quot;&lt;/span&gt; &lt;span style="color:#9966CC; font-weight:bold;"&gt;if&lt;/span&gt; mtime &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC00FF; font-weight:bold;"&gt;Time&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;now&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;-&lt;/span&gt; &lt;span style="color:#006666;"&gt;1000&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#CC0066; font-weight:bold;"&gt;puts&lt;/span&gt; &lt;span style="color:#996600;"&gt;&amp;quot;recent!&amp;quot;&lt;/span&gt;
    mtime
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;ensure&lt;/span&gt;
    f.&lt;span style="color:#9900CC;"&gt;close&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#008000; font-style:italic;"&gt;# retrieve that file's +body+&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; body
    f = &lt;span style="color:#CC00FF; font-weight:bold;"&gt;File&lt;/span&gt;.&lt;span style="color:#CC0066; font-weight:bold;"&gt;open&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;@name, &lt;span style="color:#996600;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    f.&lt;span style="color:#9900CC;"&gt;read&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;ensure&lt;/span&gt;
    f.&lt;span style="color:#9900CC;"&gt;close&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#008000; font-style:italic;"&gt;# a helper method to retrieve the mtime of a file&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; mtime_for&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#CC00FF; font-weight:bold;"&gt;File&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;mtime&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can easily refactor this code using blocks:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;class&lt;/span&gt; FileInfo
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; initialize&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;filename&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#0066ff; font-weight:bold;"&gt;@name&lt;/span&gt; = filename
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#008000; font-style:italic;"&gt;# refactor the common file management code into a method&lt;/span&gt;
  &lt;span style="color:#008000; font-style:italic;"&gt;# that takes a block&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; mtime
    with_file &lt;span style="color:#9966CC; font-weight:bold;"&gt;do&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;|&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;|&lt;/span&gt;
      mtime = mtime_for&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
      &lt;span style="color:#0000FF; font-weight:bold;"&gt;return&lt;/span&gt; &lt;span style="color:#996600;"&gt;&amp;quot;too old&amp;quot;&lt;/span&gt; &lt;span style="color:#9966CC; font-weight:bold;"&gt;if&lt;/span&gt; mtime &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC00FF; font-weight:bold;"&gt;Time&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;now&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;-&lt;/span&gt; &lt;span style="color:#006666;"&gt;1000&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
      &lt;span style="color:#CC0066; font-weight:bold;"&gt;puts&lt;/span&gt; &lt;span style="color:#996600;"&gt;&amp;quot;recent!&amp;quot;&lt;/span&gt;
      mtime
    &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; body
    with_file &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#123;&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;|&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;|&lt;/span&gt; f.&lt;span style="color:#9900CC;"&gt;read&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#125;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; mtime_for&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#CC00FF; font-weight:bold;"&gt;File&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;mtime&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
private
  &lt;span style="color:#008000; font-style:italic;"&gt;# this method opens a file, calls a block with it, and&lt;/span&gt;
  &lt;span style="color:#008000; font-style:italic;"&gt;# ensures that the file is closed once the block has&lt;/span&gt;
  &lt;span style="color:#008000; font-style:italic;"&gt;# finished executing.&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; with_file
    f = &lt;span style="color:#CC00FF; font-weight:bold;"&gt;File&lt;/span&gt;.&lt;span style="color:#CC0066; font-weight:bold;"&gt;open&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;@name, &lt;span style="color:#996600;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#9966CC; font-weight:bold;"&gt;yield&lt;/span&gt; f
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;ensure&lt;/span&gt;
    f.&lt;span style="color:#9900CC;"&gt;close&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Again, the important thing to note here is that we could move the code into a block without changing it. Unfortunately, this same case does not work in JavaScript. Let&amp;#8217;s first write the equivalent &lt;code&gt;FileInfo&lt;/code&gt; class in JavaScript.&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #006600; font-style: italic;"&gt;// constructor for the FileInfo class&lt;/span&gt;
FileInfo &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;filename&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #000066;"&gt;name&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; filename&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
FileInfo.&lt;span style="color: #660066;"&gt;prototype&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// retrieve the file's mtime&lt;/span&gt;
  mtime&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;try&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
      &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; f &lt;span style="color: #339933;"&gt;=&lt;/span&gt; File.&lt;span style="color: #000066;"&gt;open&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
      &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; mtime &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;mtimeFor&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
      &lt;span style="color: #000066; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;mtime &lt;span style="color: #339933;"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;new&lt;/span&gt; Date&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #339933;"&gt;-&lt;/span&gt; &lt;span style="color: #CC0000;"&gt;1000&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
        &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;too old&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
      sys.&lt;span style="color: #000066;"&gt;print&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;mtime&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;finally&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
      f.&lt;span style="color: #000066;"&gt;close&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #006600; font-style: italic;"&gt;// retrieve the file's body&lt;/span&gt;
  body&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;try&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
      &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; f &lt;span style="color: #339933;"&gt;=&lt;/span&gt; File.&lt;span style="color: #000066;"&gt;open&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
      &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; f.&lt;span style="color: #660066;"&gt;read&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;finally&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
      f.&lt;span style="color: #000066;"&gt;close&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #006600; font-style: italic;"&gt;// a helper method to retrieve the mtime of a file&lt;/span&gt;
  mtimeFor&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; File.&lt;span style="color: #660066;"&gt;mtime&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If we try to convert the repeated code into a method that takes a function, the &lt;code&gt;mtime&lt;/code&gt; method will look something like:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// refactor the common file management code into a method&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// that takes a block&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;withFile&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; mtime &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;mtimeFor&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;mtime &lt;span style="color: #339933;"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;new&lt;/span&gt; Date&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #339933;"&gt;-&lt;/span&gt; &lt;span style="color: #CC0000;"&gt;1000&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
      &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;too old&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
    sys.&lt;span style="color: #000066;"&gt;print&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;mtime&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;There are two very common problems here. First, &lt;code&gt;this&lt;/code&gt; has changed contexts. We can fix this by allowing a binding as a second parameter, but it means that we need to make sure that every time we refactor to a lambda we make sure to accept a binding parameter and pass it in. The &lt;code&gt;var self = this&lt;/code&gt; pattern emerged in JavaScript primarily because of the lack of correspondence.&lt;/p&gt;
&lt;p&gt;This is annoying, but not deadly. More problematic is the fact that &lt;code&gt;return&lt;/code&gt; has changed meaning. Instead of returning from the outer function, it returns from the inner one.&lt;/p&gt;
&lt;p&gt;This is the right time for JavaScript lovers (and I write this as a sometimes JavaScript lover myself) to argue that &lt;code&gt;return&lt;/code&gt; behaves exactly as intended, and this behavior is simpler and more elegant than the Ruby behavior. That may be true, but it doesn&amp;#8217;t alter the fact that this behavior breaks the correspondence principle, with very real consequences.&lt;/p&gt;
&lt;p&gt;Instead of effortlessly refactoring code with the same start and end into a function taking a function, JavaScript library authors need to consider the fact that consumers of their APIs will often need to perform some gymnastics when dealing with nested functions. In my experience as an author and consumer of JavaScript libraries, this leads to many cases where it&amp;#8217;s just too much bother to provide a nice block-based API.&lt;/p&gt;
&lt;p&gt;In order to have a language with &lt;code&gt;return&lt;/code&gt; (and possibly &lt;code&gt;super&lt;/code&gt; and other similar keywords) that satisfies the correspondence principle, the language must, like Ruby and Smalltalk before it, have a function lambda and a block lambda. Keywords like &lt;code&gt;return&lt;/code&gt; always return from the function lambda, even inside of block lambdas nested inside. At first glance, this appears a bit inelegant, and language partisans often accuse Ruby of unnecessarily having two types of &amp;#8220;callables&amp;#8221;, in my experience as an author of large libraries in both Ruby and JavaScript, it results in more elegant abstractions in the end.&lt;/p&gt;
&lt;h3&gt;Iterators and Callbacks&lt;/h3&gt;
&lt;p&gt;It&amp;#8217;s worth noting that block lambdas only make sense for functions that take functions &lt;em&gt;and invoke them immediately&lt;/em&gt;. In this context, keywords like &lt;code&gt;return&lt;/code&gt;, &lt;code&gt;super&lt;/code&gt; and Ruby&amp;#8217;s &lt;code&gt;yield&lt;/code&gt; make sense. These cases include iterators, mutex synchronization and resource management (like the block form of &lt;code&gt;File.open&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;In contrast, when functions are used as callbacks, those keywords no longer make sense. What does it mean to return from a function that has already returned? In these cases, typically involving callbacks, function lambdas make a lot of sense. In my view, this explains why JavaScript feels so elegant for evented code that involves a lot of callbacks, but somewhat clunky for the iterator case, and Ruby feels so elegant for the iterator case and somewhat more clunky for the evented case. In Ruby&amp;#8217;s case, (again in my opinion), this clunkiness is more from the massively pervasive use of blocks for synchronous code than a real deficiency in its structures.&lt;/p&gt;
&lt;p&gt;Because of these concerns, the ECMA working group responsible for ECMAScript, TC39, &lt;a href="http://wiki.ecmascript.org/doku.php?id=strawman:block_lambda_revival"&gt;is considering adding block lambdas&lt;/a&gt; to the language. This would mean that the above example could be refactored to:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;FileInfo &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #000066;"&gt;name&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
FileInfo.&lt;span style="color: #660066;"&gt;prototype&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  mtime&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #006600; font-style: italic;"&gt;// use the proposed block syntax, `{ |args| }`.&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;withFile&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; &lt;span style="color: #339933;"&gt;|&lt;/span&gt;f&lt;span style="color: #339933;"&gt;|&lt;/span&gt;
      &lt;span style="color: #006600; font-style: italic;"&gt;// in block lambdas, +this+ is unchanged&lt;/span&gt;
      &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; mtime &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;mtimeFor&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
      &lt;span style="color: #000066; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;mtime &lt;span style="color: #339933;"&gt;&amp;lt;&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;new&lt;/span&gt; Date&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #339933;"&gt;-&lt;/span&gt; &lt;span style="color: #CC0000;"&gt;1000&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
        &lt;span style="color: #006600; font-style: italic;"&gt;// block lambdas return from their nearest function&lt;/span&gt;
        &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;too old&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
      sys.&lt;span style="color: #000066;"&gt;print&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;mtime&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp;
  body&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;withFile&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; &lt;span style="color: #339933;"&gt;|&lt;/span&gt;f&lt;span style="color: #339933;"&gt;|&lt;/span&gt; f.&lt;span style="color: #660066;"&gt;read&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp;
  mtimeFor&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; File.&lt;span style="color: #660066;"&gt;mtime&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp;
  withFile&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;block&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;try&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
      &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; f &lt;span style="color: #339933;"&gt;=&lt;/span&gt; File.&lt;span style="color: #000066;"&gt;open&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;r&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
      block&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;f&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;finally&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
      f.&lt;span style="color: #000066;"&gt;close&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that a parallel proposal, which replaces function-scoped &lt;code&gt;var&lt;/code&gt; with block-scoped &lt;code&gt;let&lt;/code&gt;, will almost certainly be accepted by TC39, which would slightly, but not substantively, change this example. Also note block lambdas automatically return their last statement.&lt;/p&gt;
&lt;p&gt;Our experience with Smalltalk and Ruby show that people do not need to understand the &lt;em&gt;SCARY&lt;/em&gt; correspondence principle for a language that satisfies it to yield the desired results. I love the fact that the concept of &amp;#8220;iterator&amp;#8221; is not built into the language, but is instead a consequence of natural block semantics. This gives Ruby a rich, broadly useful set of built-in iterators, and language users commonly build custom ones. As a JavaScript practitioner, I often run into situations where using a for loop is significantly more straight-forward than using &lt;code&gt;forEach&lt;/code&gt;, always because of the lack of correspondence between the code inside a built-in for loop and the code inside the function passed to &lt;code&gt;forEach&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;For the reasons described above, I strongly approve of &lt;a href="http://wiki.ecmascript.org/doku.php?id=strawman:block_lambda_revival"&gt;the block lambda proposal&lt;/a&gt; and hope it is adopted.&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2012%2F01%2F10%2Fjavascript-needs-blocks%2F';
  addthis_title  = 'JavaScript+Needs+Blocks';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=v1ctIJnejg4:YSxx--Twyr4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/v1ctIJnejg4" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2012/01/10/javascript-needs-blocks/#comments" thr:count="16" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2012/01/10/javascript-needs-blocks/feed/atom/" thr:count="16" />
		<thr:total>16</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2012/01/10/javascript-needs-blocks/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[Amber.js (formerly SproutCore 2.0) is now Ember.js]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/5_ye09WM9kU/" />
		<id>http://yehudakatz.com/?p=694</id>
		<updated>2011-12-12T21:56:45Z</updated>
		<published>2011-12-12T21:51:44Z</published>
		<category scheme="http://yehudakatz.com" term="Other" />		<summary type="html"><![CDATA[After we announced Amber.js last week, a number of people brought Amber Smalltalk, a Smalltalk implementation written in JavaScript, to our attention. After some communication with the folks behind Amber Smalltalk, we started a discussion on Hacker News about what we should do. Most people told us to stick with Amber.js, but a sizable minority [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2011/12/12/amber-js-formerly-sproutcore-2-0-is-now-ember-js/">&lt;p&gt;After we announced Amber.js last week, a number of people brought Amber Smalltalk, a Smalltalk implementation written in JavaScript, to our attention. After some communication with the folks behind Amber Smalltalk, we started &lt;a href="http://news.ycombinator.com/item?id=3335916"&gt;a discussion on Hacker News&lt;/a&gt; about what we should do.&lt;/p&gt;
&lt;p&gt;Most people told us to stick with Amber.js, but a sizable minority told us to come up with a different name. After thinking about it, we didn&amp;#8217;t feel good about the conflict and decided to choose a new name.&lt;/p&gt;
&lt;p&gt;Henceforth, the project formerly known as SproutCore 2.0 will be known as Ember.js. Our new website is up at &lt;a href="http://www.emberjs.com"&gt;http://www.emberjs.com&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;(and yes, we know this is pretty ridiculous)&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2011%2F12%2F12%2Famber-js-formerly-sproutcore-2-0-is-now-ember-js%2F';
  addthis_title  = 'Amber.js+%28formerly+SproutCore+2.0%29+is+now+Ember.js';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=5_ye09WM9kU:-rXADmBx3qk:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/5_ye09WM9kU" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2011/12/12/amber-js-formerly-sproutcore-2-0-is-now-ember-js/#comments" thr:count="22" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2011/12/12/amber-js-formerly-sproutcore-2-0-is-now-ember-js/feed/atom/" thr:count="22" />
		<thr:total>22</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2011/12/12/amber-js-formerly-sproutcore-2-0-is-now-ember-js/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[Announcing Amber.js]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/kcjvX4gkC1Q/" />
		<id>http://yehudakatz.com/?p=686</id>
		<updated>2011-12-08T16:45:46Z</updated>
		<published>2011-12-08T16:32:07Z</published>
		<category scheme="http://yehudakatz.com" term="Other" />		<summary type="html"><![CDATA[A little over a year ago, I got my first serious glimpse at SproutCore, the JavaScript framework Apple used to build MobileMe (now iCloud). At the time, I had worked extensively with jQuery and Rails on client-side projects, and I had never found the arguments for the &#8220;solutions for big apps&#8221; very compelling. At the [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2011/12/08/announcing-amber-js/">&lt;p&gt;A little over a year ago, I got my first serious glimpse at SproutCore, the JavaScript framework Apple used to build MobileMe (now iCloud). At the time, I had worked extensively with jQuery and Rails on client-side projects, and I had never found the arguments for the &amp;#8220;solutions for big apps&amp;#8221; very compelling. At the time, most of the arguments (at least within the jQuery community) focused on bringing more object orientation to JavaScript, but I never felt that they offered the layers of abstraction you really want to manage complexity.&lt;/p&gt;
&lt;p&gt;When I first started to play with SproutCore, I realized that the bindings and computed properties were what gave it its real power. Bindings and computed properties provide a clean mechanism for building the layers of abstractions that improve the structure of large applications.&lt;/p&gt;
&lt;p&gt;But even before I got involved in SproutCore, I had an epiphany one day when playing with Mustache.js. Because Mustache.js was a declarative way of describing a translation from a piece of JSON to HTML, it seemed to me that there was enough information in the template to &lt;strong&gt;also&lt;/strong&gt; update the template when the underlying data changed. Unfortunately, Mustache.js itself lacked the power to implement this idea, and I was still lacking a robust enough observer library.&lt;/p&gt;
&lt;p&gt;Not wanting to build an observer library in isolation (and believing that jQuery&amp;#8217;s data support would work in a pinch), I started working on the first problem: building a template engine powerful enough to build automatically updating templates. The kernel of the idea for Handlebars (helpers and block helpers as the core primitives) came out of a discussion with Carl Lerche back when we were still at Engine Yard, and I got to work.&lt;/p&gt;
&lt;p&gt;When I met SproutCore, I realized that it provided a more powerful observer library than anything I was considering at the time for the data-binding aspect of Handlebars, and that SproutCore&amp;#8217;s biggest weakness was the lack of a good templating solution in its view layer. I also rapidly became convinced that bindings and computed properties were a significantly better abstraction, and allowed for hiding much more complexity, than manually binding observers.&lt;/p&gt;
&lt;p&gt;After some months of retooling SproutCore with Tom Dale to take advantage of an auto-updating templating solution that fit cleanly into SproutCore&amp;#8217;s binding model, we reached a crossroads. SproutCore itself was built from the ground up to provide a desktop-like experience on desktop browsers, and our ultimate plan had started to diverge from the widget-centric focus of many existing users of SproutCore. After a lot of soul-searching, we decided to start from scratch with SproutCore 2.0, taking with us the best, core ideas of SproutCore, but leaving the large, somewhat sprawling codebase behind.&lt;/p&gt;
&lt;p&gt;Since early this year, we have worked with several large companies, including ZenDesk, BazaarVoice and LivingSocial, to iterate on the core ideas that we started from to build a powerful framework for building ambitious applications.&lt;/p&gt;
&lt;p&gt;Throughout this time, though, we became increasingly convinced that calling what we were building &amp;#8220;SproutCore 2.0&amp;#8243; was causing a lot of confusion, because SproutCore 1.x was primarily a native-style widget library, while SproutCore 2.0 was a framework for building web-based applications using HTML and CSS for the presentation layer. This lack of overlap causes serious confusion in the IRC room, mailing list, blog, when searching on Google, etc.&lt;/p&gt;
&lt;p&gt;To clear things up, we have decided to name the SproutCore-inspired framework we have been building (so far called &amp;#8220;SproutCore 2.0&amp;#8243;) &amp;#8220;Amber.js&amp;#8221;. Amber brings a proven MVC architecture to web applications, as well as features that eliminate common boilerplate. If you played with SproutCore and liked the concepts but felt like it was too heavy, give Amber a try. And if you&amp;#8217;re a Backbone fan, I think you&amp;#8217;ll love how little code you need to write with Amber.&lt;/p&gt;
&lt;p&gt;In the next few days, we&amp;#8217;ll be launching a new website with examples, documentation, and download links. Stay tuned for further updates soon.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;UPDATE:&lt;/strong&gt; The code for Amber.js is still, as of December 8, hosted at &lt;a href="http://github.com/sproutcore/sproutcore20"&gt;the SproutCore organization&lt;/a&gt;. It will be moved and re-namespaced within a few days.&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2011%2F12%2F08%2Fannouncing-amber-js%2F';
  addthis_title  = 'Announcing+Amber.js';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=kcjvX4gkC1Q:0UOkl6M4RB8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/kcjvX4gkC1Q" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2011/12/08/announcing-amber-js/#comments" thr:count="63" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2011/12/08/announcing-amber-js/feed/atom/" thr:count="63" />
		<thr:total>63</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2011/12/08/announcing-amber-js/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[How to Marshal Procs Using Rubinius]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/0ROvAEqCwNE/" />
		<id>http://yehudakatz.com/?p=676</id>
		<updated>2011-11-26T22:16:53Z</updated>
		<published>2011-11-19T06:11:53Z</published>
		<category scheme="http://yehudakatz.com" term="Rubinius" /><category scheme="http://yehudakatz.com" term="Ruby" />		<summary type="html"><![CDATA[The primary reason I enjoy working with Rubinius is that it exposes, to Ruby, much of the internal machinery that controls the runtime semantics of the language. Further, it exposes that machinery primarily in order to enable user-facing semantics that are typically implemented in the host language (C for MRI, C and C++ for MacRuby, [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2011/11/19/how-to-marshal-procs-using-rubinius/">&lt;p&gt;The primary reason I enjoy working with Rubinius is that it exposes, to Ruby, much of the internal machinery that controls the runtime semantics of the language. Further, it exposes that machinery primarily in order to enable user-facing semantics that are typically implemented in the host language (C for MRI, C and C++ for MacRuby, Java for JRuby) to be implemented in Ruby itself.&lt;/p&gt;
&lt;p&gt;There is, of course, quite a bit of low-level functionality in Rubinius implemented in C++, but a surprising number of things are implemented in pure Ruby.&lt;/p&gt;
&lt;p&gt;One example is the &lt;code&gt;Binding&lt;/code&gt; object. To create a new binding in Rubinius, you call &lt;code&gt;Binding.setup&lt;/code&gt;:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; &lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;setup&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;variables, code, static_scope, recv=&lt;span style="color:#0000FF; font-weight:bold;"&gt;nil&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  bind = allocate&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
  bind.&lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt; = recv &lt;span style="color:#006600; font-weight:bold;"&gt;||&lt;/span&gt; variables.&lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;
  bind.&lt;span style="color:#9900CC;"&gt;variables&lt;/span&gt; = variables
  bind.&lt;span style="color:#9900CC;"&gt;code&lt;/span&gt; = code
  bind.&lt;span style="color:#9900CC;"&gt;static_scope&lt;/span&gt; = static_scope
  &lt;span style="color:#0000FF; font-weight:bold;"&gt;return&lt;/span&gt; bind
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This method takes a number of more primitive constructs, which I will explain as this article progresses, but we can describe the constructs that make up the high-level Ruby &lt;code&gt;Binding&lt;/code&gt; in pure Ruby.&lt;/p&gt;
&lt;p&gt;In fact, Rubinius implements &lt;code&gt;Kernel#binding&lt;/code&gt; itself in terms of &lt;code&gt;Binding.setup&lt;/code&gt;.&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; &lt;span style="color:#CC0066; font-weight:bold;"&gt;binding&lt;/span&gt;
  &lt;span style="color:#0000FF; font-weight:bold;"&gt;return&lt;/span&gt; &lt;span style="color:#CC0066; font-weight:bold;"&gt;Binding&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;setup&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;
    &lt;span style="color:#6666ff; font-weight:bold;"&gt;Rubinius::VariableScope&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;of_sender&lt;/span&gt;,
    &lt;span style="color:#6666ff; font-weight:bold;"&gt;Rubinius::CompiledMethod&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;of_sender&lt;/span&gt;,
    &lt;span style="color:#6666ff; font-weight:bold;"&gt;Rubinius::StaticScope&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;of_sender&lt;/span&gt;,
    &lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Yes, you&amp;#8217;re reading that right. Rubinius exposes the ability to extract the constructs that make up a binding, one at a time, from a caller&amp;#8217;s scope. And this is not just a hack (like Binding.of_caller for a short time in MRI). It&amp;#8217;s core to how Rubinius manages &lt;code&gt;eval&lt;/code&gt;, which of course makes heavy use of bindings.&lt;/p&gt;
&lt;h2&gt;Marshalling Procs&lt;/h2&gt;
&lt;p&gt;For a while, I have wanted the ability to &lt;code&gt;Marshal.dump&lt;/code&gt; a proc in Ruby. MRI has historically disallowed it, but there&amp;#8217;s nothing conceptually impossible about it. A proc itself is a blob of executable code, a local variable scope (which is just a bunch of pointers to &lt;strong&gt;other&lt;/strong&gt; objects), and a constant lookup scope. Rubinius exposes each of these constructs to Ruby, so Marshaling a proc simply means figuring out how to Marshal each of these constructs.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s take a quick detour to learn about the constructs in question.&lt;/p&gt;
&lt;h2&gt;Rubinius::StaticScope&lt;/h2&gt;
&lt;p&gt;Rubinius represents Ruby&amp;#8217;s constant lookup scope as a &lt;code&gt;Rubinius::StaticScope&lt;/code&gt; object. Perhaps the easiest way to understand it would be to look at Ruby&amp;#8217;s built-in &lt;code&gt;Module.nesting&lt;/code&gt; function.&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;module&lt;/span&gt; Foo
  &lt;span style="color:#CC0066; font-weight:bold;"&gt;p&lt;/span&gt; &lt;span style="color:#9966CC; font-weight:bold;"&gt;Module&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;nesting&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;module&lt;/span&gt; Bar
    &lt;span style="color:#CC0066; font-weight:bold;"&gt;p&lt;/span&gt; &lt;span style="color:#9966CC; font-weight:bold;"&gt;Module&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;nesting&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;module&lt;/span&gt; &lt;span style="color:#6666ff; font-weight:bold;"&gt;Foo::Bar&lt;/span&gt;
  &lt;span style="color:#CC0066; font-weight:bold;"&gt;p&lt;/span&gt; &lt;span style="color:#9966CC; font-weight:bold;"&gt;Module&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;nesting&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
&lt;span style="color:#008000; font-style:italic;"&gt;# Output:&lt;/span&gt;
&lt;span style="color:#008000; font-style:italic;"&gt;# [Foo]&lt;/span&gt;
&lt;span style="color:#008000; font-style:italic;"&gt;# [Foo::Bar, Foo]&lt;/span&gt;
&lt;span style="color:#008000; font-style:italic;"&gt;# [Foo::Bar]&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Every execution context in Rubinius has a &lt;code&gt;Rubinius::StaticScope&lt;/code&gt;, which may optionally have a parent scope. In general, the top static scope (the static scope with no parent) in any execution context is &lt;code&gt;Object&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Because Rubinius allows us to get the static scope of a calling method, we can implement &lt;code&gt;Module.nesting&lt;/code&gt; in Rubinius:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; nesting
  scope = &lt;span style="color:#6666ff; font-weight:bold;"&gt;Rubinius::StaticScope&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;of_sender&lt;/span&gt;
  nesting = &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#93;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;while&lt;/span&gt; scope &lt;span style="color:#9966CC; font-weight:bold;"&gt;and&lt;/span&gt; scope.&lt;span style="color:#9966CC; font-weight:bold;"&gt;module&lt;/span&gt; != &lt;span style="color:#CC00FF; font-weight:bold;"&gt;Object&lt;/span&gt;
    nesting &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;lt;&amp;lt;&lt;/span&gt; scope.&lt;span style="color:#9966CC; font-weight:bold;"&gt;module&lt;/span&gt;
    scope = scope.&lt;span style="color:#9900CC;"&gt;parent&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
  nesting
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;A static scope also has an addition property called &lt;code&gt;current_module&lt;/code&gt;, which is used during &lt;code&gt;class_eval&lt;/code&gt; to define which module the runtime should add new methods to.&lt;/p&gt;
&lt;p&gt;Adding &lt;code&gt;Marshal.dump&lt;/code&gt; support to a static scope is therefore quite easy:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;class&lt;/span&gt; &lt;span style="color:#6666ff; font-weight:bold;"&gt;Rubinius::StaticScope&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; marshal_dump
    &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#91;&lt;/span&gt;@&lt;span style="color:#9966CC; font-weight:bold;"&gt;module&lt;/span&gt;, &lt;span style="color:#0066ff; font-weight:bold;"&gt;@current_module&lt;/span&gt;, &lt;span style="color:#0066ff; font-weight:bold;"&gt;@parent&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#93;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; marshal_load&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;array&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    @&lt;span style="color:#9966CC; font-weight:bold;"&gt;module&lt;/span&gt;, &lt;span style="color:#0066ff; font-weight:bold;"&gt;@current_module&lt;/span&gt;, &lt;span style="color:#0066ff; font-weight:bold;"&gt;@parent&lt;/span&gt; = &lt;span style="color:#CC0066; font-weight:bold;"&gt;array&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;These three instance variables are defined as Rubinius &lt;em&gt;slots&lt;/em&gt;, which means that they are fully accessible to Ruby as instance variables, but don&amp;#8217;t show up in the &lt;code&gt;instance_variables&lt;/code&gt; list. As a result, we need to explicitly dump the instance variables that we care about and reload them later.&lt;/p&gt;
&lt;h2&gt;Rubinius::CompiledMethod&lt;/h2&gt;
&lt;p&gt;A compiled method holds the information necessary to execute a blob of Ruby code. Some important parts of a compiled method are its instruction sequence (a list of the compiled instructions for the code), a list of any literals it has access to, names of local variables, its method signature, and a number of other important characteristics.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s actually quite a complex structure, but Rubinius has already knows how to convert an in-memory &lt;code&gt;CompiledMethod&lt;/code&gt; into a String, as it dumps compiled Ruby files into compiled files as part of its normal operation. There is one small caveat: this String form that Rubinius uses for its compiled method does not include its static scope, so we will need to include the static scope separately in the marshaled form. Since we already told Rubinius how to marshal a static scope, this is easy.&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;class&lt;/span&gt; &lt;span style="color:#6666ff; font-weight:bold;"&gt;Rubinius::CompiledMethod&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; _dump&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;depth&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#CC00FF; font-weight:bold;"&gt;Marshal&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;dump&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#91;&lt;/span&gt;@scope, &lt;span style="color:#6666ff; font-weight:bold;"&gt;Rubinius::CompiledFile&lt;/span&gt;::&lt;span style="color:#CC00FF; font-weight:bold;"&gt;Marshal&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;new&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;marshal&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; &lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;._load&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;string&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    scope, dump = &lt;span style="color:#CC00FF; font-weight:bold;"&gt;Marshal&lt;/span&gt;.&lt;span style="color:#CC0066; font-weight:bold;"&gt;load&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;string&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    cm = &lt;span style="color:#6666ff; font-weight:bold;"&gt;Rubinius::CompiledFile&lt;/span&gt;::&lt;span style="color:#CC00FF; font-weight:bold;"&gt;Marshal&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;new&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;unmarshal&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;dump&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    cm.&lt;span style="color:#9900CC;"&gt;scope&lt;/span&gt; = scope
    cm
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2&gt;Rubinius::VariableScope&lt;/h2&gt;
&lt;p&gt;A variable scope represents the &lt;strong&gt;state&lt;/strong&gt; of the current execution context. It contains all of the local variables in the current scope, the execution context currently in scope, the current &lt;code&gt;self&lt;/code&gt;, and several other characteristics.&lt;/p&gt;
&lt;p&gt;I wrote about the variable scope &lt;a href="http://yehudakatz.com/2011/02/18/getting-comfortable-with-rubinius-pure-ruby-internals/"&gt;before&lt;/a&gt;. It&amp;#8217;s one of my favorite Rubinius constructs, because it provides a ton of useful runtime information to Ruby that is usually locked away inside the native implementation.&lt;/p&gt;
&lt;p&gt;Dumping and loading the &lt;code&gt;VariableScope&lt;/code&gt; is also easy:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;class&lt;/span&gt; VariableScope
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; _dump&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;depth&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#CC00FF; font-weight:bold;"&gt;Marshal&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;dump&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#91;&lt;/span&gt;@method, @&lt;span style="color:#9966CC; font-weight:bold;"&gt;module&lt;/span&gt;, &lt;span style="color:#0066ff; font-weight:bold;"&gt;@parent&lt;/span&gt;, @&lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;, &lt;span style="color:#0000FF; font-weight:bold;"&gt;nil&lt;/span&gt;, locals&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; &lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;._load&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;string&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    VariableScope.&lt;span style="color:#9900CC;"&gt;synthesize&lt;/span&gt; &lt;span style="color:#006600; font-weight:bold;"&gt;*&lt;/span&gt;&lt;span style="color:#CC00FF; font-weight:bold;"&gt;Marshal&lt;/span&gt;.&lt;span style="color:#CC0066; font-weight:bold;"&gt;load&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;string&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;synthesize&lt;/code&gt; method is new to Rubinius master; getting a new variable scope previously required synthesizing its locals using &lt;code&gt;class_eval&lt;/code&gt;, and the new method is better.&lt;/p&gt;
&lt;h2&gt;Rubinius::BlockEnvironment&lt;/h2&gt;
&lt;p&gt;A Proc is basically nothing but a wrapper around a &lt;code&gt;Rubinius::BlockEnvironment&lt;/code&gt;, which wraps up all of the objects we&amp;#8217;ve been working with so far. Its &lt;code&gt;scope&lt;/code&gt; attribute is a &lt;code&gt;VariableScope&lt;/code&gt; and its &lt;code&gt;code&lt;/code&gt; attribute is a &lt;code&gt;CompiledMethod&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Dumping it should be quite familiar by now.&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;class&lt;/span&gt; BlockEnvironment
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; marshal_dump
    &lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#91;&lt;/span&gt;@scope, &lt;span style="color:#0066ff; font-weight:bold;"&gt;@code&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#93;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; marshal_load&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;array&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    scope, code = &lt;span style="color:#006600; font-weight:bold;"&gt;*&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;array&lt;/span&gt;
    under_context scope, code
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The only thing new here is the &lt;code&gt;under_context&lt;/code&gt; method, which gives a &lt;code&gt;BlockEnvironment&lt;/code&gt; its variable scope and compiled method. Note that we dumped the static scope along with the compiled method above.&lt;/p&gt;
&lt;h2&gt;Proc&lt;/h2&gt;
&lt;p&gt;Finally, a Proc is just a wrapper around a BlockEnvironment, so dumping it is easy:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="ruby" style="font-family:monospace;"&gt;&lt;span style="color:#9966CC; font-weight:bold;"&gt;class&lt;/span&gt; &lt;span style="color:#CC0066; font-weight:bold;"&gt;Proc&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; _dump&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;depth&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#CC00FF; font-weight:bold;"&gt;Marshal&lt;/span&gt;.&lt;span style="color:#9900CC;"&gt;dump&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;@block&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;def&lt;/span&gt; &lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;._load&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;string&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    block = &lt;span style="color:#CC00FF; font-weight:bold;"&gt;Marshal&lt;/span&gt;.&lt;span style="color:#CC0066; font-weight:bold;"&gt;load&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color:#CC0066; font-weight:bold;"&gt;string&lt;/span&gt;&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
    &lt;span style="color:#0000FF; font-weight:bold;"&gt;self&lt;/span&gt;.__from_block__&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#40;&lt;/span&gt;block&lt;span style="color:#006600; font-weight:bold;"&gt;&amp;#41;&lt;/span&gt;
  &lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;
&lt;span style="color:#9966CC; font-weight:bold;"&gt;end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;__from_block__&lt;/code&gt; method constructs a new Proc from a BlockEnvironment.&lt;/p&gt;
&lt;p&gt;So there you have it. Dumping and reloading Proc objects in pure Ruby using Rubinius! (the full source is at &lt;a href="https://gist.github.com/1378518"&gt;https://gist.github.com/1378518&lt;/a&gt;).&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2011%2F11%2F19%2Fhow-to-marshal-procs-using-rubinius%2F';
  addthis_title  = 'How+to+Marshal+Procs+Using+Rubinius';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=0ROvAEqCwNE:8aNP2Ye9jho:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/0ROvAEqCwNE" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2011/11/19/how-to-marshal-procs-using-rubinius/#comments" thr:count="8" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2011/11/19/how-to-marshal-procs-using-rubinius/feed/atom/" thr:count="8" />
		<thr:total>8</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2011/11/19/how-to-marshal-procs-using-rubinius/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[A Proposal for ES.next Proposals]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/ytqY29Cb3_g/" />
		<id>http://yehudakatz.com/?p=668</id>
		<updated>2011-09-13T05:31:06Z</updated>
		<published>2011-09-13T05:29:43Z</published>
		<category scheme="http://yehudakatz.com" term="JavaScript" />		<summary type="html"><![CDATA[Over the past few years, I have occasionally expressed frustration (in public and private) about the process for approving new features to the next edition of ECMAScript. In short, the process is extremely academic in nature, and is peppered with inside baseball terms that make it nearly impossible for lay developers to provide feedback about [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2011/09/13/a-proposal-for-es-next-proposals/">&lt;p&gt;Over the past few years, I have occasionally expressed frustration (in public and private) about the process for approving new features to the next edition of ECMAScript. In short, the process is extremely academic in nature, and is peppered with inside baseball terms that make it nearly impossible for lay developers to provide feedback about proposed new features. In general, this frustration was met with a general assumption that the current process works the way it does for good reason, and that academic descriptions of the new features was the correct (and only) way to properly discuss them.&lt;/p&gt;
&lt;p&gt;I have nothing against new features being described in the language of implementors, but I would like to propose some additions to the current process that would make it significantly easier for language &lt;strong&gt;consumers&lt;/strong&gt; (especially framework and library implementors) to provide timely feedback about the proposal in the hope of making an impact before it&amp;#8217;s too late.&lt;/p&gt;
&lt;p&gt;I would like proposals for new features to have the following elements, in addition to whatever elements are already normally included (such as BNF for any new syntax).&lt;/p&gt;
&lt;h4&gt;What Problem Are We Solving?&lt;/h4&gt;
&lt;p&gt;At a high level, what can language users do &lt;strong&gt;now&lt;/strong&gt; that they could not do &lt;strong&gt;before&lt;/strong&gt;. In some cases, proposals may provide simpler or more convenient ways to achieve already-possible goals. These kinds of proposals are often just as important. For example, there is a current proposal to provide a new syntax for class creation. In this case, the new class syntax significantly improves the experience of building a common JavaScript construct.&lt;/p&gt;
&lt;h4&gt;Provide Non-Trivial Example Code Showing the Problem&lt;/h4&gt;
&lt;p&gt;If the proposal is solving a problem that exists in the wild, it should be possible to identify non-trivial examples of the problem rearing its head. At the very least, the process of identifying or synthesizing these examples will help language users understand what real-world problems the proposal is attempting to solve. At best, finding real-world examples will help refine the proposal at an early stage.&lt;/p&gt;
&lt;h4&gt;Show How the Example Code is Improved With the Proposal&lt;/h4&gt;
&lt;p&gt;After identifying or synthesizing example code to illustrate the problem, show how the problem would be improved if the proposal was accepted. In some cases, the problem is as simple as &amp;#8220;needing a large library to perform this operation&amp;#8221; and the solution is &amp;#8220;building common functionality into the language&amp;#8221;. In an example from a related field, the DOM library, the problem addressed by &lt;code&gt;querySelectorAll&lt;/code&gt; was &amp;#8220;many incompatible implementations of a CSS3 selector library&amp;#8221;. The solution was to build the functionality into the browser.&lt;/p&gt;
&lt;p&gt;In this case, a mistake in the &lt;code&gt;querySelectorAll&lt;/code&gt; specification, which was resolved by the addition of &lt;code&gt;queryScopedSelectorAll&lt;/code&gt; in the &lt;a href="http://www.w3.org/TR/selectors-api2/"&gt;Selectors API Level 2&lt;/a&gt; could have been addressed ahead of time by evaluating real-life code using selector engine libraries. Of course, the DOM library is not the same as the language specification, so the example is merely an analogy.&lt;/p&gt;
&lt;h4&gt;What are Alternate ES3 (or ES5) Solutions to the Same Problem?&lt;/h4&gt;
&lt;p&gt;Simply enumerate the ways that existing JavaScript developers have attempted to resolve the problem without language support. In small language changes, this overlaps considerably with the previous questions. By having proposal authors enumerate existing solutions to the problem, it will be easier for language users to identify the scope of the solution.&lt;/p&gt;
&lt;p&gt;This will allow language users to provide feedback about how the solution on offer stacks up compared to existing pure-JS solutions.&lt;/p&gt;
&lt;h4&gt;Are there any restrictions that do not exist in original pure-JS solutions?&lt;/h4&gt;
&lt;p&gt;Are there any restrictions in the proposal that limit its utility as a solution to the problem in question, especially if those restrictions do not apply to solutions currently used by language users.&lt;/p&gt;
&lt;h4&gt;If the Proposal is For New Syntax, What if Anything Does it Desugar To?&lt;/h4&gt;
&lt;p&gt;Also, if the proposal desugars, why choose this particular desugaring as opposed to alternatives?&lt;/p&gt;
&lt;h4&gt;If New Syntax, Can it Be Compiled to ES3 (or ES5)?&lt;/h4&gt;
&lt;p&gt;If the proposal can desugar to an older version of the specification, can a source-to-source translator be written? If so, is there a reference implementation of a source-to-source translator written in that version?&lt;/p&gt;
&lt;p&gt;By writing such a source-to-source translator, existing language users can experiment with the new syntax easily in a browser environment without requiring a separate compilation pass. This also allows users to build an in-browser translation UI (similar to &lt;a href="http://jashkenas.github.com/coffee-script/"&gt;try CoffeeScript&lt;/a&gt;), which can improve general understanding of the new syntax and produce important feedback.&lt;/p&gt;
&lt;p&gt;To be more specific, what I would like to see here is a general-purpose source-to-source translation engine written in ES3 with a mechanism for plugging in translation passes for specific features. If new features come with translation passes, it would be trivial for language users to try out new features incrementally in production applications (with a nice development-time workflow). This would provide usability feedback at an early enough stage for it to be useful.&lt;/p&gt;
&lt;h4&gt;If a New Standard Library, Can it Be Polyfilled to ES3 (or ES5)?&lt;/h4&gt;
&lt;p&gt;If the proposal is for a new library whose syntax is valid in an earlier version of the specification, can it be implemented in terms of the existing primitives available in that version. If necessary, primitives not defined by the language, but provided historically by web browsers, can be used instead. The goal is to provide shims for older browsers so that a much broader group of people can experiment with the APIs and provide feedback.&lt;/p&gt;
&lt;p&gt;In general, new libraries that parse in older versions of the specifications should also come with a well-defined mechanism to detect whether the feature is present, to make it easy for library and framework vendors, as well as the general public, to opt their users into the new features where appropriate.&lt;/p&gt;
&lt;p&gt;Even if a fully backwards-compatible shim cannot be provided, it is still useful to provide a partial implementation together with a feature detection mechanism. At the very least, error-checking shims can be useful, so language users can easily understand the interface to the proposed library.&lt;/p&gt;
&lt;h4&gt;Does the Proposal Change any Existing Semantics?&lt;/h4&gt;
&lt;p&gt;In some cases, the proposal unavoidably changes existing semantics. For example, ES5 changed the semantics of an indirect call to &lt;code&gt;eval&lt;/code&gt; (a call to an alias to eval, such as &lt;code&gt;x = eval; x('some code')&lt;/code&gt; to use the global environment for the evaluated code. In ES3, indirect calls to eval behaved the same as direct calls to eval.&lt;/p&gt;
&lt;p&gt;These cases are rare, and in most cases, require an explicit opt-in (such as the directive &lt;code&gt;"use strict";&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;When such changes are made, especially when they do not require an opt-in, they should be explicitly called out in the proposal to gather feedback about their likely impact on existing code. Even when they require an opt-in, information about the frequency of their use could be useful to assess the difficulty of opting in.&lt;/p&gt;
&lt;p&gt;Since these opt-ins often enable new features as well as changing existing semantics, understanding the impact of the opt-in on existing code would help language users assess their overall utility and timeframe for adoption. This information could also help drive these decisions.&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2011%2F09%2F13%2Fa-proposal-for-es-next-proposals%2F';
  addthis_title  = 'A+Proposal+for+ES.next+Proposals';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=ytqY29Cb3_g:bjTAlLyu32o:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/ytqY29Cb3_g" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2011/09/13/a-proposal-for-es-next-proposals/#comments" thr:count="2" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2011/09/13/a-proposal-for-es-next-proposals/feed/atom/" thr:count="2" />
		<thr:total>2</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2011/09/13/a-proposal-for-es-next-proposals/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[New Hope for The Ruby Specification]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/4Pt5hPHIpQk/" />
		<id>http://yehudakatz.com/?p=657</id>
		<updated>2011-09-05T08:07:17Z</updated>
		<published>2011-09-05T08:00:01Z</published>
		<category scheme="http://yehudakatz.com" term="Ruby" />		<summary type="html"><![CDATA[For a few years, a group of Japanese academics have been working on formalizing the Ruby programming language into a specification they hoped would be accepted by ISO. From time to time, I have read through it, and I had one major concern. Because Ruby 1.9 was still in a lot of flux when they [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2011/09/05/new-hope-for-the-ruby-specification/">&lt;p&gt;For a few years, a group of Japanese academics have been working on formalizing the Ruby programming language into &lt;a href="http://www.ipa.go.jp/osc/english/ruby/Ruby_final_draft_enu_20100825.pdf"&gt;a specification&lt;/a&gt; they hoped would be accepted by ISO. From time to time, I have read through it, and I had one major concern.&lt;/p&gt;
&lt;p&gt;Because Ruby 1.9 was still in a lot of flux when they were drafting the specification, the authors left a lot of details out. Unfortunately, some of these details are extremely important. Here&amp;#8217;s one example, from the specification of &lt;code&gt;String#[]&lt;/code&gt;:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="text" style="font-family:monospace;"&gt;Behavior:
&amp;nbsp;
a) If the length of args is 0 or larger than 2, raise a
   direct instance of the class ArgumentError.
b) Let P be the ﬁrst element of args. Let n be the length
   of the receiver.
c) If P is an instance of the class Integer, let b be the
   value of P.
   1) If the length of args is 1:
      i)   If b is smaller than 0, increment b by n. If b is
           still smaller than 0, return nil.
      ii)  If b &amp;gt;= n, return nil.
      iii) Create an instance of the class Object which
           represents the bth character of
           the receiver and return this instance.&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The important bit here is &lt;code&gt;c(1)(iii)&lt;/code&gt;, which says to create &amp;#8220;an instance of the class Object which represents the btw character of the receiver&amp;#8221;. The reason for this ambiguity, as best as I can determine, is that Ruby 1.8 and Ruby 1.9 differ on the behavior:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="text" style="font-family:monospace;"&gt;1.8 &amp;gt;&amp;gt; &amp;quot;hello&amp;quot;[1]
=&amp;gt; 101
&amp;nbsp;
1.9 &amp;gt;&amp;gt; &amp;quot;hello&amp;quot;[1]
=&amp;gt; &amp;quot;e&amp;quot;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Of course, neither of these results in a &lt;strong&gt;direct&lt;/strong&gt; instance of the class &lt;code&gt;Object&lt;/code&gt;, but since Fixnums and Strings are both &amp;#8220;instances of the class Object&amp;#8221;, this is technically true. Unfortunately, any real-life Ruby code will need to know what &lt;strong&gt;actual&lt;/strong&gt; object this method will return.&lt;/p&gt;
&lt;p&gt;Another very common reason for unspecified behaviors is a failure to specify Ruby&amp;#8217;s coercion protocol, so &lt;code&gt;String#+&lt;/code&gt; is unspecified if the &lt;code&gt;other&lt;/code&gt; is not a String, even though all Ruby implementations will call &lt;code&gt;to_str&lt;/code&gt; on the &lt;code&gt;other&lt;/code&gt; to attempt to coerce it. The coercion protocol has been underspecified for a long time, and it&amp;#8217;s understandable that the group punted on it, but because it is heavily relied on by real-life code, it is important that we actually describe the behavior.&lt;/p&gt;
&lt;p&gt;This week, I am in Matsue in Japan for RubyWorld, and I was glad to learn that the group working on the ISO specification sees the current work as a first step that will continue with a more rigid specification of currently &amp;#8220;unspecified&amp;#8221; behavior based on Ruby 1.9.&lt;/p&gt;
&lt;p&gt;The word &amp;#8220;unspecified&amp;#8221; appears 170 times in the current draft of the Ruby specification. I hope that the next version will eliminate most if not all of these unspecified behaviors in favor of explicit behavior or explicitly requiring an exception to be thrown. In cases that actually differ between implementations (for instance, Rubinius allows &lt;code&gt;Class&lt;/code&gt; to be subclassed), I would hope that these unspecified behaviors be the subject of some discussion at the implementor level.&lt;/p&gt;
&lt;p&gt;In any event, I am thrilled at the news that the Ruby specification will become less ambiguous in the future!&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2011%2F09%2F05%2Fnew-hope-for-the-ruby-specification%2F';
  addthis_title  = 'New+Hope+for+The+Ruby+Specification';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=4Pt5hPHIpQk:iOyArsde7ek:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/4Pt5hPHIpQk" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2011/09/05/new-hope-for-the-ruby-specification/#comments" thr:count="2" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2011/09/05/new-hope-for-the-ruby-specification/feed/atom/" thr:count="2" />
		<thr:total>2</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2011/09/05/new-hope-for-the-ruby-specification/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[Understanding &#8220;Prototypes&#8221; in JavaScript]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/CeVa9V66Jj0/" />
		<id>http://yehudakatz.com/?p=639</id>
		<updated>2011-08-26T23:05:12Z</updated>
		<published>2011-08-13T00:33:53Z</published>
		<category scheme="http://yehudakatz.com" term="JavaScript" />		<summary type="html"><![CDATA[For the purposes of this post, I will be talking about JavaScript objects using syntax defined in ECMAScript 5.1. The basic semantics existed in Edition 3, but they were not well exposed. A Whole New Object In JavaScript, objects are pairs of keys and values (in Ruby, this structure is called a Hash; in Python, [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/">&lt;p&gt;For the purposes of this post, I will be talking about JavaScript objects using syntax defined in ECMAScript 5.1. The basic semantics existed in Edition 3, but they were not well exposed.&lt;/p&gt;
&lt;h1&gt;A Whole New Object&lt;/h1&gt;
&lt;p&gt;In JavaScript, objects are pairs of keys and values (in Ruby, this structure is called a Hash; in Python, it&amp;#8217;s called a dictionary). For example, if I wanted to describe my name, I could have an object with two keys: `firstName` would point to &amp;#8220;Yehuda&amp;#8221; and `lastName` would point to &amp;#8220;Katz&amp;#8221;. Keys in a JavaScript object are Strings.&lt;/p&gt;
&lt;p&gt;To create the simplest new object in JavaScript, you can use &lt;code&gt;Object.create&lt;/code&gt;:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// this creates an empty objects&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Why didn&amp;#8217;t we just use &lt;code&gt;var person = {};&lt;/code&gt;? Stick with me! To look up a value in the object by key, use bracket notation. If there is no value for the key in question, JavaScript will return `undefined`.&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;person&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;'name'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// undefined&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;If the String is a valid identifier[1], you can use the dot form:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;person.&lt;span style="color: #000066;"&gt;name&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// undefined&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;small&gt;&lt;em&gt;[1] in general, an &lt;a href="http://es5.github.com/#x7.6"&gt;identifier&lt;/a&gt; starts with a unicode letter, $, _, followed by any of the starting characters or numbers. A valid identifier must also not be a &lt;a href="http://es5.github.com/#x7.6.1"&gt;reserved word&lt;/a&gt;. There are other allowed characters, such as unicode combining marks, unicode connecting punctuation, and unicode escape sequences. Check out the spec for the full details&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;
&lt;h1&gt;Adding values&lt;/h1&gt;
&lt;p&gt;So now you have an empty object. Not that useful, eh? Before we can add some properties, we need to understand what a property (what the spec calls a &amp;#8220;named data property&amp;#8221;) looks like in JavaScript.&lt;/p&gt;
&lt;p&gt;Obviously, a property has a name and a value. In addition, a property can be &lt;strong&gt;enumerable&lt;/strong&gt;, &lt;strong&gt;configurable&lt;/strong&gt; and &lt;strong&gt;writable&lt;/strong&gt;. If a value is enumerable, it will show up when enumerating over an object using a &lt;code&gt;for(prop in obj)&lt;/code&gt; loop. If a property is writable, you can replace it. If a property is configurable, you can delete it or change its other attributes.&lt;/p&gt;
&lt;p&gt;In general, when we create a new property, we will want it to be enumerable, configurable, and writable. In fact, prior to ECMAScript 5, that was the only kind of property a user could create directly.&lt;/p&gt;
&lt;p&gt;We can add a property to an object using &lt;code&gt;Object.defineProperty&lt;/code&gt;. Let&amp;#8217;s add a first name and last name to our empty object:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
Object.&lt;span style="color: #660066;"&gt;defineProperty&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'firstName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  value&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Yehuda&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  writable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  enumerable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  configurable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
Object.&lt;span style="color: #660066;"&gt;defineProperty&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'lastName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  value&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Katz&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  writable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  enumerable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  configurable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Obviously, this is extremely verbose. We can make it a bit less verbose by eliminating the common defaults:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; config &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  writable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  enumerable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  configurable&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;true&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; defineProperty &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;obj&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; value&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  config.&lt;span style="color: #660066;"&gt;value&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; value&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  Object.&lt;span style="color: #660066;"&gt;defineProperty&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;obj&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; config&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'firstName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Yehuda&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'lastName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;   &lt;span style="color: #3366CC;"&gt;&amp;quot;Katz&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Still, this is pretty ugly to create a simple property list. Before we can get to a prettier solution, we will need to add another weapon to our JavaScript object arsenal.&lt;/p&gt;
&lt;h1&gt;Prototypes&lt;/h1&gt;
&lt;p&gt;So far, we&amp;#8217;ve talked about objects as simple pairs of keys and values. In fact, JavaScript objects also have one additional attribute: a pointer to &lt;em&gt;another&lt;/em&gt; object. We call this pointer the object&amp;#8217;s &lt;em&gt;prototype&lt;/em&gt;. If you try to look up a key on an object and it is not found, JavaScript will look for it in the prototype. It will follow the &amp;#8220;prototype chain&amp;#8221; until it sees a &lt;code&gt;null&lt;/code&gt; value. In that case, it returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You&amp;#8217;ll recall that we created a new object by invoking &lt;code&gt;Object.create(null)&lt;/code&gt;. The parameter tells JavaScript what it should set as the Object&amp;#8217;s &lt;em&gt;prototype&lt;/em&gt;. You can look up an object&amp;#8217;s prototype by using &lt;code&gt;Object.getPrototypeOf&lt;/code&gt;:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; man &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;man&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'sex'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;male&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; yehuda &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;man&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;yehuda&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'firstName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Yehuda&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;yehuda&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'lastName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Katz&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
yehuda.&lt;span style="color: #660066;"&gt;sex&lt;/span&gt;       &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;male&amp;quot;&lt;/span&gt;
yehuda.&lt;span style="color: #660066;"&gt;firstName&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Yehuda&amp;quot;&lt;/span&gt;
yehuda.&lt;span style="color: #660066;"&gt;lastName&lt;/span&gt;  &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Katz&amp;quot;&lt;/span&gt;
&amp;nbsp;
Object.&lt;span style="color: #660066;"&gt;getPrototypeOf&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;yehuda&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// returns the man object&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We can also add functions that we share across many objects this way:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'fullName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;firstName&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;' '&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;lastName&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// this time, let's make man's prototype person, so all&lt;/span&gt;
&lt;span style="color: #006600; font-style: italic;"&gt;// men share the fullName function&lt;/span&gt;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; man &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;man&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'sex'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;male&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; yehuda &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;man&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;yehuda&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'firstName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Yehuda&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
defineProperty&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;yehuda&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;'lastName'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Katz&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
yehuda.&lt;span style="color: #660066;"&gt;sex&lt;/span&gt;        &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;male&amp;quot;&lt;/span&gt;
yehuda.&lt;span style="color: #660066;"&gt;fullName&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Yehuda Katz&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h1&gt;Setting Properties&lt;/h1&gt;
&lt;p&gt;Since creating a new writable, configurable, enumerable property is pretty common, JavaScript makes it easy to do so using assignment syntax. Let&amp;#8217;s update the previous example using assignment instead of &lt;code&gt;defineProperty&lt;/code&gt;:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// instead of using defineProperty and specifying writable,&lt;/span&gt;
&lt;span style="color: #006600; font-style: italic;"&gt;// configurable, and enumerable, we can just assign the&lt;/span&gt;
&lt;span style="color: #006600; font-style: italic;"&gt;// value directly and JavaScript will take care of the rest&lt;/span&gt;
person&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;'fullName'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;firstName&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;' '&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;lastName&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// this time, let's make man's prototype person, so all&lt;/span&gt;
&lt;span style="color: #006600; font-style: italic;"&gt;// men share the fullName function&lt;/span&gt;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; man &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
man&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;'sex'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;male&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; yehuda &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;man&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
yehuda&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;'firstName'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Yehuda&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
yehuda&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;'lastName'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Katz&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
yehuda.&lt;span style="color: #660066;"&gt;sex&lt;/span&gt;        &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;male&amp;quot;&lt;/span&gt;
yehuda.&lt;span style="color: #660066;"&gt;fullName&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Yehuda Katz&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Just like when looking up properties, if the property you are defining is an &lt;em&gt;identifier&lt;/em&gt;, you can use dot syntax instead of bracket syntax. For instance, you could say &lt;code&gt;man.sex = "male"&lt;/code&gt; in the example above.&lt;/p&gt;
&lt;h1&gt;Object Literals&lt;/h1&gt;
&lt;p&gt;Still, having to set a number of properties every time can get annoying. JavaScript provides a literal syntax for creating an object and assigning properties to it at one time.&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; firstName&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Paul&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; lastName&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Irish&amp;quot;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This syntax is approximately sugar for:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;Object.&lt;span style="color: #660066;"&gt;prototype&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
person.&lt;span style="color: #660066;"&gt;firstName&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Paul&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
person.&lt;span style="color: #660066;"&gt;lastName&lt;/span&gt;  &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Irish&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The most important thing about the expanded form is that object literals &lt;em&gt;always&lt;/em&gt; set the newly created object&amp;#8217;s prototype to an object located at &lt;code&gt;Object.prototype&lt;/code&gt;. Internally, the object literal looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img style="display:block; margin-left:auto; margin-right:auto;" src="http://yehudakatz.com/wp-content/uploads/2011/08/prototype-chain.png" alt="prototype-chain.png" title="prototype-chain.png" border="0" width="464" height="440" /&gt;&lt;/p&gt;
&lt;p&gt;The default &lt;code&gt;Object.prototype&lt;/code&gt; dictionary comes with a number of the methods we have come to expect objects to contain, and through the magic of the prototype chain, all new objects created as object literal will contain these properties. Of course, objects can happily override them by defining the properties directly. Most commonly, developers will override the &lt;code&gt;toString&lt;/code&gt; method:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; alex &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; firstName&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Alex&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; lastName&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Russell&amp;quot;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
person.&lt;span style="color: #660066;"&gt;toString&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;[object Object]&amp;quot;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; brendan &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  firstName&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Brendan&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  lastName&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Eich&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  toString&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Brendan Eich&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
brendan.&lt;span style="color: #660066;"&gt;toString&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Brendan Eich&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is especially useful because a number of internal coercion operations use a supplied &lt;code&gt;toString&lt;/code&gt; method.&lt;/p&gt;
&lt;p&gt;Unfortunately, this literal syntax only works if we are willing to make the new object&amp;#8217;s prototype &lt;code&gt;Object.prototype&lt;/code&gt;. This eliminates the benefits we saw earlier of sharing properties using the prototype. In many cases, the convenience of the simple object literal outweighs this loss. In other cases, you will want a simple way to create a new object with a particular prototype. I&amp;#8217;ll explain it right afterward:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; fromPrototype &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;prototype&lt;span style="color: #339933;"&gt;,&lt;/span&gt; object&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; newObject &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;prototype&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #000066; font-weight: bold;"&gt;for&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; prop &lt;span style="color: #000066; font-weight: bold;"&gt;in&lt;/span&gt; object&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;if&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;object.&lt;span style="color: #660066;"&gt;hasOwnProperty&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;prop&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
      newObject&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;prop&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; object&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;prop&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;      
    &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; newObject&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  toString&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;firstName&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;' '&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;lastName&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; man &lt;span style="color: #339933;"&gt;=&lt;/span&gt; fromPrototype&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  sex&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;male&amp;quot;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; jeremy &lt;span style="color: #339933;"&gt;=&lt;/span&gt; fromPrototype&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;man&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  firstName&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Jeremy&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  lastName&lt;span style="color: #339933;"&gt;:&lt;/span&gt;  &lt;span style="color: #3366CC;"&gt;&amp;quot;Ashkenas&amp;quot;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
jeremy.&lt;span style="color: #660066;"&gt;sex&lt;/span&gt;        &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;male&amp;quot;&lt;/span&gt;
jeremy.&lt;span style="color: #660066;"&gt;toString&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Jeremy Ashkenas&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let&amp;#8217;s deconstruct the &lt;code&gt;fromPrototype&lt;/code&gt; method. The goal of this method is to create a new object with a set of properties, but with a particular prototype. First, we will use &lt;code&gt;Object.create()&lt;/code&gt; to create a new empty object, and assign the prototype we specify. Next, we will enumerate all of the properties in the object that we supplied, and copy them over to the new object.&lt;/p&gt;
&lt;p&gt;Remember that when you create an object literal, like the ones we are passing in to &lt;code&gt;fromPrototype&lt;/code&gt;, it will always have &lt;code&gt;Object.prototype&lt;/code&gt; as its prototype. By default, the properties that JavaScript includes on &lt;code&gt;Object.prototype&lt;/code&gt; are &lt;em&gt;not enumerable&lt;/em&gt;, so we don&amp;#8217;t have to worry about &lt;code&gt;valueOf&lt;/code&gt; showing up in our loop. However, since &lt;code&gt;Object.prototype&lt;/code&gt; is an object like any other object, anyone can define a new property on it, which may (and probably would) be marked enumerable.&lt;/p&gt;
&lt;p&gt;As a result, while we are looping through the properties on the object we passed in, we want to restrict our copying to properties that were defined on the object itself, and not found on the prototype. JavaScript includes a method called &lt;code&gt;hasOwnProperty&lt;/code&gt; on &lt;code&gt;Object.prototype&lt;/code&gt; to check whether a property was defined on the object itself. Since object literals will always have &lt;code&gt;Object.prototype&lt;/code&gt; as their prototype, you can use it in this manner.&lt;/p&gt;
&lt;p&gt;The object we created in the example above looks like this:&lt;/p&gt;
&lt;p&gt;&lt;img style="display:block; margin-left:auto; margin-right:auto;" src="http://yehudakatz.com/wp-content/uploads/2011/08/prototype-chain-2.png" alt="prototype-chain-2.png" title="prototype-chain-2.png" border="0" width="464" height="588" /&gt;&lt;/p&gt;
&lt;h1&gt;Native Object Orientation&lt;/h1&gt;
&lt;p&gt;At this point, it should be obvious that prototypes can be used to inherit functionality, much like traditional object oriented languages. To facilitate using it in this manner, JavaScript provides a &lt;code&gt;new&lt;/code&gt; operator.&lt;/p&gt;
&lt;p&gt;In order to facilitate object oriented programming, JavaScript allows you to use a Function object as a combination of a prototype to use for the new object and a constructor function to invoke:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; Person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;firstName&lt;span style="color: #339933;"&gt;,&lt;/span&gt; lastName&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;firstName&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; firstName&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;lastName&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; lastName&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
Person.&lt;span style="color: #660066;"&gt;prototype&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  toString&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;firstName&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;' '&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #660066;"&gt;lastName&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Here, we have a single Function object that is both a constructor function and an object to use as the prototype of new objects. Let&amp;#8217;s implement a function that will create new instances from this &lt;code&gt;Person&lt;/code&gt; object:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt; newObject&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;func&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// get an Array of all the arguments except the first one&lt;/span&gt;
  &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; args &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Array.&lt;span style="color: #660066;"&gt;prototype&lt;/span&gt;.&lt;span style="color: #660066;"&gt;slice&lt;/span&gt;.&lt;span style="color: #660066;"&gt;call&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;arguments&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #CC0000;"&gt;1&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #006600; font-style: italic;"&gt;// create a new object with its prototype assigned to func.prototype&lt;/span&gt;
  &lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; object &lt;span style="color: #339933;"&gt;=&lt;/span&gt; Object.&lt;span style="color: #660066;"&gt;create&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;func.&lt;span style="color: #660066;"&gt;prototype&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #006600; font-style: italic;"&gt;// invoke the constructor, passing the new object as 'this'&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// and the rest of the arguments as the arguments&lt;/span&gt;
  func.&lt;span style="color: #660066;"&gt;apply&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;object&lt;span style="color: #339933;"&gt;,&lt;/span&gt; args&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #006600; font-style: italic;"&gt;// return the new object&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; object&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; brendan &lt;span style="color: #339933;"&gt;=&lt;/span&gt; newObject&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;Person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Brendan&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Eich&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
brendan.&lt;span style="color: #660066;"&gt;toString&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Brendan Eich&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;new&lt;/code&gt; operator in JavaScript essentially does this work, providing a syntax familiar to those comfortable with traditional object oriented languages:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; mark &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;new&lt;/span&gt; Person&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;Mark&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Miller&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
mark.&lt;span style="color: #660066;"&gt;toString&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Mark Miller&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In essence, a JavaScript &amp;#8220;class&amp;#8221; is just a Function object that serves as a constructor plus an attached prototype object. I mentioned before that earlier versions of JavaScript did not have &lt;code&gt;Object.create&lt;/code&gt;. Since it is so useful, people often created something like it using the &lt;code&gt;new&lt;/code&gt; operator:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; createObject &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;o&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// we only want the prototype part of the `new`&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// behavior, so make an empty constructor&lt;/span&gt;
  &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt; F&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #006600; font-style: italic;"&gt;// set the function's `prototype` property to the&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// object that we want the new object's prototype&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// to be.&lt;/span&gt;
  F.&lt;span style="color: #660066;"&gt;prototype&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; o&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
  &lt;span style="color: #006600; font-style: italic;"&gt;// use the `new` operator. We will get a new&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// object whose prototype is o, and we will&lt;/span&gt;
  &lt;span style="color: #006600; font-style: italic;"&gt;// invoke the empty function, which does nothing.&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;new&lt;/span&gt; F&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;I really love that ECMAScript 5 and newer versions have begun to expose internal aspects of the implementation, like allowing you to directly define non-enumerable properties or define objects directly using the prototype chain.&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2011%2F08%2F12%2Funderstanding-prototypes-in-javascript%2F';
  addthis_title  = 'Understanding+%26%238220%3BPrototypes%26%238221%3B+in+JavaScript';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=CeVa9V66Jj0:RAldfagRVEs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/CeVa9V66Jj0" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/#comments" thr:count="30" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/feed/atom/" thr:count="30" />
		<thr:total>30</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[Understanding JavaScript Function Invocation and &#8220;this&#8221;]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/bKbTOr3MVfM/" />
		<id>http://yehudakatz.com/?p=617</id>
		<updated>2011-12-09T04:57:10Z</updated>
		<published>2011-08-11T07:54:22Z</published>
		<category scheme="http://yehudakatz.com" term="JavaScript" />		<summary type="html"><![CDATA[Over the years, I&#8217;ve seen a lot of confusion about JavaScript function invocation. In particular, a lot of people have complained that the semantics of `this` in function invocations is confusing. In my opinion, a lot of this confusion is cleared up by understanding the core function invocation primitive, and then looking at all other [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/">&lt;p&gt;Over the years, I&amp;#8217;ve seen a lot of confusion about JavaScript function invocation. In particular, a lot of people have complained that the semantics of `this` in function invocations is confusing.&lt;/p&gt;
&lt;p&gt;In my opinion, a lot of this confusion is cleared up by understanding the core function invocation primitive, and then looking at all other ways of invoking a function as sugar on top of that primitive. In fact, this is exactly how the ECMAScript spec thinks about it. In some areas, this post is a simplification of the spec, but the basic idea is the same.&lt;/p&gt;
&lt;h1&gt;The Core Primitive&lt;/h1&gt;
&lt;p&gt;First, let&amp;#8217;s look at the core function invocation primitive, a Function&amp;#8217;s &lt;code&gt;call&lt;/code&gt; method[1]. The call method is relatively straight forward.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Make an argument list (&lt;code&gt;argList&lt;/code&gt;) out of parameters 1 through the end&lt;/li&gt;
&lt;li&gt;The first parameter is &lt;code&gt;thisValue&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Invoke the function with &lt;code&gt;this&lt;/code&gt; set to &lt;code&gt;thisValue&lt;/code&gt; and the &lt;code&gt;argList&lt;/code&gt; as its argument list&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;For example:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt; hello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  console.&lt;span style="color: #660066;"&gt;log&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot; says hello &amp;quot;&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
hello.&lt;span style="color: #660066;"&gt;call&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;Yehuda&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;//=&amp;gt; Yehuda says hello world&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;As you can see, we invoked the &lt;code&gt;hello&lt;/code&gt; method with &lt;code&gt;this&lt;/code&gt; set to &lt;code&gt;"Yehuda"&lt;/code&gt; and a single argument &lt;code&gt;"world"&lt;/code&gt;. This is the core primitive of JavaScript function invocation. You can think of all other function calls as desugaring to this primitive. (to &amp;#8220;desugar&amp;#8221; is to take a convenient syntax and describe it in terms of a more basic core primitive).&lt;/p&gt;
&lt;p&gt;&lt;small&gt;&lt;em&gt;[1] In &lt;a href="http://es5.github.com/#x15.3.4.4"&gt;the ES5 spec&lt;/a&gt;, the &lt;code&gt;call&lt;/code&gt; method is described in terms of another, more low level primitive, but it&amp;#8217;s a very thin wrapper on top of that primitive, so I&amp;#8217;m simplifying a bit here. See the end of this post for more information.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;
&lt;h1&gt;Simple Function Invocation&lt;/h1&gt;
&lt;p&gt;Obviously, invoking functions with &lt;code&gt;call&lt;/code&gt; all the time would be pretty annoying. JavaScript allows us to invoke functions directly using the parens syntax (&lt;code&gt;hello("world")&lt;/code&gt;. When we do that, the invocation desugars:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt; hello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  console.&lt;span style="color: #660066;"&gt;log&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;Hello &amp;quot;&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// this:&lt;/span&gt;
hello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// desugars to:&lt;/span&gt;
hello.&lt;span style="color: #660066;"&gt;call&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;window&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This behavior has changed in ECMAScript 5 &lt;strong&gt;only when using strict mode&lt;/strong&gt;[2]:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #006600; font-style: italic;"&gt;// this:&lt;/span&gt;
hello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// desugars to:&lt;/span&gt;
hello.&lt;span style="color: #660066;"&gt;call&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;undefined&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The short version is: &lt;strong&gt;a function invocation like &lt;code&gt;fn(...args)&lt;/code&gt; is the same as &lt;code&gt;fn.call(window [ES5-strict: undefined], ...args)&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Note that this is also true about functions declared inline: &lt;code&gt;(function() {})()&lt;/code&gt; is the same as &lt;code&gt;(function() {}).call(window [ES5-strict: undefined)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;small&gt;&lt;em&gt;[2] Actually, I lied a bit. The ECMAScript 5 spec says that &lt;code&gt;undefined&lt;/code&gt; is (almost) always passed, but that the function being called should change its &lt;code&gt;thisValue&lt;/code&gt; to the global object when not in strict mode. This allows strict mode callers to avoid breaking existing non-strict-mode libraries.&lt;/em&gt;&lt;/small&gt;&lt;/p&gt;
&lt;h1&gt;Member Functions&lt;/h1&gt;
&lt;p&gt;The next very common way to invoke a method is as a member of an object (&lt;code&gt;person.hello()&lt;/code&gt;). In this case, the invocation desugars:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Brendan Eich&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  hello&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    console.&lt;span style="color: #660066;"&gt;log&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot; says hello &amp;quot;&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// this:&lt;/span&gt;
person.&lt;span style="color: #660066;"&gt;hello&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// desugars to this:&lt;/span&gt;
person.&lt;span style="color: #660066;"&gt;hello&lt;/span&gt;.&lt;span style="color: #660066;"&gt;call&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Note that it doesn't matter how the &lt;code&gt;hello&lt;/code&gt; method becomes attached to the object in this form. Remember that we previously defined &lt;code&gt;hello&lt;/code&gt; as a standalone function. Let's see what happens if we attach is to the object dynamically:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt; hello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  console.&lt;span style="color: #660066;"&gt;log&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot; says hello &amp;quot;&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Brendan Eich&amp;quot;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
person.&lt;span style="color: #660066;"&gt;hello&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; hello&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
person.&lt;span style="color: #660066;"&gt;hello&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// still desugars to person.hello.call(person, &amp;quot;world&amp;quot;)&lt;/span&gt;
&amp;nbsp;
hello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;[object DOMWindow]world&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Notice that the function doesn't have a persistent notion of its 'this'. It is always set at call time based upon the way it was invoked by its caller.&lt;/p&gt;
&lt;h1&gt;Using &lt;code&gt;Function.prototype.bind&lt;/code&gt;&lt;/h1&gt;
&lt;p&gt;Because it can sometimes be convenient to have a reference to a function with a persistent &lt;code&gt;this&lt;/code&gt; value, people have historically used a simple closure trick to convert a function into one with an unchanging &lt;code&gt;this&lt;/code&gt;:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Brendan Eich&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  hello&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    console.&lt;span style="color: #660066;"&gt;log&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #000066;"&gt;name&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot; says hello &amp;quot;&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; boundHello &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; person.&lt;span style="color: #660066;"&gt;hello&lt;/span&gt;.&lt;span style="color: #660066;"&gt;call&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #339933;"&gt;,&lt;/span&gt; thing&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
boundHello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Even though our &lt;code&gt;boundHello&lt;/code&gt; call still desugars to &lt;code&gt;boundHello.call(window, "world")&lt;/code&gt;, we turn right around and use our primitive &lt;code&gt;call&lt;/code&gt; method to change the &lt;code&gt;this&lt;/code&gt; value back to what we want it to be.&lt;/p&gt;
&lt;p&gt;We can make this trick general-purpose with a few tweaks:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; bind &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;func&lt;span style="color: #339933;"&gt;,&lt;/span&gt; thisValue&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
    &lt;span style="color: #000066; font-weight: bold;"&gt;return&lt;/span&gt; func.&lt;span style="color: #660066;"&gt;apply&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;thisValue&lt;span style="color: #339933;"&gt;,&lt;/span&gt; arguments&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; boundHello &lt;span style="color: #339933;"&gt;=&lt;/span&gt; bind&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person.&lt;span style="color: #660066;"&gt;hello&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; person&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
boundHello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Brendan Eich says hello world&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In order to understand this, you just need two more pieces of information. First, &lt;code&gt;arguments&lt;/code&gt; is an Array-like object that represents all of the arguments passed into a function. Second, the &lt;code&gt;apply&lt;/code&gt; method works exactly like the &lt;code&gt;call&lt;/code&gt; primitive, except that it takes an Array-like object instead of listing the arguments out one at a time.&lt;/p&gt;
&lt;p&gt;Our &lt;code&gt;bind&lt;/code&gt; method simply returns a new function. When it is invoked, our new function simply invokes the original function that was passed in, setting the original value as &lt;code&gt;this&lt;/code&gt;. It also passes through the arguments.&lt;/p&gt;
&lt;p&gt;Because this was a somewhat common idiom, ES5 introduced a new method &lt;code&gt;bind&lt;/code&gt; on all &lt;code&gt;Function&lt;/code&gt; objects that implements this behavior:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; boundHello &lt;span style="color: #339933;"&gt;=&lt;/span&gt; person.&lt;span style="color: #660066;"&gt;hello&lt;/span&gt;.&lt;span style="color: #660066;"&gt;bind&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
boundHello&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #006600; font-style: italic;"&gt;// &amp;quot;Brendan Eich says hello world&amp;quot;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is most useful when you need a raw function to pass as a callback:&lt;/p&gt;

&lt;div class="wp_syntax"&gt;&lt;div class="code"&gt;&lt;pre class="javascript" style="font-family:monospace;"&gt;&lt;span style="color: #003366; font-weight: bold;"&gt;var&lt;/span&gt; person &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
  &lt;span style="color: #000066;"&gt;name&lt;/span&gt;&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot;Alex Russell&amp;quot;&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
  hello&lt;span style="color: #339933;"&gt;:&lt;/span&gt; &lt;span style="color: #003366; font-weight: bold;"&gt;function&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt; console.&lt;span style="color: #660066;"&gt;log&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000066; font-weight: bold;"&gt;this&lt;/span&gt;.&lt;span style="color: #000066;"&gt;name&lt;/span&gt; &lt;span style="color: #339933;"&gt;+&lt;/span&gt; &lt;span style="color: #3366CC;"&gt;&amp;quot; says hello world&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
$&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #3366CC;"&gt;&amp;quot;#some-div&amp;quot;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;.&lt;span style="color: #660066;"&gt;click&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person.&lt;span style="color: #660066;"&gt;hello&lt;/span&gt;.&lt;span style="color: #660066;"&gt;bind&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;person&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #006600; font-style: italic;"&gt;// when the div is clicked, &amp;quot;Alex Russell says hello world&amp;quot; is printed&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This is, of course, somewhat clunky, and TC39 (the committee that works on the next version(s) of ECMAScript) continues to work on a more elegant, still-backwards-compatible solution.&lt;/p&gt;
&lt;h1&gt;On jQuery&lt;/h1&gt;
&lt;p&gt;Because jQuery makes such heavy use of anonymous callback functions, it uses the &lt;code&gt;call&lt;/code&gt; method internally to set the &lt;code&gt;this&lt;/code&gt; value of those callbacks to a more useful value. For instance, instead of receiving &lt;code&gt;window&lt;/code&gt; as &lt;code&gt;this&lt;/code&gt; in all event handlers (as you would without special intervention), jQuery invokes &lt;code&gt;call&lt;/code&gt; on the callback with the element that set up the event handler as its first parameter.&lt;/p&gt;
&lt;p&gt;This is extremely useful, because the default value of &lt;code&gt;this&lt;/code&gt; in anonymous callbacks is not particularly useful, but it can give beginners to JavaScript the impression that &lt;code&gt;this&lt;/code&gt; is, &lt;strong&gt;in general&lt;/strong&gt; a strange, often mutated concept that is hard to reason about.&lt;/p&gt;
&lt;p&gt;If you understand the basic rules for converting a sugary function call into a desugared &lt;code&gt;func.call(thisValue, ...args)&lt;/code&gt;, you should be able to navigate the not so treacherous waters of the JavaScript &lt;code&gt;this&lt;/code&gt; value.&lt;/p&gt;
&lt;p&gt;&lt;img style="display:block; margin-left:auto; margin-right:auto;" src="http://yehudakatz.com/wp-content/uploads/2011/08/this-table2.png" alt="this-table.png" title="this-table.png" border="0" width="526" height="195" /&gt;&lt;/p&gt;
&lt;h1&gt;PS: I Cheated&lt;/h1&gt;
&lt;p&gt;In several places, I simplified the reality a bit from the exact wording of the specification. Probably the most important cheat is the way I called &lt;code&gt;func.call&lt;/code&gt; a "primitive". In reality, the spec has a primitive (internally referred to as &lt;code&gt;[[Call]]&lt;/code&gt;) that both &lt;code&gt;func.call&lt;/code&gt; and &lt;code&gt;[obj.]func()&lt;/code&gt; use.&lt;/p&gt;
&lt;p&gt;However, take a look at the definition of &lt;code&gt;func.call&lt;/code&gt;:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;If IsCallable(func) is false, then throw a TypeError exception.&lt;/li&gt;
&lt;li&gt;Let argList be an empty List.&lt;/li&gt;
&lt;li&gt;If this method was called with more than one argument then in left to right order starting with arg1 append each argument as the last element of argList&lt;/li&gt;
&lt;li&gt;Return the result of calling the [[Call]] internal method of func, providing thisArg as the this value and argList as the list of arguments.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;As you can see, this definition is essentially a very simple JavaScript language binding to the primitive &lt;code&gt;[[Call]]&lt;/code&gt; operation.&lt;/p&gt;
&lt;p&gt;If you look at the definition of invoking a function, the first seven steps set up &lt;code&gt;thisValue&lt;/code&gt; and &lt;code&gt;argList&lt;/code&gt;, and the last step is: "Return the result of calling the [[Call]] internal method on func, providing thisValue as the this value and providing the list argList as the argument values."&lt;/p&gt;
&lt;p&gt;It's essentially identical wording, once the &lt;code&gt;argList&lt;/code&gt; and &lt;code&gt;thisValue&lt;/code&gt; have been determined.&lt;/p&gt;
&lt;p&gt;I cheated a bit in calling &lt;code&gt;call&lt;/code&gt; a primitive, but the meaning is essentially the same as had I pulled out the spec at the beginning of this article and quoted chapter and verse.&lt;/p&gt;
&lt;p&gt;There are also some additional cases (most notably involving &lt;code&gt;with&lt;/code&gt;) that I didn't cover here.&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2011%2F08%2F11%2Funderstanding-javascript-function-invocation-and-this%2F';
  addthis_title  = 'Understanding+JavaScript+Function+Invocation+and+%26%238220%3Bthis%26%238221%3B';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=bKbTOr3MVfM:mbokqisykys:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/bKbTOr3MVfM" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/#comments" thr:count="20" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/feed/atom/" thr:count="20" />
		<thr:total>20</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2011/08/11/understanding-javascript-function-invocation-and-this/</feedburner:origLink></entry>
		<entry>
		<author>
			<name>wycats</name>
					</author>
		<title type="html"><![CDATA[What&#8217;s Up With All These Changes in Rails?]]></title>
		<link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/KatzGotYourTongue/~3/nOjsBTaJiqA/" />
		<id>http://yehudakatz.com/?p=608</id>
		<updated>2011-06-14T18:39:02Z</updated>
		<published>2011-06-14T18:31:47Z</published>
		<category scheme="http://yehudakatz.com" term="Rails 3" /><category scheme="http://yehudakatz.com" term="Rubinius" /><category scheme="http://yehudakatz.com" term="Ruby on Rails" />		<summary type="html"><![CDATA[Yesterday, there was a blog post entitled &#8220;What the Hell is Happening to Rails&#8221; that stayed at the number one spot on Hacker News for quite a while. The post and many (but not most) the comments on the post reflect deep-seated concern about the recent direction of Rails. Others have addressed the core question [...]]]></summary>
		<content type="html" xml:base="http://yehudakatz.com/2011/06/14/what-the-hell-is-happening-to-rails/">&lt;p&gt;Yesterday, there was a blog post entitled &amp;#8220;What the Hell is Happening to Rails&amp;#8221; that stayed at the number one spot on Hacker News for quite a while. The post and many (but not most) the comments on the post reflect deep-seated concern about the recent direction of Rails. Others have addressed the core question about change in the framework, but I&amp;#8217;d like to address questions about specific changes that came up in the post and comments.&lt;/p&gt;
&lt;p&gt;The intent of this post is not to nitpick the specific arguments that were made, or to address the larger question of how much change is appropriate in the framework, but rather to provide some background on some of the changes that have been made since Rails 2.3 and to explain why we made them.&lt;/p&gt;
&lt;h2&gt;Block Helpers&lt;/h2&gt;
&lt;blockquote&gt;&lt;p&gt;I too get a feeling of &amp;#8220;change for the sake of change&amp;#8221; from Rails at times. That&amp;#8217;s obviously not something they&amp;#8217;re doing, as all the changes have some motivation, but at times it feels a bit like churn.&lt;br /&gt;
At one point in time, you did forms like &amp;lt;%= form &amp;#8230;. and then they switched to &amp;lt;% form &amp;#8230;. do and now they&amp;#8217;ve switched back to &amp;lt;%= form &amp;#8230; do again.&lt;br /&gt;
Also, the upgrade to Rails 3 is not an easy one. Yeah, you get some nice stuff, but because it&amp;#8217;s so painful, it&amp;#8217;s not happening for a lot of people, which is causing more problems.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Prior to Rails 3.0, Rails never used &lt;code&gt;&amp;lt;= form_for&lt;/code&gt; because it was technically very difficult to make it work. I wrote &lt;a href="http://yehudakatz.com/2009/08/31/simplifying-rails-block-helpers-with-a-side-of-rubinius/"&gt;a post about it&lt;/a&gt; in Summer 2009 that walked through the technical problems. The short version is that every ERB template is compiled into a Ruby method, and reliably converting &lt;code&gt;&amp;amp;lt%=&lt;/code&gt; with blocks proved to be extremely complicated.&lt;/p&gt;
&lt;p&gt;However, knowing when to use &lt;code&gt;&amp;lt;%&lt;/code&gt; and when to use &lt;code&gt;&amp;lt;=&lt;/code&gt; caused major issues for new developers, and it was a major 3.0 priority to make this work. In addition, because of the backwards compatibility issue, we went to extreme Ruby-exploiting lengths to enable deprecation warnings about the change, so that we could fix the issue for new apps, but also not break old apps.&lt;/p&gt;
&lt;p&gt;The night José and I figured out how to do this (the Engine Yard party at Mountain West RubyConf 2009), we were pretty close to coming to the conclusion that it couldn&amp;#8217;t be done short of using a full language parser in ERB, which should give you some sense of how vexing a problem it was for us.&lt;/p&gt;
&lt;h2&gt;Performance&lt;/h2&gt;
&lt;blockquote&gt;&lt;p&gt;The general disregard for improving performance, inherited from Ruby, is also something endemic from a while back.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;Yes, there have been some performance regressions in Rails 3.0. However, the idea that the Rails core team doesn&amp;#8217;t care about performance, and neither does the Ruby team doesn&amp;#8217;t pass the smell test.&lt;/p&gt;
&lt;p&gt;Aaron Patterson, the newest core team member, worked full time for almost a year to get the new ActiveRecord backend into decent performance shape. Totally new code often comes with some performance regressions, and the changes to ActiveRecord were important and a long-time coming. Many of us didn&amp;#8217;t see the magnitude of the initial problem until it was too late for 3.0, but we take the problem extremely seriously.&lt;/p&gt;
&lt;p&gt;Ruby core (&amp;#8220;MRI&amp;#8221;) itself has sunk an enormous amount of time into performance improvements in Ruby 1.9, going so far as to completely rewrite the core VM from scratch. This resulted in significant performance improvements, and the Ruby team continues to work on improvements to performance and to core systems like the garbage collector.&lt;/p&gt;
&lt;p&gt;The Ruby C API poses some long-term problems for the upper-bound of Ruby performance improvements, but the JRuby and Rubinius projects are showing how you can use Ruby C extensions inside a state-of-the-art virtual machine. Indeed, the JRuby and Rubinius projects show that the Ruby community both cares about, and is willing to invest significant energy into improving the performance of Ruby.&lt;/p&gt;
&lt;h2&gt;Heroku and the Asset Pipeline&lt;/h2&gt;
&lt;blockquote&gt;&lt;p&gt;The assets pipeline feels horrible, it&amp;#8217;s really slow. I upgraded to Rails 3.1rc, realized it fights with Heroku unless upgrading to the Cedar stack&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The problem with Heroku is that the default Gemfile that comes with Rails 3.1 currently requires a JavaScript runtime to boot, even in production. &lt;strong&gt;This is clearly wrong&lt;/strong&gt; and will be fixed posthaste. Requiring Rails apps to have node or a compiled v8 in production is an unacceptable burden.&lt;/p&gt;
&lt;p&gt;On the flip side, the execjs gem, which manages compiling CoffeeScript (and more importantly minifying your JavaScript), is actually a pretty smart piece of work. It turns out that both Mac OSX and Windows ship with usable JavaScript binaries, so in development, most Rails users will already have a JavaScript library ready to use.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s worth noting that a JavaScript engine is needed to run &amp;#8220;uglify.js&amp;#8221;, the most popular and most effective JavaScript minifier. It is best practice to minify your JavaScript before deployment, so you can feel free to format and comment your code as you like without effecting the payload. You can learn more about minification in &lt;a href="https://developer.yahoo.com/blogs/ydn/posts/2007/07/high_performanc_8/"&gt;this excellent post by Steve Souders&lt;/a&gt; from a few years back. Rails adding minification by default is an unambiguous improvement in the workflow of Rails applications, because it makes it easy (almost invisible) to do something that everyone &lt;em&gt;should&lt;/em&gt; be doing, but which has previously been something of a pain.&lt;/p&gt;
&lt;p&gt;Again, making node a dependency in production is clearly wrong, and will be removed before the final release.&lt;/p&gt;
&lt;h2&gt;Change for Change&amp;#8217;s Sake&lt;/h2&gt;
&lt;blockquote&gt;&lt;p&gt;The problem with Rails is not the pace of change so much as the wild changes of direction it takes, sometimes introducing serious performance degradations into official releases. Sometimes it&amp;#8217;s hard to see a guiding core philosophy other than the fact they want to be on the shiny edge&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;When Rails shipped, it came with a number of defaults:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;ActiveRecord&lt;/li&gt;
&lt;li&gt;Test::Unit&lt;/li&gt;
&lt;li&gt;ERB&lt;/li&gt;
&lt;li&gt;Prototype&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Since 2004, alternatives to all of those options have been created, and in many cases (Rspec, jQuery, Haml) have become somewhat popular. Rails 3.0 made &lt;strong&gt;no&lt;/strong&gt; changes to the defaults, despite much clamoring for a change from Prototype to jQuery. Rails 3.1 changed to jQuery only when it became overwhelmingly clear that jQuery was the de facto standard on the web.&lt;/p&gt;
&lt;p&gt;As someone who has sat in on many discussions about changing defaults, I can tell you that defaults in Rails are not changed lightly, and certainly not &amp;#8220;to be on the shiny edge.&amp;#8221; In fact, I think that Rails is more conservative than most would expect in changing defaults.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;Right, but the problem is that there doesn&amp;#8217;t seem to be a &amp;#8216;right&amp;#8217; way, That&amp;#8217;s the problem.&lt;/p&gt;
&lt;p&gt;We were all prototype a few years ago, now it jquery &amp;#8230; we (well I) hadn&amp;#8217;t heard of coffeescript till a few months ago and now its a default option in rails, The way we were constructing ActiveRecord finders had been set all through Rails 2, now we&amp;#8217;ve changed it, the way we dealt with gems was set all through rails 2 now its changed completely in Rails 3.&lt;/p&gt;
&lt;p&gt;I like change, I like staying on the cutting edge of web technologies, but I don&amp;#8217;t want to learn something, only to discard it and re-do it completely to bring it up to date with a new way of doing things all the time.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;First, jQuery has become the de facto standard on the web. As I said earlier, Rails resisted making this change in 3.0, despite a lot of popular demand, and the change to jQuery is actually an example of the stability of Rails&amp;#8217; default choices over time, rather than the opposite.&lt;/p&gt;
&lt;p&gt;Changes to gem handling evolved as the Rails community evolved to use gems more. In Rails 1, all extensions were implemented as plugins that got pulled out of svn and dumped into your project. This didn&amp;#8217;t allow for versioning or dependencies, so Rails 2 introduced first-class support for Rails plugins as gems.&lt;/p&gt;
&lt;p&gt;During the Rails 2 series, Rails added a dependency on Rack, which caused serious problems when Rails was used with other gems that rely on Rack, due to the way that raw Rubygems handles dependency activation. Because Rails 3 uses gem dependencies more extensively, we spent a year building bundler, which adds per-application dependency resolution to Rubygems. This was simply the natural evolution of the way that Rails has used dependencies over time.&lt;/p&gt;
&lt;p&gt;The addition of CoffeeScript is interesting, because it&amp;#8217;s a pretty young technology, but it&amp;#8217;s also not really different from shipping a new template handler. When you create a new Rails app, the JavaScript file is a regular JS file, and the asset compilation and dependency support does not require CoffeeScript. Shipping CoffeeScript is basically like shipping Builder: should you want it, it&amp;#8217;s there for you. Since we want to support minification out of the box anyway, CoffeeScript doesn&amp;#8217;t add any new requirements. And since it&amp;#8217;s just there in the default Gemfile, as opposed to included in Rails proper like Builder, turning it off (if you really want to) is as simple as removing a line in your Gemfile. Nothing scary here.&lt;/p&gt;
&lt;h2&gt;ActiveRecord Changes&lt;/h2&gt;
&lt;blockquote&gt;&lt;p&gt;The way we were constructing ActiveRecord finders had been set all through Rails 2, now we&amp;#8217;ve changed it&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;&amp;#8230;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;The Rails core team does seem to treat the project as if it&amp;#8217;s a personal playground&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;One of the biggest problems with ActiveRecord was the way it internally used Strings to represent queries. This meant that changes to queries often required &lt;code&gt;gsub&lt;/code&gt;ing String to make simple changes. Internally, it was a mess, and it expressed itself in the public API in how conditions were generated, and more importantly how named scopes were created.&lt;/p&gt;
&lt;p&gt;One goal of the improvements in Rails 3 was to get rid of the ad-hoc query generation in Rails 2 and replace it with something better. ActiveRelation, this library, was literally multiple years in the making, and a large amount of the energy in the Rails 3.0 process was spent on integrating ActiveRelation.&lt;/p&gt;
&lt;p&gt;From a user-facing perspective, we wanted to unify all of the different ways that queries were made. This means that scopes, class methods, and one-time-use queries all use the same API. As in the &lt;code&gt;&amp;lt;=&lt;/code&gt; case, a significant amount of effort was spent on backwards compatibility with Rails 2.3. In fact, we decided to hold onto support for the old API as least as long as Rails 3.2, in order to soften the community transition to the new API.&lt;/p&gt;
&lt;p&gt;In general, we were quite careful about backwards compatibility in Rails 3.0, and while a project as complex as Rails was not going to be perfect in this regard, characterizing the way we handled this as &amp;#8220;irresponsible&amp;#8221; or &amp;#8220;playground&amp;#8221; disregards the tremendous amount of work and gymnastics that the core team and community contributors put into supporting Rails 2.3 APIs across the entire codebase when changes were made.&lt;/p&gt;
&lt;script type="text/javascript"&gt;
  addthis_url    = 'http%3A%2F%2Fyehudakatz.com%2F2011%2F06%2F14%2Fwhat-the-hell-is-happening-to-rails%2F';
  addthis_title  = 'What%26%238217%3Bs+Up+With+All+These+Changes+in+Rails%3F';
  addthis_pub    = '';
&lt;/script&gt;&lt;script type="text/javascript" src="http://s7.addthis.com/js/addthis_widget.php?v=12" &gt;&lt;/script&gt;
&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/KatzGotYourTongue?a=nOjsBTaJiqA:jBMWOAf6yY8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/KatzGotYourTongue?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/KatzGotYourTongue/~4/nOjsBTaJiqA" height="1" width="1"/&gt;</content>
		<link rel="replies" type="text/html" href="http://yehudakatz.com/2011/06/14/what-the-hell-is-happening-to-rails/#comments" thr:count="48" />
		<link rel="replies" type="application/atom+xml" href="http://yehudakatz.com/2011/06/14/what-the-hell-is-happening-to-rails/feed/atom/" thr:count="48" />
		<thr:total>48</thr:total>
	<feedburner:origLink>http://yehudakatz.com/2011/06/14/what-the-hell-is-happening-to-rails/</feedburner:origLink></entry>
	</feed><!-- Dynamic page generated in 3.330 seconds. --><!-- Cached page generated by WP-Super-Cache on 2012-05-15 13:21:31 -->

