<?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">
 
 <title>Recursive</title>
 
 <link href="http://killdream.github.com/" />
 <updated>2013-04-27T14:25:21-07:00</updated>
 <id>http://killdream.github.com/</id>
 <author>
   <name>Sorella's Basement</name>
   <email>quildreen@gmail.com</email>
 </author>

 
 <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/quildreen/en" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="quildreen/en" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry>
   <title>The Hikikomori's Guide to JavaScript</title>
   <link href="http://killdream.github.com//blog/2013/04/the-hikkikomoris-guide-to-javascript/index.html" />
   <updated>2013-04-27T00:00:00-07:00</updated>
   <id>http://killdream.github.com//blog/2013/04/the-hikkikomoris-guide-to-javascript/the-hikkikomoris-guide-to-javascript</id>
   <content type="html">&lt;blockquote&gt;

&lt;p&gt;“The Answer to the Great Question&amp;hellip; Of Life, the Universe and
Everything&amp;hellip; Is&amp;hellip; Forty-two,' said Deep Thought, with infinite
majesty and calm.”
&lt;/p&gt;
&lt;p&gt;
— Douglas Adams in The Hitchhiker's Guide to the Galaxy.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
Why am I quoting Douglas Adams' most awesome book? Why am I using a
wordplay on my blog post's title? Why are JavaScript's modules such a
mess? How do brains work? Well, my dearest basement dwellers, I am
afraid I do not have all the answers, but none the less I shall try to
provide some tips on writing useful, simple and composable applications
for the Great Good™ of the JavaScript community here.
&lt;/p&gt;
&lt;p&gt;
Here's a TL;DR:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Start with an API to manipulate your data, not with an Application.

&lt;/li&gt;
&lt;li&gt;Extract the most basic, orthogonal concepts, and provide primitives
     to encode them.

&lt;/li&gt;
&lt;li&gt;Provide “glue” code to compose concepts together and create big
     things.

&lt;/li&gt;
&lt;li&gt;Work with structured data.

&lt;/li&gt;
&lt;li&gt;Write the actual application using your API.
&lt;/li&gt;
&lt;/ol&gt;




&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;Introduction&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
There was a time when no one knew how to write JavaScript, but those
times are long since gone. People know how to write JavaScript now
(some), and that's good!!1ELEVEN! Unfortunately, there's still a large
portion of people who don't know how to write applications&lt;sup&gt;&lt;a class="footref" name="fnr.1" href="#fn.1"&gt;1&lt;/a&gt;&lt;/sup&gt;.
&lt;/p&gt;
&lt;p&gt;
As a result of this, you often end up with applications that do too
much, or applications that do too little. But the worst problem of all
is when you end up with applications that you can only use through some
human interface of sorts, and can't easily manipulate the stuff you're
interested in with different things. Mind you, programmatic extensions
matter a lot!
&lt;/p&gt;
&lt;p&gt;
Thus, in this blog post I'll try to provide a few hints on how to
achieve small, composable and extensible applications. Stick with me!
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;Start with an API&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
Don't start with an Application, start with the API. This might seem
weird, but it actually makes a lot of sense, because the API defines how
other computer things can interact with the data your application
manipulates. But hey, why should we leave that as “other computer
things”? Let me rephrase:
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;The API defines how your application (and other computer things) can
interact with the data you're interested in.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
My usual design approach is to examine the inputs and outputs of the
application, and then design an API that handles such data. Later I'll
stack the user experience and the “glue” code on top of such API to form
an actual application.
&lt;/p&gt;
&lt;p&gt;
This fits nicely with the "One Module Does One Thing" approach because
then the API defines which kind of data you're dealing with, and all the
meaningful transformations you can apply to such data. Transformations
in turn do one thing, and might be grouped logically. Modules are really
just a logical group of computations, that it happens to be a file in
many languages is just an accident.
&lt;/p&gt;
&lt;p&gt;
For example, let's say we want to write an application to display Tweets
to the user. First and foremost, we examine the inputs and outputs, and
draft some types to encode the data in our application (I'm using a type
notation inspired by Haskell, but this should be nonetheless pretty
straightforward):
&lt;/p&gt;



&lt;pre class="src src-haskell"&gt;&lt;span style="color: #ACAE95;"&gt;-- | A TwitterTweet is something Twitter gives us (the input)&lt;/span&gt;
&lt;span style="color: #ACAE95;"&gt;-- (note that this is a stripped down version)&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;TwitterTweet&lt;/span&gt;
  favorited     &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Boolean&lt;/span&gt;
  retweeted     &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Boolean&lt;/span&gt;
  retweet_count &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Number&lt;/span&gt;

  created_at &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;  &lt;span style="color: #ACAE95;"&gt;-- ^ Serialised Date&lt;/span&gt;
  source     &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
  user       &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;User&lt;/span&gt;
  id_str     &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;

  in_reply_to_user_id_str   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
  in_reply_to_status_id_str &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
  in_reply_to_screen_name   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;

  entities   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; { &lt;span style="color: #A6E32D;"&gt;"urls"&lt;/span&gt;          &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt;]
                , &lt;span style="color: #A6E32D;"&gt;"hashtags"&lt;/span&gt;      &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt;]
                , &lt;span style="color: #A6E32D;"&gt;"user_mentions"&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt;]
                }

  text &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;    
&lt;/pre&gt;


&lt;p&gt;
So, the data Twitter gives us is quite a mess, and it'd be really
difficult to manipulate that kind of data in our application. We can do
better, so let's define a better type to encode a Tweet:
&lt;/p&gt;



&lt;pre class="src src-haskell"&gt;&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;User&lt;/span&gt;
  name &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #ACAE95;"&gt;-- ^ The users' screen name&lt;/span&gt;
  id   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #ACAE95;"&gt;-- ^ Twitter's user ID&lt;/span&gt;

&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt;
  url          &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
  expandedUrl  &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
  displayUrl   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
  indices      &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; (&lt;span style="color: #FC951E;"&gt;Number&lt;/span&gt;, &lt;span style="color: #FC951E;"&gt;Number&lt;/span&gt;)


&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Text&lt;/span&gt;
  plain    &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
  entities &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; { &lt;span style="color: #A6E32D;"&gt;"urls"&lt;/span&gt;          &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt;]
              , &lt;span style="color: #A6E32D;"&gt;"hashtags"&lt;/span&gt;      &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt;]
              , &lt;span style="color: #A6E32D;"&gt;"user_mentions"&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt;]
              }

&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Tweet&lt;/span&gt;
  id            &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #ACAE95;"&gt;-- ^ Unique identifier for this tweet&lt;/span&gt;
  user          &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;User&lt;/span&gt;   &lt;span style="color: #ACAE95;"&gt;-- ^ The user that tweeted this&lt;/span&gt;
  inReplyTo     &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Tweet&lt;/span&gt;
  source        &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #ACAE95;"&gt;-- ^ Which application was used to compose this&lt;/span&gt;
  date          &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Date&lt;/span&gt;   &lt;span style="color: #ACAE95;"&gt;-- ^ When this tweet was crafted&lt;/span&gt;
  favourited    &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Boolean&lt;/span&gt;
  retweeted     &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Boolean&lt;/span&gt;
  retweetCount  &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Number&lt;/span&gt;
  text          &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Text&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
So, now &lt;code&gt;User&lt;/code&gt; and &lt;code&gt;Text&lt;/code&gt; are separate types, this is because they make
sense outside of the context of a &lt;code&gt;Tweet&lt;/code&gt; and we might want to
manipulate them separately. There's no reason to provide a complected
type to a function that only needs to know the name of a user, for
example.
&lt;/p&gt;
&lt;p&gt;
Once we're done with the types our application needs to manipulate, we
can draft an API that provides the primitives to manipulate these
types, given the operations we'll be applying to them and the output.
&lt;/p&gt;



&lt;pre class="src src-haskell"&gt;&lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;* Type conversions&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | We need to convert from Twitter format to ours&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;normaliseTweet&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;TwitterTweet&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Tweet&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | Convert Twitter Date serialisation to actual DateTime&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;parseDate&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Date&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | Extract the User that composed the tweet&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;twittedBy&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;TwitterText&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;User&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | Extract reply information&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;repliedToUser&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;TwitterText&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;User&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;repliedToTweet&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;TwitterText&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Tweet&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | Extract the Text&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;textFor&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;TwitterText&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Text&lt;/span&gt;


&lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;* Display transformations&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(These are application-specific because they only make sense in the&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;context of user-facing HTML pages)&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | We want to display a Tweet as HTML&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;renderTweet&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Tweet&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;HTML&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | We want to display a Text as HTML&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;textToHTML&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Text&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;HTML&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | We want to know the relative time since the tweet&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;fromNow&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Date&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | We want to display a link to a User&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;linkToUser&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;User&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;HTML&lt;/span&gt;

&lt;span style="color: #ACAE95;"&gt;-- | We also want to display a link to a Tweet&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;linkToTweet&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Tweet&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;HTML&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
If there's one hint I can provide when doing the initial API design, it
would be:
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;Extract the most basic, orthogonal concepts, and provide primitives to
encode them.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
You can always add combinators on top of those minimal and simple
primitives to let them do more stuff. Working with reeeeally small set
of primitives and a lot of combinators means you get to write simple
code that actually scales! But then, picking the right primitives can be
really hard at times, so you need to have a good deal of knowledge about
the domain you're trying to encode in your API.
&lt;/p&gt;


&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;Provide “glue” code to compose concepts&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
Compositionality is a big thing. Compositionality is what you want in a
big application. Compositionality is what will save your bacon when you
have to actually maintain all the shit you've written. This is one of
the reasons we don't put them in the first API draft, we want to get the
primitives right first, and make sure they don't overlap!
&lt;/p&gt;
&lt;p&gt;
Back to our Twitter example, when you retrieve data from Twitter, you
usually get a List of tweets. Notice that nothing in the previous API
allows you to take a list of Tweets and spits back a list of HTMLs, but
it can take a single tweet and spit back a single HTML. We also have
baked right into the standard library a function that takes a List of
things, a function that transforms a thing A into thing B, and returns a
list of things B. Well, this is enough to derive our transformation for
lists of Tweets:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Renders a list of Tweets&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;renderTweetList :: [Tweet] -&amp;gt; [HTML]&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;renderTweetList&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;tweets&lt;/span&gt;) {
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; tweets.map(renderTweet)
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Or we can use a better version of Map (if you know functional&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;programming) &lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;map&lt;/span&gt; = curry(2, Function.call.bind([].map))
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;renderTweetList&lt;/span&gt; = map(renderTweet)


&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;-- An aside: ------------------------------------------------------&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;If you don't know what `curry` is, well... A minimal explanation&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;would be that functions in JavaScript actually takes a List of&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;arguments. You should think about:&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;add&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;a&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;b&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; a + b }

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;As being actually:&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;add&lt;/span&gt;(&lt;span style="color: #FA2573;"&gt;arguments&lt;/span&gt;){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;arguments&lt;/span&gt;[0] + &lt;span style="color: #FA2573;"&gt;arguments&lt;/span&gt;[1] }

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And when you're calling it as: add(1, 2) you're actually saying&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;add([1, 2]).&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Currying takes a different route. Functions takes only one&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;argument:&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;itself&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;a&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; a }

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;If you need to create a function that takes more than one argument,&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;you use closures:&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;add&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;a&lt;/span&gt;){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; to(b) { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; a + b }}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And when you're calling it as: add(1, 2) you're actually saying&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;add(1)(2).&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;You can see an implementation of currying here:&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;https://github.com/killdream/athena/blob/master/src/higher-order.ls#L56-L81&lt;/span&gt;
&lt;/pre&gt;




&lt;p&gt;
But this doesn't display anything in the screen yet, mostly because
that's not the job of renderTweetList — it already does everything it
needs to do. A thing that displays tweets on the screen should be
something that takes an HTML, a container and adds that HTML to the
container:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;addTo :: HTML, HTML -&amp;gt; HTML&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;addTo&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;container&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;html&lt;/span&gt;) {
  $(container).append(html)
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; container
}
&lt;/pre&gt;


&lt;p&gt;
Now we can derive a simple function that will take a list of HTML
things, and add them to a container:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;addAllTo :: HTML, [HTML] -&amp;gt; HTML&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;addAllTo&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;container&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;htmls&lt;/span&gt;) {
  htmls.map(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;html&lt;/span&gt;){ addTo(container, html) })
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; container
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Or, we can go use our Curry friend and make it better-er&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;addTo&lt;/span&gt; = curry(2, addTo)
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;addAllTo&lt;/span&gt; = curry(2, &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;container&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;htmls&lt;/span&gt;) {
  htmls.map(addTo(container))
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; container
})
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;Work with structured data&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;


&lt;p&gt;
I can't stress this point enough! If you want people to actually use
your API in a meaningful way, you &lt;b&gt;must&lt;/b&gt; work with structured
data. Please don't “but strings are easy!” me. Strings might be easy,
but we don't want &lt;b&gt;easy&lt;/b&gt; when designing an API, we want &lt;b&gt;simple&lt;/b&gt; &lt;sup&gt;&lt;a class="footref" name="fnr.2" href="#fn.2"&gt;2&lt;/a&gt;&lt;/sup&gt;. 
Simple stuff sometimes means you get to write more, but also means that
you get something that's more meaningful overall, that's extensible and
that composes well with other things without randomly breaking for no
good reason. When you pass Strings around for other people to parse you
lose all the guarantees that they'll agree with each other on the
structure your API (and external APIs) expect.
&lt;/p&gt;
&lt;p&gt;
In the case of our API example, it would mean passing around &lt;code&gt;Tweet&lt;/code&gt;
types, rather than the HTML representation of them!. All of the central
points of the API should accept one of our types (&lt;code&gt;Tweet&lt;/code&gt;, &lt;code&gt;User&lt;/code&gt;,
&lt;code&gt;Text&lt;/code&gt;), not arbitrary HTML or plain text strings, because then everyone
can encode that slightly different.
&lt;/p&gt;
&lt;p&gt;
“So, what if I want to send it over to someone else over the wire?
Wouldn't it be better if I just use the representation that the other
side will use to display the thing?”
&lt;/p&gt;
&lt;p&gt;
Well, think about the following scenario: You have your application
sending tweets to a logger that will display them. You want to “Keep It
Easy”, and so decides it's a good idea to just send the way you want
tweets to be displayed on the other side, so people don't need to write
anything besides &lt;code&gt;console.log&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
A few weeks later someone comes up to those guys and say, “Hey, we're
going to log only stuff that got retweeted at least 50 times.” The
other-side guys quickly hack together a regular expression that looks
for &lt;code&gt;/(\d+) retweet/&lt;/code&gt; and call it a day.
&lt;/p&gt;
&lt;p&gt;
Some days later you decide &lt;code&gt;retweet&lt;/code&gt; is too long and it's taking
valuable space on the screen of your application now that you're porting
it over to mobile devices. Then you decide to shorten that to
&lt;code&gt;rt&lt;/code&gt;. Guess who just got all their system's screwed?
&lt;/p&gt;
&lt;p&gt;
If you pass over structured data, then it's simple. They wouldn't even
need to touch their main system if they didn't want to, just put on a
proxy in front of the service with this code:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;next(tweets.filter(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;tweet&lt;/span&gt;){ tweet.retweetCount &amp;gt; 50 }))
&lt;/pre&gt;


&lt;p&gt;
If you need to communicate data with other services, you should just
encode a structured representation using the best serialisation format
for the job. JSON everywhere won't cut it, as won't XML. JSON is a
generic data serialisation format as plain text, and XML is a &lt;b&gt;document&lt;/b&gt;
serialisation format as plain text. They're cool if they fit your data,
and you don't care about the additional bandwidth/encoding
time. Otherwise there are other stuff like Protocol Buffers to take a
look at.
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;Warning&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
    Please please please please please please! For the love of God,
    don't use XML to encode general data. XML is a document
    serialisation format, it's something you use to serialise &lt;b&gt;TREE     STRUCTURES&lt;/b&gt;. Mind you, Lists are not the best case for XML,
    dictionaries aren't either. Use a general data serialisation format
    for everything that isn't a tree.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5" class="outline-2"&gt;
&lt;h2 id="sec-5"&gt;Write your application using your API&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;


&lt;p&gt;
You've gone through great lengths to create a minimal and polished API,
now it's finally time to use it by writing your Application on top of
it. Why, you might ask? Well, because Applications are the &lt;code&gt;human-facing interface&lt;/code&gt; to your data. Applications talk to humans, and only ever to
humans, because they choose a format that is difficult or impossible to
use to talk to other application. APIs on the other hand talk only to
applications using structured data, which is not the best format to
present to the user for most types of data.
&lt;/p&gt;
&lt;p&gt;
Say we want an application that will get the timeline of a given user
and display it on a webpage. This can be encoded in a simple way using
our API:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;dataP&lt;/span&gt; = twitter.statuses(&lt;span style="color: #A6E32D;"&gt;'notSorella'&lt;/span&gt;)

dataP.then(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;data&lt;/span&gt;) {
  &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;tweets&lt;/span&gt; = data.map(normaliseTweet)
  addAllTo(twitterContainer, renderTweetList(tweets))
}).done()
&lt;/pre&gt;


&lt;p&gt;
If we then are tasked with displaying the same set of Tweets on the
command line, we can just use the primitives, which are not
HTML-specific!
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;dataP&lt;/span&gt; = twitter.statuses(&lt;span style="color: #A6E32D;"&gt;'notSorella'&lt;/span&gt;)

&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;renderTweet&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;tweet&lt;/span&gt;) { 
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;'@'&lt;/span&gt; + tweet.user.name + &lt;span style="color: #A6E32D;"&gt;': '&lt;/span&gt; + tweet.text.plain
}

dataP.then(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;data&lt;/span&gt;) {
  &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;tweets&lt;/span&gt; = data.map(normaliseTweet)
  tweets.map(compose(print, renderTweet))
}).done()
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-6" class="outline-2"&gt;
&lt;h2 id="sec-6"&gt;Conclusion&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-6"&gt;


&lt;p&gt;
This is it, me dears. This is the key to write large applications, this
is the key to write extensible applications, and the key to write easily
maintainable applications: compositionality.
&lt;/p&gt;
&lt;p&gt;
You start with an idea, extract the key components of that idea
(primitives), provide combinators to compose ideas together, and only
then provide additional transformations for the user-facing interface.
&lt;/p&gt;
&lt;div id="footnotes"&gt;
&lt;h2 class="footnotes"&gt;Footnotes: &lt;/h2&gt;
&lt;div id="text-footnotes"&gt;
&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.1" href="#fnr.1"&gt;1&lt;/a&gt;&lt;/sup&gt; : I am, of course, referring to my own notion of How Applications
        Should Be Written™, which might be fairly arbitrary.
&lt;/p&gt;


&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.2" href="#fnr.2"&gt;2&lt;/a&gt;&lt;/sup&gt; : For a much better explanation of why we should value &lt;b&gt;simplicity&lt;/b&gt;
        over &lt;b&gt;easiness&lt;/b&gt;, Rich Hickey (the guy from Clojure) has a most
        awesome presentation on the topic, called
        &lt;a href="http://www.infoq.com/presentations/Simple-Made-Easy"&gt;Simple Made Easy&lt;/a&gt;.
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Trans people should not be allowed to pee!</title>
   <link href="http://killdream.github.com//blog/2013/03/trans-people-should-not-be-allowed-to-pee/index.html" />
   <updated>2013-03-23T00:00:00-07:00</updated>
   <id>http://killdream.github.com//blog/2013/03/trans-people-should-not-be-allowed-to-pee/trans-people-should-not-be-allowed-to-pee</id>
   <content type="html">&lt;p&gt;
Let's face it, trans people should not be allowed to use public
bathrooms for straight, cisgendered people. Trans people are
weird. Like, &lt;b&gt;really&lt;/b&gt; weird. And they do make normal, straight people
immensely uncomfortable. As such, I am forced to agree with John
Kavanagh, and reinforce his claim that &lt;a href="https://www.allout.org/en/actions/arizona"&gt;If you think someone is using a public bathroom that's the wrong gender for them, THEY SHOULD FACE THE JAIL!&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Now, unlike John, I hold a more extremist view on this subject. Why stop
at trans people? There are lots of other things that make normal,
straight people likewise uncomfortable. For example, this scenario:
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;You're a white, christian, straight man, making your way over to the
public bathroom in a STEM convention. You decide to use the urinals,
like any sane person would in this occasion. At this point, another
man (if we ever could call that&amp;hellip; &lt;b&gt;disgusting thing&lt;/b&gt; that), looks at
you and stops at the urinal at your side. He pulls out his dick, still
staring at you, making faces at you. HOLY MOTHER OF FUCKS HE'S MAKING
FACES AT YOU! You feel violated, wishing all this awkward moment will
end as soon as possible. You feel an urge to punch him, but decide not
to: that would cause a huge commotion, and what if people think &lt;b&gt;you&lt;/b&gt;
are gay too?! Oh, God forbid that!
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
There's no denying that this sort of thing should not happen to normal,
white, straight men. Life is already way too difficult as it is. And
these things, these feelings of being violated! Oh my dearest God, I can
not even think about these! As such, I propose we don't stop with just
potentially forbidding trans people from using public bathrooms, but
also homosexuals. Those disgusting, vile people!
&lt;/p&gt;
&lt;p&gt;
However, by taking such actions, the international community and the
"human" rights community might be upset at America&lt;sup&gt;&lt;a class="footref" name="fnr.1" href="#fn.1"&gt;1&lt;/a&gt;&lt;/sup&gt;. People might
say we are not "inclusive" for people. Or that we're not a truly "free"
country. Or even utter bullshit like "We're not making progress, but
regressing instead!" Those fools! The 19th century was clearly more
developed in manners and society values as this century. We truly valued
masculinity back then. And now? Now we have these&amp;hellip; oh I can't even
finish this phrase. It disgusts me so.
&lt;/p&gt;
&lt;p&gt;
None the less, we want the Americans who might support this kind of
things to think we're being "inclusive", so I propose that besides
forbidding those weird fucks to use public bathrooms, we should do so
under the pretence of "We want to make Americans not feel violated when
using a public place.". This would mean that the rules for using public
bathrooms would have to change to the following:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Public bathroom usage would be defined by gender &lt;b&gt;and&lt;/b&gt;
   sexual/romantic orientation. That is, you would only be able to use
   the public bathroom that fits your gender (with a distinction on
   cis/trans people), and your sexual orientation.

&lt;p&gt;   
   So, &lt;i&gt;homosexual cis-men&lt;/i&gt; would have to use a separate bathroom than
   an &lt;i&gt;heterosexual cis-men&lt;/i&gt;, which is only reasonable. A &lt;i&gt;homosexual    trans-men&lt;/i&gt; would not use the same public bathroom as a &lt;i&gt;homosexual    cis-men&lt;/i&gt;, even more if that trans people haven't fully completed the
   procedures for sex reassignment yet.
&lt;/p&gt;
&lt;p&gt;
   For &lt;i&gt;genderfluid&lt;/i&gt; people, this mean we'd need an additional division,
   since those fuckers might feel like a man one day and a woman the
   other day. Disgusting!
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;Public places, if providing public bathrooms, would be &lt;b&gt;required by    law&lt;/b&gt; to provide bathrooms for every combination of gender and
   sexual/romantic orientations. Since we don't have a fixed number of
   these, this could potentially grow to infinity.

&lt;p&gt;
   This would, of course, need to have a flaw in the law's writing such
   that a decent lawyer could use it to say "Oh, we're still trying to
   adapt to the changes, just give us a little more time." Of course, as
   long as the place has public bathrooms for &lt;i&gt;straight cis-men&lt;/i&gt; and
   &lt;i&gt;straight cis-women&lt;/i&gt;. If the place doesn't provide these, we should
   just fuck them up.
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;ul&gt;
&lt;li&gt;People who are reported as using the wrong public bathroom for their
   gender or sexual/orientation will face a penalty according to how
   weird they are. Thus, &lt;i&gt;straight cis&lt;/i&gt; people would just have to spend
   the night on the jail. Weird people would spend a lot more.
&lt;/li&gt;
&lt;/ul&gt;





&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;Conclusion&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
I really think this is the way America should go. To deny that people
who don't conform with the gender roles for their biological sex are
even people. This is a huge improvement from the 19th century, and I
believe perhaps even more than the Dark Age. It's likely to also be more
inclusive than the Nazi Germany, if we keep at it with all our
strength.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="https://www.allout.org/en/actions/arizona"&gt;Let's together make America a better place for &lt;i&gt;straight, white, cis-men&lt;/i&gt;, who are already way too bullied by their social position, and worse, threatened by this scourge of weird asses who insist in labelling themselves "human".&lt;/a&gt;
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;About this piece&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
I'm a genderfluid, asexual, panromantic person who was just way too
shocked with mister John Kavanagh's proposed law. If you don't get it,
this blog post is riddled with blatant sarcasm, and I'm just expressing
my support to the petition linked a few times throughout this post
(&lt;a href="https://www.allout.org/en/actions/arizona"&gt;https://www.allout.org/en/actions/arizona&lt;/a&gt;), by showing how absurd this
law really is :3
&lt;/p&gt;


&lt;div id="footnotes"&gt;
&lt;h2 class="footnotes"&gt;Footnotes: &lt;/h2&gt;
&lt;div id="text-footnotes"&gt;
&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.1" href="#fnr.1"&gt;1&lt;/a&gt;&lt;/sup&gt; : I don't even know why it's called human rights, they defend the
        rights of gay and trans people, who clearly are not human.
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>A Case for Prototypes</title>
   <link href="http://killdream.github.com//blog/2013/02/a-case-for-prototypes/index.html" />
   <updated>2013-02-28T00:00:00-08:00</updated>
   <id>http://killdream.github.com//blog/2013/02/a-case-for-prototypes/a-case-for-prototypes</id>
   <content type="html">&lt;p&gt;
Two days ago I started working on my own implementation of &lt;a href="http://en.wikipedia.org/wiki/QuickCheck"&gt;QuickCheck&lt;/a&gt;, since
none of the implementations I saw for JavaScript did quite what I wanted. So, I
sat down to outline the major goals of &lt;a href="http://github.com/killdream/claire"&gt;Claire&lt;/a&gt;:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Provide the means for property-based testing. For pure functionality.

&lt;/li&gt;
&lt;li&gt;Provide the means for random program generation. For complex,
    state-dependant functionality.

&lt;/li&gt;
&lt;li&gt;Provide core primitives and combinators for random-data generation. This
    way domain-specific generators can be easily derived by combining
    primitives.

&lt;/li&gt;
&lt;li&gt;Be a nice DSL for use in JavaScript.
&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
So I started drafting the initial API that would satisfy those goals (ignoring
program generation for now, since I decided to leave that for later), and
decided on two basic data types:
&lt;/p&gt;



&lt;pre class="src src-haskell"&gt;&lt;span style="color: #ACAE95;"&gt;-- | Describes a property and its specifications&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Property&lt;/span&gt;
  arguments    &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Gen&lt;/span&gt; a]
  classifiers  &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [(a&lt;span style="color: #729FCF;"&gt;...&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Maybe&lt;/span&gt; b)]
  implications &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [(a&lt;span style="color: #729FCF;"&gt;...&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Bool&lt;/span&gt;)]
  invariant    &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; (a&lt;span style="color: #729FCF;"&gt;...&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Bool&lt;/span&gt;)

  &lt;span style="color: #ACAE95;"&gt;-- | The DSL&lt;/span&gt;
  satisfy  &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;@&lt;/span&gt;&lt;span style="color: #FC951E;"&gt;Property&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;=&amp;gt;&lt;/span&gt; (a&lt;span style="color: #729FCF;"&gt;...&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Bool&lt;/span&gt;) &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Property&lt;/span&gt;
  classify &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;@&lt;/span&gt;&lt;span style="color: #FC951E;"&gt;Property&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;=&amp;gt;&lt;/span&gt; (a&lt;span style="color: #729FCF;"&gt;...&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Maybe&lt;/span&gt; b) &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Property&lt;/span&gt;
  given    &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;@&lt;/span&gt;&lt;span style="color: #FC951E;"&gt;Property&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;=&amp;gt;&lt;/span&gt; (a&lt;span style="color: #729FCF;"&gt;...&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Bool&lt;/span&gt;) &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Property&lt;/span&gt;


&lt;span style="color: #ACAE95;"&gt;-- | Describes a way of randomly generate a series of values&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Gen&lt;/span&gt; a
  size   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Int&lt;/span&gt;
  next   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;()&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; a

&lt;span style="color: #ACAE95;"&gt;-- | A semantic way of getting a Property than Property.make()&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;forAll&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Gen&lt;/span&gt; a&lt;span style="color: #729FCF;"&gt;...&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Property&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
I'll use these types as a basis for the rest of the article. If you don't know
Haskell's type annotation syntax, it suffices to say that &lt;code&gt;::&lt;/code&gt; describes the
type of the thing on the left, &lt;code&gt;[a]&lt;/code&gt; describes a list of &lt;code&gt;a&lt;/code&gt;'s, and &lt;code&gt;a -&amp;gt; b&lt;/code&gt;
describes a function that takes a value &lt;code&gt;a&lt;/code&gt;, and returns a value &lt;code&gt;b&lt;/code&gt;. I've
added &lt;code&gt;@a =&amp;gt; b&lt;/code&gt; for describing a function that, when applied to type &lt;code&gt;a&lt;/code&gt; (as
&lt;code&gt;this&lt;/code&gt;), yields a value of type &lt;code&gt;b&lt;/code&gt;. And the ellipsis to denote variadic
functions.
&lt;/p&gt;


&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;Maintaining purity&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
First of all, I wanted the DSLs of properties to be pure. That means that, even
though you'll be writing things like:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;sqrt_p&lt;/span&gt; = forAll( Int )
             .satisfy(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;n&lt;/span&gt;){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; Math.sqrt(n * n) == n })
&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;satisfy&lt;/code&gt; function should not change the &lt;code&gt;Property&lt;/code&gt; instance that it was
applied to. Instead, it should always return a new &lt;code&gt;Property&lt;/code&gt; instance. Thus,
when you write things like:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;all_positive_ints&lt;/span&gt; = forAll( Int )
                        .given(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;n&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; n &amp;gt; 0 })

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;sqrt_p&lt;/span&gt; = all_positive_ints
             .satisfy(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;n&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; Math.sqrt(n * n) == n })
&lt;/pre&gt;


&lt;p&gt;
Even though &lt;code&gt;satisfy&lt;/code&gt; modifies the &lt;code&gt;invariant&lt;/code&gt; field of a &lt;code&gt;Property&lt;/code&gt;,
&lt;code&gt;all_positive_ints&lt;/code&gt; and &lt;code&gt;sqrt_p&lt;/code&gt; should be two different data structures. It
could be obviously achieved by creating a new object, copying all fields of the
previous object in the new one, and changing the &lt;code&gt;invariant&lt;/code&gt; field, but
prototypes make that both dead simple and efficient (in terms of processing
power and memory usage).
&lt;/p&gt;
&lt;p&gt;
So, turns out the implementation for this is just a simple and straight-forward
one-line (the actual implementation is simpler due to both LiveScript's syntax
and the fact that Property inherits from Boo's &lt;code&gt;Base&lt;/code&gt; object):
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;derive&lt;/span&gt; = require(&lt;span style="color: #A6E32D;"&gt;'boo'&lt;/span&gt;).derive

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;Property&lt;/span&gt; = {
  &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(...) */&lt;/span&gt;
  &lt;span style="color: #67D9F0;"&gt;satisfy&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;f&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; derive(&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;, { invariant: f }) }
}
&lt;/pre&gt;


&lt;dl&gt;
&lt;dt&gt;note&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  So, &lt;code&gt;derive&lt;/code&gt; here is just a thin abstraction over &lt;code&gt;Object.create&lt;/code&gt; that
  creates a new &lt;code&gt;Object&lt;/code&gt; which delegates to another object (&lt;code&gt;this&lt;/code&gt;, in the case
  above), and extends that new object with the given properties.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;


&lt;p&gt;
If you inspect the values of &lt;code&gt;invariant&lt;/code&gt;, you'll get:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;all_positive_ints.invariant
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(Function) =&amp;gt; function (){&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;//                 &lt;/span&gt;&lt;span style="color: #75766A;"&gt;throw Error('unimplemented');&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;//               &lt;/span&gt;&lt;span style="color: #75766A;"&gt;}&lt;/span&gt;

sqrt_p.invariant
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(Function) =&amp;gt; function (n){&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;//                 &lt;/span&gt;&lt;span style="color: #75766A;"&gt;return Math.sqrt(n * n) === n;&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;//               &lt;/span&gt;&lt;span style="color: #75766A;"&gt;}&lt;/span&gt;

all_positive_ints.isPrototypeOf(sqrt_p)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(Boolean) =&amp;gt; true&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
So, this is a cheap trick, and not really new (I've used it on a parser
combinator library I was writing ages ago). But it's a cheap trick that gets
the work done extremely well, in a simple way (both to write and to reason
about) and still manages to be fairly efficient. We couldn't really ask for
more ;3
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;The real deal&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
Things start getting a lot more awesome when we take a look at how Generators
were modelled, specially when we take into account the combinators defined for
creating new Generators. Here's the deal:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;We want generators to follow the pure model Properties do.

&lt;/li&gt;
&lt;li&gt;We want combinators to be expressive, but still easy enough to write.
&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
I could have used functions, in which case those would be relatively easy to
achieve due to the higher-order facility in JavaScript, but a Generator
actually encodes more than just a way of generating the next value. It also
knows how to shrink a value it has generated, and how to display itself to the
user in a human-readable and concise way for debugging purposes.
&lt;/p&gt;
&lt;p&gt;
So, I was stuck with objects, and I still had to satisfy the two constraints
above. Well, meet the redemption: Prototypes. Prototypes are a really great fit
in this case because, even though all generators obey the basic &lt;code&gt;Gen a&lt;/code&gt;
structural type, they can be based in any kind of generator that the user may
come up with. Since we have prototypes rather than classes, it's entirely
possible to encode this complex and dynamic hierarchy as simple functions as
combinators. Yes, we get all we need from combinators, and still have
combinators as plain functions (that yield new Generators).
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-2-1" class="outline-3"&gt;
&lt;h3 id="sec-2-1"&gt;The `asGenerator' combinator&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-1"&gt;


&lt;p&gt;
So, the first and most basic generator is the &lt;code&gt;asGenerator&lt;/code&gt;. This is a function
that takes any value &lt;code&gt;a&lt;/code&gt;, and returns a Generator that will always yield that
value (or the result of applying a function, if &lt;code&gt;a&lt;/code&gt; is a function). On the
other hand, if the function receives a generator as value, it works like the
identity function. This allows us to lift a regular primitive value, like a
Boolean or a String, into a generator:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;True&lt;/span&gt; = asGenerator(&lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;)
True.next() &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; (Boolean) true&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
The core &lt;code&gt;Generator&lt;/code&gt; object inherits from Boo's &lt;code&gt;Base&lt;/code&gt; object, so it's rather
easy to write a function like &lt;code&gt;asGenerator&lt;/code&gt;, and in fact all other generators
take advantage of this sugar. The following is a slightly simplified version of
&lt;code&gt;asGenerator&lt;/code&gt;:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;asGenerator&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;a&lt;/span&gt;) {
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; generatorP(a)?   a
       : &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;otherwise */&lt;/span&gt;  Generator.derive({
                            &lt;span style="color: #67D9F0;"&gt;next&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; a }
                          })
}
&lt;/pre&gt;


&lt;p&gt;
In a class based language, specially those that are less expressive, like Java,
you would have to encode the &lt;code&gt;asGenerator&lt;/code&gt; function as a whole new Class,
making the implementation unnecessarily complicated.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-2" class="outline-3"&gt;
&lt;h3 id="sec-2-2"&gt;The `choice' and `frequency' combinators&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-2"&gt;


&lt;p&gt;
On top of the lifting generator, a clear second step is to provide people with
a way to alternatively select between different generators to create a new
value. These roles are satisfied by the &lt;code&gt;choice&lt;/code&gt; combinator, which takes
several generators and generates a value using one of them in an uniformly
distributed pseudo-random choice. And then there's the &lt;code&gt;frequency&lt;/code&gt; combinator,
which does almost the same as &lt;code&gt;choice&lt;/code&gt;, but uses a weighted distribution
instead.
&lt;/p&gt;
&lt;p&gt;
So, what's the big deal here? Ain't those other two cases that you could encode
as a class? Well, sure. You can always encode things using something in
computer science, but that doesn't mean it'll be easy, simple or
straight-forward. And I want the three of those characteristics.
&lt;/p&gt;
&lt;p&gt;
Using these combinators looks like this:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;vowels&lt;/span&gt; = choice(&lt;span style="color: #A6E32D;"&gt;'a'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'e'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'i'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'o'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'u'&lt;/span&gt;)
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;coins&lt;/span&gt; = frequency([3, &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;], [5, &lt;span style="color: #FA2573;"&gt;false&lt;/span&gt;])
&lt;/pre&gt;


&lt;p&gt;
Since both &lt;code&gt;choice&lt;/code&gt; and &lt;code&gt;frequency&lt;/code&gt; leverage &lt;code&gt;asGenerator&lt;/code&gt;, you can pass either
simple values or generators over to them. In fact, one of the nice things about
using objects as generators is that checking if something is a generator is
easy: you just check the conformance to the structural type &lt;code&gt;Gen a&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
This is a simplified implementation of &lt;code&gt;choice&lt;/code&gt; and &lt;code&gt;frequency&lt;/code&gt;:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;choice&lt;/span&gt;() {
  &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;gs&lt;/span&gt; = toArray(&lt;span style="color: #FA2573;"&gt;arguments&lt;/span&gt;).map(asGenerator)
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; Generator.derive({
           &lt;span style="color: #67D9F0;"&gt;next&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; pickOne(gs).next() }
         })
}

&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;frequency&lt;/span&gt;() {
  &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;gs&lt;/span&gt; = toArray(&lt;span style="color: #FA2573;"&gt;arguments&lt;/span&gt;).reduce(weightedConcat, [])
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; choice.apply(&lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;, gs)

  &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;weightedConcat&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;r&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;x&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; r.concat(replicate(x[0], x[1]))
  }
}
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-3" class="outline-3"&gt;
&lt;h3 id="sec-2-3"&gt;And finally, the `repeat' combinator&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-3"&gt;


&lt;p&gt;
Once we have all this in place, we can also generate lists of things, and this
is easily done by the &lt;code&gt;repeat&lt;/code&gt; combinator. In fact, the &lt;code&gt;List&lt;/code&gt; core generator
is a thin abstraction on the &lt;code&gt;repeat&lt;/code&gt; combinator, just so you don't have to
type &lt;code&gt;repeat(choice(...))&lt;/code&gt;. 
&lt;/p&gt;
&lt;p&gt;
Now, this is where the magic of prototypes truly shine. I didn't tell you until
now, but all generators need to maintain all of the properties of the previous
generator. All of them! This is because other generators might depend on those
properties (for example, &lt;code&gt;size&lt;/code&gt;). And in fact, the &lt;code&gt;size&lt;/code&gt; field plays a huge
and important role in the &lt;code&gt;repeat&lt;/code&gt; combinator, since it will only generate
lists of values up to that length!
&lt;/p&gt;
&lt;p&gt;
Now, enough talk, this is how the &lt;code&gt;repeat&lt;/code&gt; combinator looks for reals:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;numbers&lt;/span&gt; = repeat(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; randInt(0, 10) })
&lt;/pre&gt;


&lt;p&gt;
Pretty straight forward, huh? So is the simplified implementation:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;repeat&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;g&lt;/span&gt;) {
  &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; asGenerator(g).derive({
           &lt;span style="color: #67D9F0;"&gt;next&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() {
             &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;range&lt;/span&gt; = Array(randInt(0, &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.size)).join(0).split(0)
             &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; range.map(g.next.bind(g))
           }
         })
}
&lt;/pre&gt;


&lt;p&gt;
So, now we're taking any kind of &lt;code&gt;Generator&lt;/code&gt;, an instance, and making a new
kind of &lt;code&gt;Generator&lt;/code&gt; that efficiently shares all the properties defined in the
previous generator, but also defines its own properties. This is the true
beauty of prototypical inheritance: instances inheriting directly from
instances.
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;Conclusion&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
There's much more to &lt;a href="http://github.com/killdream/claire"&gt;Claire&lt;/a&gt; than what I've shown above, but those are out of
the scope of this blog post, which was to outline how you can take advantage of
prototypical inheritance to create awesome things, since you're already coding
in a language that provides it for you. Dynamic delegation chains are sweet for
lots of things, these are just some of them.
&lt;/p&gt;
&lt;p&gt;
Perhaps I might write a new blog post on some other aspect of Claire (for
instance, shrinking or program generation, when those are done). But I don't
know, I've got some other quite hot and sweet topics to talk about as well! :3
&lt;/p&gt;
&lt;p&gt;
Anyways, hope you guys can see now why inheriting directly from objects is a
Darn Good Thing™. And why I still think that &lt;code&gt;Function.prototype&lt;/code&gt; is an
anti-pattern in the language, which should be avoided (although it's undeniably
fast).
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;Acknowledgements&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;


&lt;p&gt;
Thanks to &lt;code&gt;Theo&lt;/code&gt; for pointing out that &lt;code&gt;abcde&lt;/code&gt; aren't vowels ;3
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Unfancy API Documentation</title>
   <link href="http://killdream.github.com//blog/2013/02/unfancy-documentation/index.html" />
   <updated>2013-02-23T00:00:00-08:00</updated>
   <id>http://killdream.github.com//blog/2013/02/unfancy-documentation/unfancy-documentation</id>
   <content type="html">&lt;p&gt;
I've been fiddling with different approaches to API documentation over
the last years, and found a sweet spot that actually fits my
needs. However I still faced a problem: there were no tools to support
my approach to documentation. There were lots of tools out there that
took different approaches for documentation, but I couldn't use any of
them because they were either monolithic or language-specific and not
that easy to bend my way.
&lt;/p&gt;
&lt;p&gt;
So, in this blog post I describe that approach better and my way of
trying to solve not just my problem, but alleviating the cost of coming
up with different approaches to documentation, which should probably
benefit more people (specially those who disagree with my dictatorial
rules :3)
&lt;/p&gt;


&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;An overview of the documentation landscape&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
I work with a few different languages in a daily basis. Sometimes I'll code in
Python, then hack up some stuff in Clojure, or invoke some clever computer
spirits with JavaScript spells. Still, most of my time these days is spent
working with LiveScript. At the end of the day, though, this context switching
between a dozen of different languages leads to a really, &lt;b&gt;really&lt;/b&gt; annoying
thing: they use completely different tools and environments.
&lt;/p&gt;
&lt;p&gt;
Some of those tooling is specific to a language because it needs to be specific
to a language, for example editor modes, or the runtime library, static
checkers and linters, etc. Others aren't really specific to a language, but
still, like editors, debugger interfaces, build tools or documentation
toolkits. Except that more often than not, people tend to make
non-language-specific tools &lt;i&gt;language specific&lt;/i&gt;, which kills the idea of
reusing a otherwise generic tool in different environments.
&lt;/p&gt;
&lt;p&gt;
I really mean it, reusing generic tools for different languages is a big
thing. And it'll be even more of a big thing as people start working in more
than one environment at the same time — front-end developers usually work with at
least three.
&lt;/p&gt;
&lt;p&gt;
I lied. Documentation isn't actually generic. There are plenty of
language-specific parts. For example, language A might have run-time macros,
whereas language B has a different concept of modules, and you want to capture
those differences in the documentation in order to help the user grasp the
meaning of your code quicker. However, these "language-specific" features are
more often than not based upon years-old programming language fundamental
building blocks, and those can be well generalised up to a point — at least, as
long as it concerns the programmers' understanding of what an API does, why it
does such, how it does such and how you should use it.
&lt;/p&gt;
&lt;p&gt;
And please, let's not forget that API documentation can be "consumed" and
visualised in lots of ways, using the same basic meta-data, whether it's
generalised or not. You could say, documentation is made of several steps, and
each of these steps produce something useful (or "consumable").
&lt;/p&gt;
&lt;p&gt;
Even with this possibility of generalising things, I've found that the majority
of the main-stream tools for documentation are language specific or totally
overlook the step part, or yet only lets you change how the basic meta-data is
visualised in a handful of ways. This is true for JSDoc, Docco, Marginalia,
PyDoc, etc. Tools that can give you the meta-data they gather from the source
code, like Doxygen, are often monolithic and generate this meta-data in
annoying formats, like XML. Tools that don't rely on the structure of the
language don't allow you to make that meta-information available by other
tools, and there isn't a "visualisation only" format (well, you could kind of
call Sphinx one, but then you're not really using meta-data).
&lt;/p&gt;
&lt;p&gt;
For example, if you have a project that uses Scala and Java, but you want to
present their API together because it &lt;b&gt;actually makes sense&lt;/b&gt;, you can't easily
do that. Or if you want to include the documentation directly on your project's
README because you have only one or two public API methods, you also can't do
that easily because there's no tool that just does: "hey, take this
documentation meta-data, and generate something I can use in a
Markdown-formatted README." You have to actually reinvent the wheel all the
time by parsing the whole source and generating the thing in the format you
want, or be forever tied to a particular tool by writing a plugin.
&lt;/p&gt;
&lt;p&gt;
You also can't say, "Oh, I want to parse the documentation in this language
using its structure, because that's easily to statically determine, but for
this other language I want a generic thing because it's all too dynamic and you
can only take guesses about what everything means. But I still want to display
them using the same format." If that thing happens, you're basically out of
luck.
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;Separating the concerns in documentation tools&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;



&lt;p&gt;
Most of this can be fixed by separating the documentation in some well-defined
steps, and creating the whole picture by mixing-and-matching tools that fit
those steps. Below is a list of concerns that could be addressed by any
documentation tool, but are at the same time independent of all other
concerns:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Analysing the entities that occur in a code-base (classes, modules, etc.)
&lt;/li&gt;
&lt;li&gt;Resolving types in the code by way of type-inference.
&lt;/li&gt;
&lt;li&gt;Resolving relationships between entities (multi-methods, inheritance,
     etc.)
&lt;/li&gt;
&lt;li&gt;Analysing or guessing contextual relationships or abstractions.
&lt;/li&gt;
&lt;li&gt;Merging documentations from different projects into one.
&lt;/li&gt;
&lt;li&gt;Searching a particular entity in a code-base.
&lt;/li&gt;
&lt;li&gt;Visualising documentation.
&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;  
This list is in no way authoritative, there might be plenty of concerns that
aren't listed here depending on the particular needs of a project. Or those
concerns might be too much for a particular project. But again, this just shows
how we could all benefit from separating all those concerns in tools that can
be easily mixed and matched to achieve a greater goal: a nice way of
documenting your APIs.
&lt;/p&gt;
&lt;p&gt;
So, I did say that we could separate a documentation tool in a fixed number of
steps. There are four steps in documenting an API, to be more precise:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;A developer annotates his code following certain rules. This helps
       analysis tools get a better sense of the code, and users of the API to
       see faster how to use or what's the intention of a particular part of
       the API.

&lt;/li&gt;
&lt;li&gt;A tool analyses and extracts meta-information about API
       entities. Entities are all the things that are visible or usable in some
       way by the end user. So, classes, modules and functions are examples of
       entities in most contexts, whereas syntactical constructions like `for`
       or `if` are not — but more on that later.

&lt;/li&gt;
&lt;li&gt;Meta-information including structure, relationships and annotations is
       put together in a single place, in some easy to parse and easy to use
       format.

&lt;/li&gt;
&lt;li&gt;A visualisation tool uses the previous meta-information to display the
       structure, relationships, annotations, usage examples and any other kind
       of information that might be associated with the entities in a way that
       makes sense &lt;b&gt;in the context of the visualisation tool&lt;/b&gt;.
&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;
The graph below shows one way the concerns could fit in the steps above:
&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/2013-02-23-doc-steps.png"  alt="http://dl.dropbox.com/u/4429200/blog/2013-02-23-doc-steps.png" /&gt;
&lt;/p&gt;
&lt;p&gt;
So, why would this work? Simple (despite what my failures at graphs would tell
you), the annotated source is the canonical reference for everything, it's what
tells us which &lt;b&gt;Entities&lt;/b&gt; we care about to handle in the next stages.
&lt;/p&gt;
&lt;p&gt;
Then, several tools analyse this annotated source from different angles, one
tool may extract entities using a simple structural analysis of the sources,
another can extract type annotations, another can make sense of annotation
meta-data in the comments, or here-docs if the language supports it. At the end
of the day, however, all of those tools are able to communicate between each
other and collaborate because they use &lt;b&gt;a standard format&lt;/b&gt;.
&lt;/p&gt;
&lt;p&gt;
Then, all of those (up until now) disjoint information about the entities in
your code base moves on to the merging stage. At this point, all of the entity
information that we gathered is put together as a single thing, and tools
collaborate between each other to resolve relationship graphs, they could also
provide better contextual organisations, solve links between entities,
automatically "suggest" similar or more abstract methods, load examples, etc.
&lt;/p&gt;
&lt;p&gt;
At last, all the information that was put together in the &lt;i&gt;merging&lt;/i&gt; phase
passes to the last phase, which is visualisation. Again, since everything uses
an standard format, the visualisation tools don't need to care about which
tools were ran, they just care about the information they were given. At this
stage, the information can be used to display the API documentation inline in
README files, if it is small enough, or as an interactive web application, or
even as a searchable resource within your text editor (Emacs has this).
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;Meet Dollphie, Calliope, Kalamoi and Papyr°&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
As an experimental implementation of the concept defined above, I quickly
hacked together these three tools (&lt;a href="http://github.com/killdream/kalamoi"&gt;Kalamoi&lt;/a&gt;, &lt;a href="http://github.com/killdream/papyr"&gt;Papyr°&lt;/a&gt;, &lt;a href="http://github.com/killdream/calliope"&gt;Calliope&lt;/a&gt;) over the course
of this week. I needed a generic documentation format since I want to use the
same toolkit for documenting all the languages I work with. But I also needed
to take into account the differences in each language's semantics and
fundamentals behind each kind of entity. And I wanted to use different kinds of
visualisation depending on the context, thus small modules with one or two
public functions would have all their API documentation in a &lt;code&gt;README.md&lt;/code&gt;,
whereas more complex ones would be displayed in an interactive web application.
&lt;/p&gt;
&lt;p&gt;
These tools are far from production ready (for example, there are no support
for class hierarchies right now, nor field visibilities, nor multi-methods),
and they need some serious refactoring (for example, &lt;code&gt;kalamoi&lt;/code&gt; does everything
from analysis to merging), but they're usable. &lt;code&gt;Calliope&lt;/code&gt; is an opinionated
wrapper over the other two tools for JavaScript projects that are already using
CommonJS packages, and it's a really thin wrapper.
&lt;/p&gt;
&lt;p&gt;
The basic idea of this toolkit revolves around a simple and generic structural
annotation format (Dollphie) to describe entities in a non-language-specific
way. This alone gets us around the problem of relying on a language's structure
in overly dynamic languages like JavaScript, and works as a fallback for
providing a common ground for languages that don't have specialised tools. Then
there's a parser and analyser (Kalamoi) for this format, which spits out a
standard documentation JSON with the entities meta-data, and finally the
visualiser (Papyr°) takes that JSON and displays to the user in an interactive
way.
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-3-1" class="outline-3"&gt;
&lt;h3 id="sec-3-1"&gt;The generic structural annotation&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-1"&gt;


&lt;p&gt;
Since most of my work is in a overtly dynamic language like JavaScript (granted
I use LiveScript now, but same semantics), parsing the source and trying to
make sense of it isn't always easy. Say you have the following:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;/// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Given an ID, sanitises it, otherwise generates an unique ID.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;id&lt;/span&gt; = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() { &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;guid&lt;/span&gt; = 0

                      &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;name&lt;/span&gt;) {
                               &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; name?    sanitise(name)
                               :      &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;_ */&lt;/span&gt;  (++guid).toString(16) }

                      &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Sanitises an ID&lt;/span&gt;
                      &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;sanitise&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;name&lt;/span&gt;) {
                        &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; name.replace(&lt;span style="color: #A6E32D;"&gt;/\s/&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'-'&lt;/span&gt;)
                                   .replace(&lt;span style="color: #A6E32D;"&gt;/\W/&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;''&lt;/span&gt;)
                                   .toLowerCase() }}()
&lt;/pre&gt;


&lt;p&gt;
Even though this use of the "Module Pattern" is way too common, it's not
obvious right away that the documentation refers to the &lt;i&gt;inner function&lt;/i&gt; rather
than the immediately invoked function expression that surrounds it. Or say you
have something like this:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;/// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Takes the first `n` items of a list.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;take&lt;/span&gt; = curry(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;n&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;xs&lt;/span&gt;) {
                   &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; xs.slice(0, n) })
&lt;/pre&gt;


&lt;p&gt;
Which is also fairly common (not just the currying, but decorating a function
in general), not just in JavaScript, but also in Python. This doesn't make the
&lt;i&gt;type&lt;/i&gt; of &lt;code&gt;take&lt;/code&gt; immediately obvious, unless you happen to have figured out the
type of &lt;code&gt;curry&lt;/code&gt; already, or if your documentation tool evaluates a certain
sub-set of JavaScript to figure out these things.
&lt;/p&gt;
&lt;p&gt;
So, you have to annotate your code anyways with types, but there might be cases
where your documentation tool will have some trouble figuring out the structure
of your code anyways. Not to say you'd need extremely language-specific tools
to extract this information. In the end I ditched this approach and decided for
a generalisation of all languages I worked mostly with (which at the time were
mainly JavaScript and Python), not just on the way entities are annotated, but
also in the way &lt;b&gt;structure&lt;/b&gt; itself is annotated.
&lt;/p&gt;
&lt;p&gt;
The documentation format I came up with (and evolved informally through the
course of this year) is heavily inspired by ReStructuredText and Markdown. The
basic idea is that you have sections with varying depth levels (nested sections
yay!), and each section can have a range of information belonging to it,
including other sections. This way, entities are just special kinds of
sections. The main difference from systems like &lt;a href="http://jashkenas.github.com/docco/"&gt;Docco&lt;/a&gt;, etc. is that there are
subtle annotations that make it possible to figure out the structure of the
code and extract meta-data concerning a particular entity.
&lt;/p&gt;
&lt;p&gt;
The whole &lt;a href="https://github.com/killdream/kalamoi/blob/master/src/parser.ls#L61"&gt;parser&lt;/a&gt; fits in under 100-loc of LiveScript code. You can get a
&lt;a href="https://github.com/killdream/kalamoi/blob/master/src/parser.ls#L61"&gt;feeling of the documentation format&lt;/a&gt; by reading Kalamoi, Papyr°, Calliope or &lt;a href="https://github.com/killdream/boo/blob/master/lib/boo.js"&gt;Boo&lt;/a&gt;
source code. Boo is written in JavaScript, the others are all
LiveScript. Adapting the parser to other languages can be done in three lines
of code, but until I refactor stuff this is still a pain:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;in parser.ls&lt;/span&gt;
exports.luaSyntax = boo.derive(baseSyntax, { comment: &lt;span style="color: #A6E32D;"&gt;'--'&lt;/span&gt; })

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;in index.ls&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;languageFor&lt;/span&gt; = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;extension&lt;/span&gt;){ ...
  &lt;span style="color: #C48DFF;"&gt;case&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;'.lua'&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;'Lua'&lt;/span&gt;

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;syntaxFor&lt;/span&gt; = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;language&lt;/span&gt;){ ...
  &lt;span style="color: #C48DFF;"&gt;case&lt;/span&gt;: &lt;span style="color: #A6E32D;"&gt;'lua'&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; parser.luaSyntax
&lt;/pre&gt;


&lt;p&gt;
You probably guessed the parser relies on single-line comments, and yes. That's
because the parser is line-based for simplicity. Basically, every line that
starts with &lt;code&gt;&amp;lt;comment-character&amp;gt;&lt;/code&gt; is treated as special meta-data for the
parser, and the rest is treated as code. Lines that start with a repetition of
the last &lt;code&gt;&amp;lt;comment-character&amp;gt;&lt;/code&gt; are treated as sections, and the number of
&lt;code&gt;&amp;lt;comment-character&amp;gt;&lt;/code&gt; defines the level of that section (just as Markdown
headings).
&lt;/p&gt;



&lt;pre class="src src-livescript"&gt;&lt;span style="color: #75766A;"&gt;## Module foo&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;# Description of `foo`.&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;### -- Internal functions&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;#### Function id&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;# Description of `id`&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;id&lt;/span&gt; = (x) -&amp;gt; x

&lt;span style="color: #75766A;"&gt;### -- Public functions&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;#### Function k&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;# Description of `k`&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;k&lt;/span&gt; = (x) -&amp;gt; (y) -&amp;gt; x
&lt;/pre&gt;


&lt;p&gt;
As you can probably guess, the special "kind" of section is defined by the
first &lt;code&gt;&amp;lt;word&amp;gt;&lt;/code&gt; after the comment character. A &lt;code&gt;&amp;lt;word&amp;gt;&lt;/code&gt; here is more in the
sense of Lisp, except the only thing that works as a word boundary is
white-space. Sections where the kind is &lt;code&gt;--&lt;/code&gt; are special headers, that have no
special entity meaning, but can be used to logically group things within a
source code to make it easier to navigate and understand the structure.
&lt;/p&gt;
&lt;p&gt;
There are only two other special constructs that can be used inside of a
section to declare special meta-data about an entity. The &lt;code&gt;&amp;lt;generic meta data&amp;gt;&lt;/code&gt;, in the form of &lt;code&gt;:keys: optional value&lt;/code&gt;, and the &lt;code&gt;&amp;lt;type signature&amp;gt;&lt;/code&gt;, in
the Haskell-veins of &lt;code&gt;:: type&lt;/code&gt;, although the format of the type signatures
themselves is not specified — not a job for this particular language:
&lt;/p&gt;



&lt;pre class="src src-livescript"&gt;&lt;span style="color: #75766A;"&gt;### Function id&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;# :internal, deprecated:&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;# The identity function.&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;#&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;# :: a -&amp;gt; a&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;id&lt;/span&gt; = (x) -&amp;gt; x
&lt;/pre&gt;


&lt;p&gt;
This all works well for well structured source code, and probably somewhat well
for pseudo-literate code.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-2" class="outline-3"&gt;
&lt;h3 id="sec-3-2"&gt;A format to exchange entity meta-data&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-2"&gt;


&lt;p&gt;
I said before that all the benefits of this approach derive from a standard and
simple format to exchange meta-data about entities. And there isn't anything
easier nowadays for this kind of structured data than JSON. It's available
everywhere, easy to parse, easy to write, and it usually maps nicely to lots of
language's core data structures (at least dynamic ones) since it uses Maps,
Vectors, Strings and Numbers as types, rather than a document tree .
&lt;/p&gt;
&lt;p&gt;
This format is devised from an overt generalisation of all entities, with basis
on what users of the API might be looking for when they browse through API
references. This includes ideas that are common in other documentation systems,
and kind of obvious, like an entity's name, its type or a description about
what it means:
&lt;/p&gt;



&lt;pre class="src src-haskell"&gt;&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;URI&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;=&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;

&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;EntityKind&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;=&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Data&lt;/span&gt;
                &lt;span style="color: #729FCF;"&gt;|&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Module&lt;/span&gt;
                &lt;span style="color: #729FCF;"&gt;|&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Function&lt;/span&gt;
                &lt;span style="color: #729FCF;"&gt;|&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Object&lt;/span&gt;
                &lt;span style="color: #729FCF;"&gt;|&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Type&lt;/span&gt;

&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Author&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;=&lt;/span&gt; { name    &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
              ; email   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
              ; website &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;
              }

&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;=&lt;/span&gt; { id         &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;URI&lt;/span&gt;  &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;An unique ID for the entity&lt;/span&gt;
              ; parent     &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;URI&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The entity that contains this entity&lt;/span&gt;
              ; kind       &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;EntityKind&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The type of the entity being represented&lt;/span&gt;
              ; name       &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The name of this entity&lt;/span&gt;
              ; signatures &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;String&lt;/span&gt;] &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The types of the entity&lt;/span&gt;
              ; text       &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;A text describing this entity&lt;/span&gt;
              ; code       &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The source code associated with this entity&lt;/span&gt;
              ; meta       &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; { &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;-&amp;gt;&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;|&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Bool&lt;/span&gt; } &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Additional metadata&lt;/span&gt;
              ; language   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The language of the source code&lt;/span&gt;
              ; line       &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Number&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Where the source code starts&lt;/span&gt;
              ; end&lt;span style="color: #729FCF;"&gt;-&lt;/span&gt;line   &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Number&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Where the source code ends&lt;/span&gt;
              ; file       &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The file where the entity appears&lt;/span&gt;
              ; copyright  &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Short copyright notice (if any)&lt;/span&gt;
              ; repository &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The canonical repository for the code&lt;/span&gt;
              ; authors    &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Author&lt;/span&gt;] &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Who contributed to the code&lt;/span&gt;
              ; licence    &lt;span style="color: #729FCF;"&gt;::&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;String&lt;/span&gt; &lt;span style="color: #75766A;"&gt;-- &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The terms in which the code is released&lt;/span&gt;
              }

&lt;span style="color: #C48DFF;"&gt;type&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Entities&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;=&lt;/span&gt; [&lt;span style="color: #FC951E;"&gt;Entity&lt;/span&gt;]
&lt;/pre&gt;


&lt;dl&gt;
&lt;dt&gt;Note&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Yes, I do realise it makes more sense to create a new data structure to store
  all information related to &lt;code&gt;code&lt;/code&gt;, I'll likely do it when refactoring these
  tools. I'll probably make &lt;code&gt;code&lt;/code&gt; a &lt;code&gt;List&lt;/code&gt; as well, so it can handle
  multi-methods defined in different parts of the code and such.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;


&lt;p&gt;
This, however does not cover several kinds of entities, specially entities that
have intrinsic relationships with others in a way that it changes their
behaviour, like Classes and SuperClasses/Mixins/Traits/Delegative &amp;amp;
Concatenative Prototypes, etc.. It could just well be encoded in the type annotation,
but then you lose the ability of using these relationships to convey additional
information for the user when visualising the documentation, and encoding it as
an additional but non-specified meta-data means that, while other tools can
easily make sense of the data, we don't have a common-ground for them to
collaborate with every other tool in this environment, so that's a no-go.
&lt;/p&gt;
&lt;p&gt;
As it current stands, these things would need to be devised on top of the
current standard and be added for the specification, so we don't have the risk
of conflicts between different notations, and tools that don't understand that
"extension" can just ignore the additional meta-data.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-3" class="outline-3"&gt;
&lt;h3 id="sec-3-3"&gt;Visualising entity meta-data&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-3"&gt;


&lt;p&gt;
Well, meta-data is good for applications to manipulate them, but not good for
the end user, thus we are left with the problem of "how do we visualise this
meta-data". Turns out, there might be many different cases where we want to do
different visualisations. The goal of Papyr° is to cover one of them: finding
some entity that fits the problem the user is trying to solve in a potentially
large and complex API, and discovering how to use that entity. In the current
form, Papyr° manages to achieve almost none of the goals outlined above,
although it is a starting point.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/papyro.png"  alt="http://dl.dropbox.com/u/4429200/blog/papyro.png" /&gt;
&lt;/p&gt;
&lt;p&gt;
First, helping the user find the right component to use for a certain problem
involves first knowing how the user searches for information about that
particular problem. In some cases it's easy to guess, for example, the user
might be looking for a stable sort, and as such he'll type "stable sort" in the
search box and expect to see classes, functions, procedures, methods, whatever
that will do the thing he wants. But perhaps the user isn't really looking for
a stable sort, perhaps in his case some other sorting algorithm will do the job
better or faster because he doesn't have the constraints he gave in the first
place. In this case, the system has to guess the intentions of the user from
the provided context, and suggest things that fit the context but provide
different constraints, or different benefits.
&lt;/p&gt;
&lt;p&gt;
This also kind of asks for a more expressive search. One where the user can
define his constraints better. For example, if he knows he's working with a
list of Elements, he might be looking for a sorting function that is efficient
for those, so he could provide a basic type, say: &lt;code&gt;sort :: [Element] -&amp;gt; [Element]&lt;/code&gt;, and the search engine would attempt to match sorting functions that
provide an approximate type to that. &lt;a href="http://www.haskell.org/hoogle/"&gt;Hoogle&lt;/a&gt; does this kind of approximate type
searches. Other constraints might involve platforms, mutability, etc. Ranking
the search results by favouring the ones that fit the constraints the user
provided better is also an interesting addition.
&lt;/p&gt;
&lt;p&gt;
Currently Papyr° does not provide automatic contextual hints for potentially
useful components, but this is definitely a feature I want to work on, and that
could perhaps be branched in other projects. So, for example, an analysis tool
could be ran on the entities meta-data earlier on to determine some possible
logical tags for each entity, which could be used by the visualisation tool for
relating different entities and ranking them. Approximate type searches are
something that are planned but not yet implemented, this is however something I
need to give a little more thought since the type signatures are not defined,
so it's not something as straight-forward as Hoogle's type search. The problem
is also more with analysing the user input and determining the types rather
than having different type annotations used by the entities, since you could
probably determine that by some meta-data or language.
&lt;/p&gt;
&lt;p&gt;
So, the second goal of Papyr° is helping the user to use a particular entity to
solve his or her problem. This is partially done by automatically loading
Markdown literate examples and displaying them alongside the source code and
other details for an entity. The literate examples are, however, static. There
could be some use in defining a format for interactive examples, or even
examples that would be ran by a particular engine when building the
documentation JSON, for the cases where fully interactive examples in the
browsers aren't possible.
&lt;/p&gt;
&lt;p&gt;
The only other visualisation format I plan on writing is Reedox, a minimal
visualisation format for overly small APIs (one or two functions), where it
doesn't make much sense to have the user load an entire new page to search for
something where the whole API fits in two paragraphs in your project's
README. And this is the case for quite a few packages on NPM, including many I
plan on writing in order to refactor the projects outlined above.
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;Conclusion and future works&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;


&lt;p&gt;
This is just the beginning, there's lots of stuff to do, not only in
standardisation, so we can get better processing and analysis tools to extract
the meta-data we need, but also in visualisation tools so we can actually use
that meta-data in a way that it's useful for the user.
&lt;/p&gt;
&lt;p&gt;
Again, none of the projects above is really finished, but they can work for you
right now, and the core interfaces are unlikely to change. Calliope will surely
have the same interface and it'll be updated to reflect any changes that might
happen in the other projects. It's also quite easy to integrate with your
existing NPM package, so you can give it a try :3
&lt;/p&gt;
&lt;p&gt;
None the less, I'll be actively working on these and other tools (testing and
building, mostly). More crazy stuff to come ;3
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>On semicolons, pseudo-technical and subjective arguments, revisited</title>
   <link href="http://killdream.github.com//blog/2012/04/on-semicolons-and-subjective-discussions-revisited/index.html" />
   <updated>2012-04-16T00:00:00-07:00</updated>
   <id>http://killdream.github.com//blog/2012/04/on-semicolons-and-subjective-discussions-revisited/on-semicolons-and-subjective-discussions-revisited</id>
   <content type="html">&lt;p&gt;
The JavaScript community is, indeed, very mature — just like any other
programming community. We always find time to properly and politely (and
heatedly!) discuss the most important kinds of topics: like whether to
use &lt;a href="https://github.com/twitter/bootstrap/issues/3057"&gt;semicolons or not&lt;/a&gt;, or whether to use &lt;a href="http://www.emacswiki.org/emacs/SmartTabs"&gt;tabs or spaces&lt;/a&gt;. Or yet, whether
to use &lt;a href="http://en.wikipedia.org/wiki/Editor_war"&gt;Emacs or Vi{,m}&lt;/a&gt;. Of course, everyone knows you should use &lt;a href="http://www.gnu.org/software/emacs/"&gt;Emacs&lt;/a&gt;,
so I don't even know why those discussions surface.
&lt;/p&gt;
&lt;p&gt;
In any case, I decided to write a post to shed some light on the subject
&lt;del&gt;and let the flames begin&lt;/del&gt;. I tried to just shake this whole discussion
away, because it started out too subjective&amp;hellip; but I can't. This is an
important post. &lt;a href="http://xkcd.com/386/"&gt;Someone is &lt;b&gt;wrong&lt;/b&gt; on the internet!&lt;/a&gt;
&lt;/p&gt;


&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;A bit of history&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
Discussions on the rules of &lt;i&gt;Automatic Semicolon Insertion&lt;/i&gt; (henceforth &lt;i&gt;ASI&lt;/i&gt;)
aren't a new thing in the community. In fact, we've been seeing these for as
long as the community exists — well, perhaps not as much, but definitely the
discussions did grow as the community as a whole grew.
&lt;/p&gt;
&lt;p&gt;
On the one side, there are people who argue that &lt;i&gt;ASI&lt;/i&gt; is harmful, and you
should be ashamed of using such a excuse for "poor coding style". Of course,
the qualities of a coding style are, usually, extremely subjective, and make
absolute no sense taken without context — that is, without stating what
concerns the particular coding style chosen tries to address. Other arguments
involve blatant (and sometimes personal) attacks on people who don't use
semicolons in their code, misconceptions about when &lt;i&gt;ASI&lt;/i&gt; applies, and things
like "we've told ASI is harmful, so it must be!" or "JS is not a semicolon-less
language" — I wonder if they would also consider Python and Haskell as
non-semicolon-less languages, since they also have semicolons and ASI (albeit
much more significant line breaks and white space handling).
&lt;/p&gt;
&lt;p&gt;
On the other side, there are people who argue that semicolons are harmful, and
we're much better off without them. Arguments against semicolons vary from a
simple "Yes, yes we can!", passing through the "But I use other languages that
don't require semicolons!", to the "Omitting semicolons make errors more
apparent by transforming them in non-usual things" — needless to say those are
not the actual quotes, but they are close enough to the crux of the arguments.
&lt;/p&gt;
&lt;p&gt;
In this post, despite the hints for flamewar here and there, I'll try to lay
out the discussion from a not-so-biased standpoint. I am on the &lt;i&gt;Yay ASI!&lt;/i&gt;,
side, though, so you might see some bias here and there — I'll try to make
those as overt as possible, so you can just laugh it off (or rage) and move
on.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;What is &lt;i&gt;ASI&lt;/i&gt;?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
We can't start this whole discussion without first clarifying what we're
talking about. That's why, we first need to define what in the Lord's name
&lt;i&gt;ASI&lt;/i&gt; is.
&lt;/p&gt;
&lt;p&gt;
Let's start with the &lt;a href="http://es5.github.com/#x7.9"&gt;specification&lt;/a&gt; definition of this whole mess:
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;Certain ECMAScript statements (empty statement, variable statement,
expression statement, &lt;code&gt;do-while&lt;/code&gt; statement, &lt;code&gt;continue&lt;/code&gt; statement, &lt;code&gt;break&lt;/code&gt;
statement, &lt;code&gt;return&lt;/code&gt; statement, and &lt;code&gt;throw&lt;/code&gt; statement) must be terminated with
semicolons. Such semicolons may always appear explicitly in the source
text. For convenience, however, such semicolons may be omitted from the
source text in certain situations. These situations are described by saying
that semicolons are automatically inserted into the source code token stream
in those situations.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
The specification then goes on to define all of the rules by which semicolon
insertion takes place.
&lt;/p&gt;
&lt;p&gt;
If you still don't grasp what &lt;i&gt;ASI&lt;/i&gt; is after reading this part of the
specification, don't worry, most people don't. We could summarise it as: "You
need semicolons to end the statements cited above, but for convenience you can
omit them in some situations, if you want to."
&lt;/p&gt;
&lt;p&gt;
As &lt;a href="http://brendaneich.com/2012/04/the-infernal-semicolon/"&gt;Brendan Eich&lt;/a&gt; noted in his post, &lt;i&gt;ASI&lt;/i&gt; is an syntactic error-correction
procedure, that got implemented in JavaScript engine parsers and then made it
into the specification. This means that, technically speaking, all code written
without semicolons has syntactic errors in them. There's no denying it, because
that's how the specification describes these procedures.
&lt;/p&gt;
&lt;p&gt;
Practically speaking, though, the parsing rules and &lt;i&gt;ASI&lt;/i&gt; rules — should we
even call it that, since parsers don't need to actually add that token, — for
the ECMAScript language are pretty deterministic, and ambiguities in terms of
&lt;i&gt;Automatic Semicolon Insertion&lt;/i&gt; can be resolved in a single or double look-ahead
— if we ignore comments and horizontal white space.
&lt;/p&gt;
&lt;p&gt;
In fact, if we looked at the practical matter of the subject, we could
categorise famous languages' semicolon insertion rules in the following groups:
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;White-space strict (aka Statements ends as early as possible)&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
    With &lt;a href="http://www.python.org/"&gt;Python&lt;/a&gt; as the most known example of this particular style. In a
    language where "statements ends as early as possible", you can kind of
    define the language's statements' (or expressions) rules as the following:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;&lt;span style="color: #67D9F0;"&gt;&amp;lt;statement&amp;gt;&lt;/span&gt;        &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; ( lots of junk ) &lt;span style="color: #729FCF;"&gt;&amp;lt;end-of-statement&amp;gt;&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;&amp;lt;end-of-statement&amp;gt;&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; NEWLINE &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;";"&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
    Which, at a first glance, might look quite easy to remember, right?
    Unfortunately, in real world code, there are quite some more intricacies
    regarding these rules. Sticking with Python, since you want a little more
    expressiveness on how to lay out your long statements, Python offers you
    optional implicit and explicit statement continuations. So, a simple
    example with a string spanning multiple lines (and yes, I am aware of the
    additional constructs for multi-line strings in the language), would look
    like the following:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;foo = &lt;span style="color: #A6E32D;"&gt;"a string"&lt;/span&gt;    \
    + &lt;span style="color: #A6E32D;"&gt;"in multiple"&lt;/span&gt; \
    + &lt;span style="color: #A6E32D;"&gt;"lines."&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
    Notice how each line has to be terminated with an explicit
    continuation. Worse! If your editor can't highlight invisible characters,
    you risk running into the following error:
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ python test.py
  File &lt;span style="color: #A6E32D;"&gt;"test.py"&lt;/span&gt;, line 1
     foo = &lt;span style="color: #A6E32D;"&gt;"a line"&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
                      ^
SyntaxError: unexpected character after line continuation character.
&lt;/pre&gt;


&lt;p&gt;    
    An alternative to explicit continuations are implicit line joins, which
    happen on parenthesised expressions:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;foo = ( &lt;span style="color: #A6E32D;"&gt;"a string"&lt;/span&gt;
        &lt;span style="color: #A6E32D;"&gt;"in multiple"&lt;/span&gt;
        &lt;span style="color: #A6E32D;"&gt;"lines."&lt;/span&gt; )
&lt;/pre&gt;


&lt;p&gt;    
    Note that, in Python's particular case, the concatenation operator isn't
    necessary when two chunks of strings are separated by just
    white-space. This style saves you from worrying about whether there's
    white-space after your explicit continuations, but then your simple
    &lt;code&gt;&amp;lt;end-of-statement&amp;gt;&lt;/code&gt; rules stop being just &lt;code&gt;NEWLINE | “;”&lt;/code&gt;.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;dl&gt;
&lt;dt&gt;Non-white-space strict (aka If we can't parse, insert a damn semicolon!)&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
    ECMAScript is actually the only language I know who fits this bill —
    Haskell having even more complex statement rules, but managing to be
    overtly white-space strict.
&lt;/p&gt;
&lt;p&gt;
    In ECMAScript, your statements &lt;b&gt;always&lt;/b&gt; ends up with a semicolon. Whether
    you need to explicitly spell one in your source code, though, is another
    matter entirely. In fact, you can write large JavaScript applications
    without needing to write any semicolon in your source code at all,
    (comments excluded), as long as you stay away from old-style &lt;code&gt;for&lt;/code&gt; loops.
&lt;/p&gt;
&lt;p&gt;
    In JavaScript, the same example above would look like this:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;var foo = &lt;span style="color: #A6E32D;"&gt;"a string"&lt;/span&gt;
        + &lt;span style="color: #A6E32D;"&gt;"in multiple"&lt;/span&gt;
        + &lt;span style="color: #A6E32D;"&gt;"lines."&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
    ECMAScript's handling of statement continuation, imho, feels much more
    natural to read and write than Python's one. Therefore, they are, of
    course, &lt;b&gt;TEH BESTEST&lt;/b&gt; — if my feelings would constitute actual arguments,
    sadly they do not.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;How &lt;i&gt;ASI&lt;/i&gt; actually works?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
If you've been reading the comments from this whole &lt;del&gt;flamewar&lt;/del&gt; discussion,
you'll see that many people describe &lt;i&gt;ASI&lt;/i&gt; in JavaScript as a &lt;a href="https://github.com/twitter/bootstrap/issues/3057#issuecomment-5138012"&gt;&lt;b&gt;guessing game&lt;/b&gt;&lt;/a&gt;. They couldn't be further from the truth. In fact, there's no guessing at
all, everything is extremely deterministic from the grammar of the language
itself.
&lt;/p&gt;
&lt;p&gt;
And what's more, to check if an statement ends, a parser would need to use at
most 2 look-aheads, if we don't consider comments and white-space. So, let's
first define what the usual rule for ending a statement in JavaScript is:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;&lt;span style="color: #67D9F0;"&gt;&amp;lt;end-of-statement&amp;gt;&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;";"&lt;/span&gt;
                     &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;horizontal-space&amp;gt;&lt;/span&gt;* NEWLINE blank*
                       (and STATEMENT_ENDED? (not &lt;span style="color: #729FCF;"&gt;&amp;lt;continuation&amp;gt;&lt;/span&gt;))
&lt;span style="color: #67D9F0;"&gt;&amp;lt;horizontal-space&amp;gt;&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; SPACE &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; TAB &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;comment&amp;gt;&lt;/span&gt;
&lt;span style="color: #67D9F0;"&gt;&amp;lt;continuation&amp;gt;&lt;/span&gt;     &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"("&lt;/span&gt; &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"["&lt;/span&gt; &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;infix-operator&amp;gt;&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
Well, actually, it's not as simple as that. You have a few special cases in the
language to handle prefix operators and a few others, like the &lt;code&gt;return&lt;/code&gt;
statement, which the specification calls &lt;code&gt;restricted productions&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
For prefix statements, it just suffices to say that they &lt;b&gt;require&lt;/b&gt; an argument
succeeding then, and so can't end earlier due to a line break. Thus, the
following is valid JavaScript:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;a&lt;/span&gt; = 1
~
a
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; -2&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
In the case of &lt;code&gt;restricted productions&lt;/code&gt;, however, we have the "awesome" &lt;code&gt;&amp;lt;no line-break here&amp;gt;&lt;/code&gt; restriction in the grammar itself. Which means that, while
these productions &lt;b&gt;may&lt;/b&gt; have something succeeding the token, they &lt;b&gt;do not&lt;/b&gt;
require that such a case happens, and as such, a line break would indicate that
we want to end the statement early. A good example of &lt;code&gt;restricted productions&lt;/code&gt;,
and that is widely (and mistakenly&lt;sup&gt;&lt;a class="footref" name="fnr.1" href="#fn.1"&gt;1&lt;/a&gt;&lt;/sup&gt;) used to indicate how you should
always end your statements with a semicolon, is the &lt;code&gt;return&lt;/code&gt; statement, which
accepts an &lt;b&gt;optional&lt;/b&gt; return value:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;&lt;span style="color: #67D9F0;"&gt;&amp;lt;return-stmt&amp;gt;&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"return"&lt;/span&gt; [no line break here] &lt;span style="color: #729FCF;"&gt;&amp;lt;expression&amp;gt;&lt;/span&gt;
&lt;/pre&gt;



&lt;pre class="src src-js"&gt;(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; 1 })()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 1&lt;/span&gt;

;(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt;
              2 }()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; undefined&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
Of course, inserting a semicolon after &lt;code&gt;2&lt;/code&gt; in the second example won't prevent
the &lt;code&gt;return&lt;/code&gt; statement from always returning &lt;code&gt;undefined&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
For more in-depth articles on the intricacies of &lt;i&gt;ASI&lt;/i&gt; in JavaScript, please
refer to the awesome people that have written about it &lt;a href="http://inimino.org/~inimino/blog/javascript_semicolons"&gt;over&lt;/a&gt; and &lt;a href="http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding"&gt;over&lt;/a&gt; and over
again. They'll probably dive into more practical (and non-grammar-ish) details
than I have here. Inimino's post is particularly awesome.
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;Is &lt;i&gt;ASI&lt;/i&gt; safe?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;


&lt;p&gt;
We've seen that all of the &lt;i&gt;ASI&lt;/i&gt; rules are pretty deterministic, and they don't
rely on any kind of &lt;a href="http://tvtropes.org/pmwiki/pmwiki.php/Main/BlackMagic"&gt;Black Magic&lt;/a&gt; from hell that only parsers and parser-writers
know. They are also pretty manageable for a human brain to keep on his head
while reading the code — or at least, I consider JavaScript's ASI rules,
despite all of the complexity, in the same level of cognitive overhead as
Python's, if only because it &lt;b&gt;feels&lt;/b&gt; more natural to me.
&lt;/p&gt;
&lt;p&gt;
But do all of the engines implement it properly? Is it safe? Will my code
suddenly break?!  Well, if you've been &lt;a href="https://github.com/twitter/bootstrap/issues/3057#issuecomment-5135562"&gt;blindly following the Cult of Crockford&lt;/a&gt;,
you might have been lead to believe that the answer to all of those questions
would be: &lt;b&gt;no&lt;/b&gt;, &lt;b&gt;NO!&lt;/b&gt;, &lt;b&gt;BET THE HELL IT WILL&lt;/b&gt;.
&lt;/p&gt;
&lt;p&gt;
Of course, you should apply the &lt;code&gt;not&lt;/code&gt; predicate from higher-order logic to all
of those answers to get the actual, useful, answers. So, addressing the
concerns in order:
&lt;/p&gt;

&lt;/div&gt;

&lt;div id="outline-container-4-1" class="outline-3"&gt;
&lt;h3 id="sec-4-1"&gt;Do all of the engines implement it properly?&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-4-1"&gt;


&lt;p&gt; 
Well, as &lt;b&gt;inimino&lt;/b&gt; says in his post about &lt;a href="http://inimino.org/~inimino/blog/javascript_semicolons"&gt;JavaScript semicolons&lt;/a&gt;, there's no
reason to fear any incompatible behaviour between browsers in regards to the
"feature" (or misfeature, the definitions vary in the community). I have not
been able to verify this claim with empiric proofs yet to date, but from my
experiments in code, it seems to hold perfectly valid.
&lt;/p&gt;
&lt;p&gt;
If any browser implementer would like to clarify the matter, it would be an
interesting (and welcome) addition, indeed.
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;Another misconception is that bugs in browser JavaScript engines mean that
using semicolons everywhere is safer, and will protect the developer from
compatibility issues between browsers. This is simply not the case. All
extant browsers implement the specification correctly with regard to ASI, and
any bugs that may have existed are long since lost in the mists of early Web
history. There is no reason to be concerned about browser compatibility in
regard to semicolon insertion: all browsers implement the same rules and they
are the rules given by the spec and explained above.
&lt;/p&gt;
&lt;/blockquote&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4-2" class="outline-3"&gt;
&lt;h3 id="sec-4-2"&gt;Is it safe?&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-4-2"&gt;


&lt;p&gt;
No. I mean yes. I mean, it depends on what you mean by safe. From a technical
stand-point, it is perfectly safe — after all, it &lt;b&gt;is&lt;/b&gt; deterministic. You just
need to know the rules.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4-3" class="outline-3"&gt;
&lt;h3 id="sec-4-3"&gt;Will my code suddenly break?!&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-4-3"&gt;


&lt;p&gt;
As &lt;a href="http://twitter.com/littlecalculist"&gt;David Herman&lt;/a&gt;, a TC39 member, kindly enough clarified &lt;a href="https://twitter.com/#!/littlecalculist/status/191549755413889024"&gt;TC39 won't just break working code for no good reason&lt;/a&gt;. All the more when there's such a large body of
working code relying on this particular feature all the way around.
&lt;/p&gt;
&lt;p&gt;
Brendan Eich also discussed the issue with compatibility with older versions of
ECMAScript and legacy code as they move forward to extend the language, several
times (in his blog, in es-discuss, in twitter, in&amp;hellip;).
&lt;/p&gt;
&lt;p&gt;
It can all be summed up as: "if something is working and in use in the
JavaScript community, we can't just break it."
&lt;/p&gt;
&lt;p&gt;
So, no, your code will not suddenly break if you start omitting semicolons here
and there. Now, I can't guarantee people won't want to break &lt;b&gt;you&lt;/b&gt;.
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5" class="outline-2"&gt;
&lt;h2 id="sec-5"&gt;Reasons to use or not ASI&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;


&lt;p&gt;
So, this all said, there are any actual arguments in favour of omitting
semicolons from your code, aside from the "well, duh, we can!" non-argument
argument? Turns out, there are a few, but they're all overtly subjective, and
not in any sense technical:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Consistency! I write code in other languages that use no semicolons, so
   avoiding them in JavaScript makes it easier for me to switch back and forth,
   without inserting them in the wrong places, in the wrong languages.

&lt;/li&gt;
&lt;li&gt;Semicolons are not necessary, thus removing them promotes them from ordinary
   statement terminators/separators to special tokens that should be present in
   certain situations. It's expected that this change in the role of semicolons
   would highlight certain kinds of bugs, by making lacks of semicolons in
   places where they matter more apparent.

&lt;/li&gt;
&lt;li&gt;Removing semicolons reduces the overall noise of a source code, granted the
   indication given by semicolons is duplicated by line breaks and indentation,
   this means that we don't have extraneous and needless symbols to distract
   you from the actual code.
&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;
Of course, there are reasons for not relying on ASI, and the valid ones are
also overtly subjective, and not in any sense technical:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Consistency! I write code in other languages that use no ASI, so putting
   them everywhere in JavaScript makes it easier for me to switch back and
   forth, without omitting them in the wrong places, in the wrong languages.

&lt;/li&gt;
&lt;li&gt;Avoiding semicolons means that you have a higher cognitive load, and you
   must always scan much more of the source code than you should, just to be
   sure that the next line doesn't imply a continuation.

&lt;/li&gt;
&lt;li&gt;I use tools that don't support ASI (like JSMin, or JSLint).
&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;   
Okay, the last one of those is not a subjective reason not to rely on ASI, but
is not a technical reason on why &lt;b&gt;other&lt;/b&gt; people should do the same. After all,
you're just relying on a broken tool.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-6" class="outline-2"&gt;
&lt;h2 id="sec-6"&gt;Conclusion&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-6"&gt;


&lt;p&gt;
You should omit all semicolons from all your JavaScript code. Semicolons
everywhere are &lt;b&gt;insanely stupid code&lt;/b&gt;, and I'm not going to change my coding
style because some punks dislike it&amp;hellip; wait, that wasn't really the topic in
discussion. Well, whatever.
&lt;/p&gt;
&lt;div id="footnotes"&gt;
&lt;h2 class="footnotes"&gt;Footnotes: &lt;/h2&gt;
&lt;div id="text-footnotes"&gt;
&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.1" href="#fnr.1"&gt;1&lt;/a&gt;&lt;/sup&gt; : Citation needed.
&lt;/p&gt;




&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Yay for Sugary JavaScript OO</title>
   <link href="http://killdream.github.com//blog/2011/11/for-sugary-object-oriented-js/index.html" />
   <updated>2011-11-19T00:00:00-08:00</updated>
   <id>http://killdream.github.com//blog/2011/11/for-sugary-object-oriented-js/for-sugary-object-oriented-js</id>
   <content type="html">&lt;p&gt;
So, &lt;a href="http://killdream.github.com/blog/2011/10/understanding-javascript-oop/index.html"&gt;JavaScript is an object oriented language&lt;/a&gt; and all that, we have
discussed it previously to length, and I have quite expressed how
confusing and rough all the primitives the language provides are when it
comes to dealing with object composition and behaviour sharing — which
is just no good for a prototypical language.
&lt;/p&gt;
&lt;p&gt;
Well, the good thing is that we can provide our own abstractions for
this overtly dynamic object orientation semantics through libraries,
which is what I have been doing for a while. And now that &lt;a href="https://github.com/killdream/boo"&gt;Boo&lt;/a&gt; has gained
some shape and use, it's time to release it officially.
&lt;/p&gt;


&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;Why? Oh god WHY?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
So, what's this &lt;code&gt;Boo&lt;/code&gt; thing I'm talking about?
&lt;/p&gt;
&lt;p&gt;
Well, my dearest friend, &lt;code&gt;Boo&lt;/code&gt; is my attempt at bringing some sugar and
sort-of declarative syntax for defining objects in JavaScript, which
makes the structuring of programs easier to follow and reason about.
&lt;/p&gt;
&lt;p&gt;
So, instead of the ugly, factory-ish approach:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;Animal&lt;/span&gt;() { &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;... */&lt;/span&gt; }
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;Cat&lt;/span&gt;()    { &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;... */&lt;/span&gt; }
Cat.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt; = Object.create(Animal.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;)
Cat.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;.say = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() { &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;... */&lt;/span&gt; }
&lt;/pre&gt;


&lt;p&gt;
You can have a sort-of declarative one that works entirely around
objects:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;Base&lt;/span&gt;   = boo.Base
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;Animal&lt;/span&gt; = Base.derive({ &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Animal's properties here */&lt;/span&gt; })
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;Cat&lt;/span&gt;    = Animal.derive({ &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Cat's properties here */&lt;/span&gt; })
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;What Boo provides?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
Boo is structured in three layers: mixins, inheritance and
syntactical-sugar. Each of these serve to a particular purpose, and build
upon the previous one — although you could say that the concepts
themselves are orthogonal, which makes them composable.
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-2-1" class="outline-3"&gt;
&lt;h3 id="sec-2-1"&gt;Mixins&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-1"&gt;


&lt;p&gt;
On the basic level, &lt;code&gt;Boo&lt;/code&gt; provides the developer with mixins and
data-objects — which are a specialised kind of mixins.
&lt;/p&gt;
&lt;p&gt;
Mixins are parent-less objects that can be included in any object, at
any time. Data-objects do the same thing, but they're also a factory for
yielding the objects that will be included, which makes them a neat
thing for default objects and such.
&lt;/p&gt;
&lt;p&gt;
The primitives &lt;code&gt;extend&lt;/code&gt; and &lt;code&gt;merge&lt;/code&gt; account for all of these:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;ring_data&lt;/span&gt; = {
  items: [], max: 3,

  &lt;span style="color: #67D9F0;"&gt;toData&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; { items: [], max: &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.max }}
}

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;ring&lt;/span&gt; = {
  &lt;span style="color: #67D9F0;"&gt;push&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;item&lt;/span&gt;) {
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.unshift(item)
    &lt;span style="color: #C48DFF;"&gt;if&lt;/span&gt; (&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.length &amp;gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.max)
      &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.pop() }
}

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;spells&lt;/span&gt; = boo.merge(ring, ring_data, {
  max: 2
})

spells.push(&lt;span style="color: #A6E32D;"&gt;'Watera'&lt;/span&gt;)
spells.push(&lt;span style="color: #A6E32D;"&gt;'Blizarra'&lt;/span&gt;)
spells.push(&lt;span style="color: #A6E32D;"&gt;'Firaga'&lt;/span&gt;)
spells.items
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; ['Firaga', 'Blizarra']&lt;/span&gt;
ring_data.items
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; []&lt;/span&gt;

boo.extend(spells, [ring_data])
spells.items
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; []&lt;/span&gt;
spells.max
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-2" class="outline-3"&gt;
&lt;h3 id="sec-2-2"&gt;Inheritance&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-2"&gt;


&lt;p&gt;
For inheritance, &lt;code&gt;Boo&lt;/code&gt; just defines a thin layer over the standard
&lt;code&gt;Object.create&lt;/code&gt; method, such that it accepts mixins rather than property
descriptors — which are way too verbose to be practical.
&lt;/p&gt;
&lt;p&gt;
The primitive &lt;code&gt;derive&lt;/code&gt; is used for coupling inheritance and extension:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;Collection&lt;/span&gt; = {
  &lt;span style="color: #67D9F0;"&gt;pop&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(){
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.pop() },

  &lt;span style="color: #67D9F0;"&gt;push&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;item&lt;/span&gt;){
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.push(item) },

  &lt;span style="color: #67D9F0;"&gt;each&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;fn&lt;/span&gt;) {
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.forEach(fn) },

  &lt;span style="color: #67D9F0;"&gt;map&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;fn&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items = &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.map(fn) }
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Clones the `collection' behaviours and extends the clone with the&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;`ring' behaviours&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;RingCollection&lt;/span&gt; = boo.derive(Collection, ring)
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;spells&lt;/span&gt;         = boo.derive(RingCollection, ring_data)

RingCollection.isPrototypeOf(spells)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; true&lt;/span&gt;

spells.push(&lt;span style="color: #A6E32D;"&gt;'Agi'&lt;/span&gt;)
spells.push(&lt;span style="color: #A6E32D;"&gt;'Dia'&lt;/span&gt;)
spells.push(&lt;span style="color: #A6E32D;"&gt;'Patra'&lt;/span&gt;)
spells.push(&lt;span style="color: #A6E32D;"&gt;'Recarm'&lt;/span&gt;)
spells.map(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;spell&lt;/span&gt;){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; spell.toUpperCase() })
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; ['RECARM', 'PATRA', 'DIA']&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-3" class="outline-3"&gt;
&lt;h3 id="sec-2-3"&gt;Syntactical sugar&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-3"&gt;


&lt;p&gt;
Last but not least, &lt;code&gt;Boo&lt;/code&gt; gives the developer nice object-oriented
syntactical sugar to write all of this in a nice sort-of declarative
syntax, which is easier to read and reason about.
&lt;/p&gt;
&lt;p&gt;
The base object is &lt;code&gt;Base&lt;/code&gt;, which can be cloned through &lt;code&gt;derive&lt;/code&gt; and
instantiated with &lt;code&gt;make&lt;/code&gt;, although both use cloning in the prototypical
sense:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;Enum&lt;/span&gt; = boo.Base.derive({
  &lt;span style="color: #67D9F0;"&gt;each&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;fn&lt;/span&gt;) {
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.forEach(fn) },

  &lt;span style="color: #67D9F0;"&gt;filter&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;fn&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items = &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.filter(fn) },

  &lt;span style="color: #67D9F0;"&gt;map&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;fn&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items = &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.map(fn) },

  &lt;span style="color: #67D9F0;"&gt;fold&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;fn&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;start&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.reduce(fn, start) }
})

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;Coll&lt;/span&gt; = Enum.derive({
  &lt;span style="color: #67D9F0;"&gt;push&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;item&lt;/span&gt;) {
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.push(item) },

  &lt;span style="color: #67D9F0;"&gt;pop&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;item&lt;/span&gt;) {
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.pop(item) },

  &lt;span style="color: #67D9F0;"&gt;has_p&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;item&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.find(item) != -1 },

  &lt;span style="color: #67D9F0;"&gt;find&lt;/span&gt;: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;item&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.items.indexOf(item) }
})

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;my_coll&lt;/span&gt; = Coll.make()
my_coll.items = [1, 2, 3]
my_coll.map(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;n&lt;/span&gt;){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; n * n })
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; [1, 4, 9]&lt;/span&gt;
my_coll.find(4)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 1&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;What's next?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
&lt;code&gt;Boo&lt;/code&gt;'s source code is all on &lt;a href="http://github.com/killdream/boo"&gt;Github&lt;/a&gt;:
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ git clone http://github.com/killdream/boo.git
&lt;/pre&gt;


&lt;p&gt;
Though, if you want a quick'n'dirty install, just get it from NPM:
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ npm install boo
node&amp;gt; var boo = require(&lt;span style="color: #A6E32D;"&gt;'boo'&lt;/span&gt;)
node&amp;gt; var Stuff = boo.Base.derive({ ... })
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;Future developments?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;


&lt;p&gt;
Next release of &lt;code&gt;Boo&lt;/code&gt; will include traits, and should be up around
December. Traits are more interesting for structuring larger programs
than mixins are, but I have yet to experiment with them more until I can
stick with a nice API :3
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5" class="outline-2"&gt;
&lt;h2 id="sec-5"&gt;Acknowledgements&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;


&lt;p&gt;
Thanks for &lt;code&gt;AdamR&lt;/code&gt; in the comments and &lt;code&gt;xivix&lt;/code&gt; on Freenode's
&lt;code&gt;##javascript&lt;/code&gt; channel for pointing out the issue with Object.create on
the first snippet.
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Understanding JavaScript OOP</title>
   <link href="http://killdream.github.com//blog/2011/10/understanding-javascript-oop/index.html" />
   <updated>2011-10-09T00:00:00-07:00</updated>
   <id>http://killdream.github.com//blog/2011/10/understanding-javascript-oop/understanding-javascript-oop</id>
   <content type="html">&lt;p&gt;
JavaScript is an object oriented (OO) language, with its roots in the &lt;a href="http://selflanguage.org/"&gt;Self&lt;/a&gt;
programming language, although it's (sadly) designed to look like Java. This
makes the language's really powerful and sweet features stay covered by
some pretty ugly and counter-intuitive &lt;i&gt;work-arounds&lt;/i&gt;.
&lt;/p&gt;
&lt;p&gt;
One such affected feature is the implementation of prototypical
inheritance. The concepts are simple yet flexible and powerful. It makes
inheritance and behaviourism first-class citizens, just like functions
are first-class in functional-ish languages (JavaScript included).
&lt;/p&gt;
&lt;p&gt;
Fortunately, &lt;a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm"&gt;ECMAScript 5&lt;/a&gt; has gotten plenty of things to move the
language in the right way, and it's on those sweet features that this article
will expand. I'll also cover the drawbacks of JavaScript's
design, and do a little comparison with the classical model here and
there, where those would highlight the advantages or disadvantages of
the language's implementation of prototypical OO.
&lt;/p&gt;
&lt;p&gt;
It's important to note, though, that this article assumes you have
knowledge over other basic JavaScript functionality, like functions
(including the concepts of closures and first-class functions),
primitive values, operators and such.
&lt;/p&gt;


&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;1. Objects&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
Everything you can manipulate in JavaScript is an object. This includes
&lt;code&gt;Strings&lt;/code&gt;, &lt;code&gt;Arrays&lt;/code&gt;, &lt;code&gt;Numbers&lt;/code&gt;, &lt;code&gt;Functions&lt;/code&gt;, and, obviously, the
so-called &lt;code&gt;Object&lt;/code&gt; — there are primitives, but they're converted to an
object when you need to operate upon them. An object in the language is
simply a collection of key/value pairs (and some internal magic
sometimes).
&lt;/p&gt;
&lt;p&gt;
There are no concepts of classes anywhere, though. That is, an object
with properties &lt;code&gt;name: Linda, age: 21&lt;/code&gt; is not an instance of any class, the
&lt;code&gt;Object&lt;/code&gt; class. Both &lt;code&gt;Object&lt;/code&gt; and &lt;code&gt;Linda&lt;/code&gt; are instances of
themselves. They define their own behaviour, directly. There are no
layers of meta-data (i.e.: classes) to dictate what given object must
look like.
&lt;/p&gt;
&lt;p&gt;
You might ask: "how?"; more so if you come from a highly classically
Object Orientated language (like Java or C#). "Wouldn't having each
object defining their own behaviour, instead of a common &lt;i&gt;class&lt;/i&gt; mean that
if I have 100 objects, I will have 100 different methods? Also, isn't it
dangerous? How would one know if an object is really an Array, for
example?"
&lt;/p&gt;
&lt;p&gt;
Well, to answer all those questions, we'll first need to unlearn
everything about the classical OO approach and start from the ground
up. But, trust me, it's worth it.
&lt;/p&gt;
&lt;p&gt;
The prototypical OO model brings in some new ways of solving old
problems, in an more dynamic and expressive way. It also presents new
and more powerful models for extensibility and code-reuse, which is what
most people are interested about when they talk about Object
Orientation. It does not, however, give you contracts. Thus, there are
no static guarantees that an object &lt;code&gt;X&lt;/code&gt; will always have a given set of
properties, but to understand the trade-offs here, we'll need to know
what we're talking about first.
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-1-1" class="outline-3"&gt;
&lt;h3 id="sec-1-1"&gt;1.1. What are objects?&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-1"&gt;


&lt;p&gt;
As mentioned previously, objects are simple pairs of unique keys that
correspond to a value — we'll call this pair a &lt;code&gt;property&lt;/code&gt;. So, suppose
you'd want to describe a few aspects of an old friend (say &lt;code&gt;Mikhail&lt;/code&gt;),
like age, name and gender:
&lt;/p&gt;



&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/oop-obj-mikhail.png"  alt="http://dl.dropbox.com/u/4429200/blog/oop-obj-mikhail.png" /&gt;
&lt;/p&gt;
&lt;p&gt;
Objects are created in JavaScript using the &lt;code&gt;Object.create&lt;/code&gt; function. It
takes a parent and an optional set of property descriptors and makes a
brand new instance. We'll not worry much about the parameters
now.
&lt;/p&gt;
&lt;p&gt;
An empty object is an object with no parent, and no properties. The
syntax to create such object in JavaScript is the following:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;mikhail&lt;/span&gt; = Object.create(&lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;)
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-2" class="outline-3"&gt;
&lt;h3 id="sec-1-2"&gt;1.2. Creating properties&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-2"&gt;


&lt;p&gt;
So, now we have an object, but no properties — we've got to fix that if
we want to describe &lt;code&gt;Mikhail&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
Properties in JavaScript are dynamic. That means that they can be
created or removed at any time. Properties are also unique, in the sense
that a property key inside an object correspond to exactly one value.
&lt;/p&gt;
&lt;p&gt;
Creating new properties is done through the &lt;code&gt;Object.defineProperty&lt;/code&gt;
function, which takes a reference to an object, the name of the property
to create and a descriptor that defines the semantics of the property.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;Object.defineProperty(mikhail, &lt;span style="color: #A6E32D;"&gt;'name'&lt;/span&gt;, { value:        &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;
                                       , writable:     &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                       , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                       , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; })

Object.defineProperty(mikhail, &lt;span style="color: #A6E32D;"&gt;'age'&lt;/span&gt;, { value:        19
                                      , writable:     &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                      , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                      , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; })

Object.defineProperty(mikhail, &lt;span style="color: #A6E32D;"&gt;'gender'&lt;/span&gt;, { value:        &lt;span style="color: #A6E32D;"&gt;'Male'&lt;/span&gt;
                                         , writable:     &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                         , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                         , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; })
&lt;/pre&gt;


&lt;p&gt;
&lt;code&gt;Object.defineProperty&lt;/code&gt; will create a new property if a property with
the given key does not exist in the object, otherwise it'll update the
semantics and value of the existing property.
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;By the way&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  You can also use the &lt;code&gt;Object.defineProperties&lt;/code&gt; when you need to add
  more than one property to an object:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;Object.defineProperties(mikhail, { name:   { value:        &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;
                                           , writable:     &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                           , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                           , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; }

                                 , age:    { value:        19
                                           , writable:     &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                           , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                           , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; }

                                 , gender: { value:        &lt;span style="color: #A6E32D;"&gt;'Male'&lt;/span&gt;
                                           , writable:     &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                           , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                           , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; }})
&lt;/pre&gt;


&lt;p&gt;
  Obviously, both calls are overtly verbose — albeit also quite
  configurable —, thus not really meant for end-user code. It's better
  to create an abstraction layer on top of them.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-3" class="outline-3"&gt;
&lt;h3 id="sec-1-3"&gt;1.3. Descriptors&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-3"&gt;


&lt;p&gt;
The little objects that carry the semantics of a property are called descriptors
(we used them in the previous &lt;code&gt;Object.defineProperty&lt;/code&gt; calls). Descriptors can be
one of two types - data descriptors or accessor descriptors.
&lt;/p&gt;
&lt;p&gt;
Both types of descriptor contain flags, which define how a property is 
treated in the language. If a flag is not set, it's assumed to be &lt;code&gt;false&lt;/code&gt; —
unfortunately this is usually not a good default value for them, which
adds to the verbosity of these descriptors.
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;writable&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Whether the concrete value of the property may be changed. Only
  applies to data descriptors.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;configurable&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Whether the type of descriptor may be changed, or if the property can
  be removed.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;enumerable&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Whether the property is listed in a loop through the properties of the
  object.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;p&gt;
Data descriptors are those that hold concrete values, and therefore have 
an additional &lt;code&gt;value&lt;/code&gt; parameter, describing the concrete data bound to 
the property:
&lt;/p&gt;

&lt;dl&gt;
&lt;dt&gt;value&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  The value of a property.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;p&gt;
Accessor descriptors, on the other hand, proxy access to the concrete value
through getter and setter functions. When not set, they'll default to 
&lt;code&gt;undefined&lt;/code&gt;. 
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;get ()&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  A function called with no arguments when the property value is
  requested.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;set (new_value)&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  A function called with the new value for the property when the user
  tries to modify the value of the property.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-4" class="outline-3"&gt;
&lt;h3 id="sec-1-4"&gt;1.4. Ditching the verbosity&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-4"&gt;


&lt;p&gt;
Luckily, property descriptors are not the only way of working with
properties in JavaScript, they can also be handled in a sane and concise
way.
&lt;/p&gt;
&lt;p&gt;
JavaScript also understands references to a property using what we call
&lt;i&gt;bracket notation&lt;/i&gt;. The general rule is:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;&lt;span style="color: #67D9F0;"&gt;&amp;lt;bracket-access&amp;gt;&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;identifier&amp;gt;&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"["&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;expression&amp;gt;&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"]"&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
Where &lt;code&gt;identifier&lt;/code&gt; is the variable that holds the object containing the
properties we want to access, and &lt;code&gt;expression&lt;/code&gt; is any valid JavaScript
expression that defines the name of the property. There are no
constraints in which name a property can have&lt;sup&gt;&lt;a class="footref" name="fnr.1" href="#fn.1"&gt;1&lt;/a&gt;&lt;/sup&gt;, everything is fair
game.
&lt;/p&gt;
&lt;p&gt;
Thus, we could just as well rewrite our previous example as:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;mikhail[&lt;span style="color: #A6E32D;"&gt;'name'&lt;/span&gt;]   = &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;
mikhail[&lt;span style="color: #A6E32D;"&gt;'age'&lt;/span&gt;]    = 19
mikhail[&lt;span style="color: #A6E32D;"&gt;'gender'&lt;/span&gt;] = &lt;span style="color: #A6E32D;"&gt;'Male'&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
⁣
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;Note&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  All property names are ultimately converted to a String, such that
  &lt;code&gt;object[1]&lt;/code&gt;, &lt;code&gt;object[⁣[1]⁣]&lt;/code&gt;, &lt;code&gt;object['1']&lt;/code&gt; and &lt;code&gt;object[variable]&lt;/code&gt; (when
  the variable resolves to &lt;code&gt;1&lt;/code&gt;) are all equivalent.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;


&lt;p&gt;
There is another way of referring to a property called &lt;i&gt;dot notation&lt;/i&gt;, which
usually looks less cluttered and is easier to read than the bracket
alternative. However, it only works when the property name is a
&lt;a href="http://es5.github.com/#x7.6"&gt;valid JavaScript IdentifierName&lt;/a&gt;&lt;sup&gt;&lt;a class="footref" name="fnr.2" href="#fn.2"&gt;2&lt;/a&gt;&lt;/sup&gt;, and doesn't allow for arbitrary expressions
(so, variables here are a no-go).
&lt;/p&gt;
&lt;p&gt;
The rule for &lt;i&gt;dot notation&lt;/i&gt; is:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;&lt;span style="color: #67D9F0;"&gt;&amp;lt;dot-access&amp;gt;&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;identifier&amp;gt;&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"."&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;identifier-name&amp;gt;&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
This would give us an even sweeter way of defining properties:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;mikhail.name   = &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;
mikhail.age    = 19
mikhail.gender = &lt;span style="color: #A6E32D;"&gt;'Male'&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
Both of these syntaxes are equivalent to creating a data property, with
all semantic flags set to &lt;code&gt;true&lt;/code&gt;.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-5" class="outline-3"&gt;
&lt;h3 id="sec-1-5"&gt;1.5. Accessing properties&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-5"&gt;


&lt;p&gt;
Retrieving the values stored in a given property is as easy as creating
new ones, and the syntax is mostly similar as well — the only difference
being there isn't an assignment.
&lt;/p&gt;
&lt;p&gt;
So, if we want to check on Mikhail's age:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;mikhail[&lt;span style="color: #A6E32D;"&gt;'age'&lt;/span&gt;]
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 19&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
Trying to access a property that does not exist in the object simply
returns &lt;code&gt;undefined&lt;/code&gt; &lt;sup&gt;&lt;a class="footref" name="fnr.3" href="#fn.3"&gt;3&lt;/a&gt;&lt;/sup&gt;:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;mikhail[&lt;span style="color: #A6E32D;"&gt;'address'&lt;/span&gt;]
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; undefined&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-6" class="outline-3"&gt;
&lt;h3 id="sec-1-6"&gt;1.6. Removing properties&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-6"&gt;


&lt;p&gt;
To remove entire properties from an object, JavaScript provides the
&lt;code&gt;delete&lt;/code&gt; operator. So, if you wanted to remove the &lt;code&gt;gender&lt;/code&gt; property
from the &lt;code&gt;mikhail&lt;/code&gt; object:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;delete&lt;/span&gt; mikhail[&lt;span style="color: #A6E32D;"&gt;'gender'&lt;/span&gt;]
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; true&lt;/span&gt;

mikhail[&lt;span style="color: #A6E32D;"&gt;'gender'&lt;/span&gt;]
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; undefined&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
The &lt;code&gt;delete&lt;/code&gt; operator returns &lt;code&gt;true&lt;/code&gt; if the property was removed,
&lt;code&gt;false&lt;/code&gt; otherwise. I won't delve into details of the workings of this
operator, since &lt;a href="http://twitter.com/kangax"&gt;@kangax&lt;/a&gt; has already written a
&lt;a href="http://perfectionkills.com/understanding-delete/"&gt;most awesome article on how delete works&lt;/a&gt;.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-7" class="outline-3"&gt;
&lt;h3 id="sec-1-7"&gt;1.7. Getters and setters&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-7"&gt;


&lt;p&gt;
Getters and setters are usually used in classical object oriented
languages to provide encapsulation. They are not much needed in
JavaScript, though, given how dynamic the language is — &lt;del&gt;and my bias against the feature&lt;/del&gt;.
&lt;/p&gt;
&lt;p&gt;
At any rate, they allow you to proxy the requests for reading a property
value or setting it, and decide how to handle each situation. So,
suppose we had separate slots for our object's first and last name, but
wanted a simple interface for reading and setting it.
&lt;/p&gt;
&lt;p&gt;
First, let's set the first and last names of our friend, as concrete
data properties:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;Object.defineProperty(mikhail, &lt;span style="color: #A6E32D;"&gt;'first_name'&lt;/span&gt;, { value:    &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;
                                             , writable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; })

Object.defineProperty(mikhail, &lt;span style="color: #A6E32D;"&gt;'last_name'&lt;/span&gt;, { value:    &lt;span style="color: #A6E32D;"&gt;'Wei&amp;#223;'&lt;/span&gt;
                                            , writable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; })
&lt;/pre&gt;


&lt;p&gt;
Then we can define a common way of accessing and setting both of those
values at the same time — let's call it &lt;code&gt;name&lt;/code&gt;:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;() &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Returns the full name of object.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;get_full_name&lt;/span&gt;() {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.first_name + &lt;span style="color: #A6E32D;"&gt;' '&lt;/span&gt; + &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.last_name
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(new_name:String) &amp;#8594; undefined&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Sets the name components of the object, from a full name.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;set_full_name&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;new_name&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;names&lt;/span&gt;
    names = new_name.trim().split(&lt;span style="color: #A6E32D;"&gt;/\s+/&lt;/span&gt;)
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.first_name = names[&amp;#8291;&lt;span style="color: #A6E32D;"&gt;'0'&lt;/span&gt;] || &lt;span style="color: #A6E32D;"&gt;''&lt;/span&gt;
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.last_name  = names[&lt;span style="color: #A6E32D;"&gt;'1'&lt;/span&gt;] || &lt;span style="color: #A6E32D;"&gt;''&lt;/span&gt;
}

Object.defineProperty(mikhail, &lt;span style="color: #A6E32D;"&gt;'name'&lt;/span&gt;, { get: get_full_name
                                       , set: set_full_name
                                       , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                       , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; })
&lt;/pre&gt;


&lt;p&gt;
Now, every-time we try to access the value of Mikhail's &lt;code&gt;name&lt;/code&gt; property,
it'll execute the &lt;code&gt;get_full_name&lt;/code&gt; getter.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;mikhail.name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Mikhail Wei&amp;#223;'&lt;/span&gt;

mikhail.first_name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Mikhail'&lt;/span&gt;

mikhail.last_name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Wei&amp;#223;'&lt;/span&gt;

mikhail.last_name = &lt;span style="color: #A6E32D;"&gt;'White'&lt;/span&gt;
mikhail.name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Mikhail White'&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
We can also set the name of the object, by assigning a value to the
property, this will then execute &lt;code&gt;set_full_name&lt;/code&gt; to do the dirty work.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;mikhail.name = &lt;span style="color: #A6E32D;"&gt;'Michael White'&lt;/span&gt;

mikhail.name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White'&lt;/span&gt;

mikhail.first_name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael'&lt;/span&gt;

mikhail.last_name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'White'&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
Of course, getters and setters make property access and modification
&lt;a href="http://jsperf.com/getter-setter/8"&gt;fairly slower&lt;/a&gt;. They do have some use-cases, but while browsers don't
optimise them better, methods seem to be the way to go.
&lt;/p&gt;
&lt;p&gt;
Also, it should be noted that while getters and setters are usually used
for encapsulation in other languages, in ECMAScript 5 you still can't have
such if you need the information to be stored in the object itself. All
properties in an object are public.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-8" class="outline-3"&gt;
&lt;h3 id="sec-1-8"&gt;1.8. Listing properties&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-8"&gt;


&lt;p&gt;
Since properties are dynamic, JavaScript provides a way of checking out
which properties an object defines. There are two ways of listing the
properties of an object, depending on what kind of properties one is
interested into.
&lt;/p&gt;
&lt;p&gt;
The first one is done through a call to &lt;code&gt;Object.getOwnPropertyNames&lt;/code&gt;,
which returns an &lt;code&gt;Array&lt;/code&gt; containing the names of &lt;b&gt;all&lt;/b&gt; properties set
directly in the object — we call these kind of property &lt;b&gt;own&lt;/b&gt;, by the
way.
&lt;/p&gt;
&lt;p&gt;
If we check now what we know about Mikhail:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;Object.getOwnPropertyNames(mikhail)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; [ 'name', 'age', 'gender', 'first_name', 'last_name' ]&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
The second way is using &lt;code&gt;Object.keys&lt;/code&gt;, which returns all own properties
that have been marked as &lt;b&gt;enumerable&lt;/b&gt; when they were defined:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;Object.keys(mikhail)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; [ 'name', 'age', 'gender' ]&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-9" class="outline-3"&gt;
&lt;h3 id="sec-1-9"&gt;1.9. Object literals&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-9"&gt;


&lt;p&gt;
An even easier way of defining objects is to use the object literal
(also called &lt;i&gt;object initialiser&lt;/i&gt;) syntax that JavaScript provides. An
object literal denotes a fresh object, that has its parent as the
&lt;code&gt;Object.prototype&lt;/code&gt; object. We'll talk more about parents when we visit
inheritance, later on.
&lt;/p&gt;
&lt;p&gt;
At any rate, the object literal syntax allows you to define simple
objects and initialise it with properties at the same time. So, we could
rewrite our Mikhail object to the following:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;mikhail&lt;/span&gt; = { first_name: &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;
              , last_name:  &lt;span style="color: #A6E32D;"&gt;'Wei&amp;#223;'&lt;/span&gt;
              , age:        19
              , gender:     &lt;span style="color: #A6E32D;"&gt;'Male'&lt;/span&gt;

              &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;() &amp;#8594; String&lt;/span&gt;
              &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Returns the full name of object.&lt;/span&gt;
              , get name() {
                    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.first_name + &lt;span style="color: #A6E32D;"&gt;' '&lt;/span&gt; + &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.last_name }

              &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(new_name:String) &amp;#8594; undefined&lt;/span&gt;
              &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Sets the name components of the object,&lt;/span&gt;
              &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;from a full name.&lt;/span&gt;
              , set name(new_name) { &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;names&lt;/span&gt;
                    names = new_name.trim().split(&lt;span style="color: #A6E32D;"&gt;/\s+/&lt;/span&gt;)
                    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.first_name = names[&lt;span style="color: #A6E32D;"&gt;'0'&lt;/span&gt;] || &lt;span style="color: #A6E32D;"&gt;''&lt;/span&gt;
                    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.last_name  = names[&lt;span style="color: #A6E32D;"&gt;'1'&lt;/span&gt;] || &lt;span style="color: #A6E32D;"&gt;''&lt;/span&gt; }
              }
&lt;/pre&gt;


&lt;p&gt;
Property names that are not valid identifiers must be quoted. Also note
that the getter/setter notation for object literals strictly defines a
new anonymous function. If you want to assign a previously declared
function to a getter/setter, you need to use the &lt;code&gt;Object.defineProperty&lt;/code&gt;
function.
&lt;/p&gt;
&lt;p&gt;
The rules for object literal can be described as the following:
&lt;/p&gt;



&lt;pre class="src src-bnf"&gt;&lt;span style="color: #67D9F0;"&gt;&amp;lt;object-literal&amp;gt;&lt;/span&gt;  &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"{"&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;property-list&amp;gt;&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"}"&lt;/span&gt;
                    ;
&lt;span style="color: #67D9F0;"&gt;&amp;lt;property-list&amp;gt;&lt;/span&gt;   &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;property&amp;gt;&lt;/span&gt; [&lt;span style="color: #A6E32D;"&gt;","&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;property&amp;gt;&lt;/span&gt;]*
                    ;
&lt;span style="color: #67D9F0;"&gt;&amp;lt;property&amp;gt;&lt;/span&gt;        &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;data-property&amp;gt;&lt;/span&gt;
                    &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;getter-property&amp;gt;&lt;/span&gt;
                    &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;setter-property&amp;gt;&lt;/span&gt;
                    ;
&lt;span style="color: #67D9F0;"&gt;&amp;lt;data-property&amp;gt;&lt;/span&gt;   &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;property-name&amp;gt;&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;":"&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;expression&amp;gt;&lt;/span&gt;
                    ;
&lt;span style="color: #67D9F0;"&gt;&amp;lt;getter-property&amp;gt;&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"get"&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;identifier&amp;gt;&lt;/span&gt;
                    :       &lt;span style="color: #729FCF;"&gt;&amp;lt;function-parameters&amp;gt;&lt;/span&gt;
                    :       &lt;span style="color: #729FCF;"&gt;&amp;lt;function-block&amp;gt;&lt;/span&gt;
                    ;
&lt;span style="color: #67D9F0;"&gt;&amp;lt;setter-property&amp;gt;&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #A6E32D;"&gt;"set"&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;identifier&amp;gt;&lt;/span&gt;
                    :       &lt;span style="color: #729FCF;"&gt;&amp;lt;function-parameters&amp;gt;&lt;/span&gt;
                    :       &lt;span style="color: #729FCF;"&gt;&amp;lt;function-block&amp;gt;&lt;/span&gt;
                    ;
&lt;span style="color: #67D9F0;"&gt;&amp;lt;property-name&amp;gt;&lt;/span&gt;   &lt;span style="color: #FA2573;"&gt;::=&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;identifier&amp;gt;&lt;/span&gt;
                    &lt;span style="color: #E52222; font-weight: bold;"&gt;|&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;&amp;lt;quoted-identifier&amp;gt;&lt;/span&gt;
                    ;
&lt;/pre&gt;


&lt;p&gt;
Object literals can only appear inside expressions in
JavaScript. Since the syntax is ambiguous to block statements in the
language, new-comers usually confound the two:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;This is a block statement, with a label:&lt;/span&gt;
{ foo: &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt; }
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'bar'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;This is a syntax error (labels can't be quoted):&lt;/span&gt;
{ &lt;span style="color: #A6E32D;"&gt;"foo"&lt;/span&gt;: &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt; }
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; SyntaxError: Invalid label&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;This is an object literal (note the parenthesis to force&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;parsing the contents as an expression):&lt;/span&gt;
({ &lt;span style="color: #A6E32D;"&gt;"foo"&lt;/span&gt;: &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt; })
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; { foo: 'bar' }&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Where the parser is already expecting expressions,&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;object literals don't need to be forced. E.g.:&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;x&lt;/span&gt; = { foo: &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt; }
fn({foo: &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt;})
&lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; { foo: &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt; }
1, { foo: &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt; }
( ... )
&lt;/pre&gt;



&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;2. Methods&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
Up until now, the Mikhail object only defined slots of concrete data —
with the exception of the name getter/setter. Defining actions that may
be performed on a certain object in JavaScript is just as simple.
&lt;/p&gt;
&lt;p&gt;
This is because JavaScript does not differentiate how you can manipulate
a &lt;code&gt;Function&lt;/code&gt;, a &lt;code&gt;Number&lt;/code&gt; or an &lt;code&gt;Object&lt;/code&gt;. Everything is treated the same
way (i.e.: functions in JavaScript are first-class).
&lt;/p&gt;
&lt;p&gt;
As such, to define an action for a given object, you just assign a
function object reference to a property. Let's say we wanted a way for
Mikhail to greet someone:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets a random person&lt;/span&gt;
mikhail.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Why, hello there, '&lt;/span&gt; + person + &lt;span style="color: #A6E32D;"&gt;'.'&lt;/span&gt;
}
&lt;/pre&gt;


&lt;p&gt;
After setting the property, we can use it the same way we used the
concrete data that were assigned to the object. That is, accessing the
property will return a reference to the function object stored there, so
we can just call.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;mikhail.greet(&lt;span style="color: #A6E32D;"&gt;'you'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: Why, hello there, you.'&lt;/span&gt;

mikhail.greet(&lt;span style="color: #A6E32D;"&gt;'Kristin'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: Why, hello there, Kristin.'&lt;/span&gt;
&lt;/pre&gt;




&lt;/div&gt;

&lt;div id="outline-container-2-1" class="outline-3"&gt;
&lt;h3 id="sec-2-1"&gt;2.1. Dynamic &lt;code&gt;this&lt;/code&gt;&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-1"&gt;


&lt;p&gt;
One thing that you must have noticed both in the &lt;code&gt;greet&lt;/code&gt; function, and
the functions we've used for the &lt;code&gt;name&lt;/code&gt;'s getter/setter, is that they
use a magical variable called &lt;code&gt;this&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
It holds a reference to the object that the function is being applied
to. This doesn't necessarily means that &lt;code&gt;this&lt;/code&gt; will equal the object
where the function is &lt;b&gt;stored&lt;/b&gt;. No, JavaScript is not so
selfish. 
&lt;/p&gt;
&lt;p&gt;
Functions are generics. That is, in JavaScript, what &lt;code&gt;this&lt;/code&gt; refers to is
decided dynamically, at the time the function is called, and depending
only on how such a function is called.
&lt;/p&gt;
&lt;p&gt;
Having &lt;code&gt;this&lt;/code&gt; dynamically resolved is an incredible powerful mechanism
for the dynamism of JavaScript's object orientation and lack of
strictly enforced structures (i.e.: classes), this means one can apply a
function to any object that meets the requirements of the actions it
performs, regardless of how the object has been constructed — hack in
some custom multiple dispatcher and you have &lt;a href="http://en.wikipedia.org/wiki/Common_Lisp_Object_System"&gt;CLOS&lt;/a&gt;.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-2" class="outline-3"&gt;
&lt;h3 id="sec-2-2"&gt;2.2. How &lt;code&gt;this&lt;/code&gt; is resolved&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-2"&gt;


&lt;p&gt;
There are four different ways of resolving the &lt;code&gt;this&lt;/code&gt; variable in a
function, depending on how a function is called: directly; as a method;
explicitly applied; as a constructor. We'll dive in the first three for
now, and come back at constructors later on.
&lt;/p&gt;
&lt;p&gt;
For the following examples, we'll take these definitions into account:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(other:Number[, yet_another:Number]) &amp;#8594; Number&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Returns the sum of the object's value with the given Number&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;add&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;other&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;yet_another&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.value + other + (yet_another || 0)
}

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;one&lt;/span&gt; = { value: 1, add: add }
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;two&lt;/span&gt; = { value: 2, add: add }
&lt;/pre&gt;




&lt;/div&gt;

&lt;div id="outline-container-2-2-1" class="outline-4"&gt;
&lt;h4 id="sec-2-2-1"&gt;2.2.1. Called as a method&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-2-2-1"&gt;


&lt;p&gt;
If a function is called as an object's method, then &lt;code&gt;this&lt;/code&gt; inside the
function will refer to the object. That is, when we explicitly state
that an object is carrying an action, then that object will be our
&lt;code&gt;this&lt;/code&gt; inside the function.
&lt;/p&gt;
&lt;p&gt;
This is what happened when we called &lt;code&gt;mikhail.greet()&lt;/code&gt;. The property
access at the time of the call tells JavaScript that we want to apply
whatever actions the &lt;code&gt;greet&lt;/code&gt; function defines to the &lt;code&gt;mikhail&lt;/code&gt; object.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;one.add(two.value) &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === one&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 3&lt;/span&gt;

two.add(3)         &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === two&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 5&lt;/span&gt;

one[&lt;span style="color: #A6E32D;"&gt;'add'&lt;/span&gt;](two.value) &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;brackets are cool too&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-2-2" class="outline-4"&gt;
&lt;h4 id="sec-2-2-2"&gt;2.2.2. Called directly&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-2-2-2"&gt;


&lt;p&gt;
When a function is called directly, &lt;code&gt;this&lt;/code&gt; will be resolved to the
global object in the engine (e.g.: &lt;code&gt;window&lt;/code&gt; in browsers, &lt;code&gt;global&lt;/code&gt; in
Node.js)
&lt;/p&gt;



&lt;pre class="src src-js"&gt;add(two.value)  &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === global&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; NaN&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;The global object still has no `value' property, let's fix that.&lt;/span&gt;
value = 2
add(two.value)  &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === global&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 4&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-2-3" class="outline-4"&gt;
&lt;h4 id="sec-2-2-3"&gt;2.2.3. Explicitly applied&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-2-2-3"&gt;


&lt;p&gt;
Finally, a function may be explicitly applied to any object, regardless
of whether the object has the function stored as a property or
not. These applications are done through a either the &lt;code&gt;call&lt;/code&gt; or &lt;code&gt;apply&lt;/code&gt;
method of a function object.
&lt;/p&gt;
&lt;p&gt;
The difference between these two methods is the way they take in the
parameters that will be passed to the function, and the performance —
&lt;code&gt;apply&lt;/code&gt; being up to 55x slower than a direct call, whereas &lt;code&gt;call&lt;/code&gt; is
usually not as bad. This might vary greatly depending on the engine
though, so it's always better to do a &lt;a href="http://jsperf.com"&gt;Perf test&lt;/a&gt; rather than being scared
of using the functionality — don't optimise early!
&lt;/p&gt;
&lt;p&gt;
Anyways, &lt;code&gt;call&lt;/code&gt; expects the object that the function will be applied to
as the first parameter, and the parameters to apply to the function as
positional arguments:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;add.call(two, 2, 2)      &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === two&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 6&lt;/span&gt;

add.call(window, 4)      &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === global&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 6&lt;/span&gt;

add.call(one, one.value) &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === one&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 2&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
On the other hand, &lt;code&gt;apply&lt;/code&gt; lets you pass an array of parameters as the
second parameter of the function. The array will be passed as positional
arguments to the target function:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;add.apply(two, [2, 2])       &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;equivalent to two.add(2, 2)&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 6&lt;/span&gt;

add.apply(window, [ 4 ])       &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;equivalent to add(4)&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 6&lt;/span&gt;

add.apply(one, [one.value])  &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;equivalent to one.add(one.value)&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 2&lt;/span&gt;
&lt;/pre&gt;


&lt;dl&gt;
&lt;dt&gt;Note&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  What &lt;code&gt;this&lt;/code&gt; resolves to when applying a function to &lt;code&gt;null&lt;/code&gt; or
  &lt;code&gt;undefined&lt;/code&gt; depends on the semantics used by the engine. Usually, it
  would be the same as explicitly applying the function to the global
  object. But if the engine is running on &lt;a href="https://developer.mozilla.org/en/JavaScript/Strict_mode"&gt;strict mode&lt;/a&gt;, then &lt;code&gt;this&lt;/code&gt; will
  be resolved as expected — to the exact thing it was applied to:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;window.value = 2
add.call(&lt;span style="color: #FA2573;"&gt;undefined&lt;/span&gt;, 1) &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === window&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 3&lt;/span&gt;

&lt;span style="color: #C48DFF;"&gt;void&lt;/span&gt; &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() {
  &lt;span style="color: #A6E32D;"&gt;"use strict"&lt;/span&gt;
  add.call(&lt;span style="color: #FA2573;"&gt;undefined&lt;/span&gt;, 1) &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === undefined&lt;/span&gt;
  &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; NaN&lt;/span&gt;
  &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Since primitives can't hold properties.&lt;/span&gt;
}()
&lt;/pre&gt;

&lt;/dd&gt;
&lt;/dl&gt;



&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-3" class="outline-3"&gt;
&lt;h3 id="sec-2-3"&gt;2.3. Bound methods&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-3"&gt;


&lt;p&gt;
Aside from the dynamic nature of functions in JavaScript, there is also
a way of making a function bound to an specific object, such that &lt;code&gt;this&lt;/code&gt;
inside that function will always resolve to the given object, regardless
of whether it's called as that object's method or directly.
&lt;/p&gt;
&lt;p&gt;
The function that provides such functionality is &lt;code&gt;bind&lt;/code&gt;. It takes an
object, and additional parameters (in the same manner as &lt;code&gt;call&lt;/code&gt;), and
returns a new function that will apply those parameters to the original
function when called:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;one_add&lt;/span&gt; = add.bind(one)

one_add(2) &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === one&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 3&lt;/span&gt;

two.one_adder = one_add
two.one_adder(2) &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === one&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 3&lt;/span&gt;

one_add.call(two, 2) &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;this === one&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 3&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;3. Inheritance&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
Up to this point we have seen how objects can define their own
behaviours, and how we can reuse (by explicit application) actions in
other objects, however, this still doesn't give us a nice way for
code reuse and extensibility.
&lt;/p&gt;
&lt;p&gt;
That's where inheritance comes into play. Inheritance allows for a
greater separation of concerns, where objects define specialised
behaviours by building upon the behaviours of other objects.
&lt;/p&gt;
&lt;p&gt;
The prototypical model goes further than that, though, and allows for
selective extensibility, behaviour sharing and other interesting
patterns we'll explore in a bit. Sad thing is: the specific model of
prototypical OO implemented by JavaScript is a bit limited, so
circumventing these limitations to accommodate these patterns will bring
in a bit of overhead sometimes.
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-3-1" class="outline-3"&gt;
&lt;h3 id="sec-3-1"&gt;3.1. Prototypes&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-1"&gt;


&lt;p&gt;
Inheritance in JavaScript revolves around cloning the behaviours of an
object and extending it with specialised behaviours. The object that has
it's behaviours cloned is called &lt;b&gt;Prototype&lt;/b&gt; (not to be confounded with
the &lt;code&gt;prototype&lt;/code&gt; property of functions).
&lt;/p&gt;
&lt;p&gt;
A prototype is just a plain object, that happens to share it's
behaviours with another object — it acts as the object's parent.
&lt;/p&gt;
&lt;p&gt;
Now, the concepts of this &lt;i&gt;behaviour cloning&lt;/i&gt; does not imply that you'll
have two different copies of the same function, or data. In fact,
JavaScript implements inheritance by delegation, all properties are kept
in the parent, and access to them is just extended for the child.
&lt;/p&gt;
&lt;p&gt;
As mentioned previously, the parent (or &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt;) of an object is
defined by making a call to &lt;code&gt;Object.create&lt;/code&gt;, and passing a reference of
the object to use as parent in the first parameter.
&lt;/p&gt;
&lt;p&gt;
This would come well in our example up until now. For example, the
greeting and name actions can be well defined in a separate object and
shared with other objects that need them.
&lt;/p&gt;
&lt;p&gt;
Which takes us to the following model:
&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/oop-proto-person.png"  alt="http://dl.dropbox.com/u/4429200/blog/oop-proto-person.png" /&gt;
&lt;/p&gt;
&lt;p&gt;
We can implement this in JavaScript with the following definitions:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;person&lt;/span&gt; = Object.create(&lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;)

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Here we are reusing the previous getter/setter functions&lt;/span&gt;
Object.defineProperty(person, &lt;span style="color: #A6E32D;"&gt;'name'&lt;/span&gt;, { get: get_full_name
                                      , set: set_full_name
                                      , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                      , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; })

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And adding the `greet' function&lt;/span&gt;
person.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; (&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Why, hello there, '&lt;/span&gt; + person + &lt;span style="color: #A6E32D;"&gt;'.'&lt;/span&gt;
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Then we can share those behaviours with Mikhail&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;By creating a new object that has it's [[Prototype]] property&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;pointing to `person'.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;mikhail&lt;/span&gt; = Object.create(person)
mikhail.first_name = &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;
mikhail.last_name  = &lt;span style="color: #A6E32D;"&gt;'Wei&amp;#223;'&lt;/span&gt;
mikhail.age        = 19
mikhail.gender     = &lt;span style="color: #A6E32D;"&gt;'Male'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And we can test whether things are actually working.&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;First, `name' should be looked on `person'&lt;/span&gt;
mikhail.name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Mikhail Wei&amp;#223;'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Setting `name' should trigger the setter&lt;/span&gt;
mikhail.name = &lt;span style="color: #A6E32D;"&gt;'Michael White'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Such that `first_name' and `last_name' now reflect the&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;previously name setting.&lt;/span&gt;
mikhail.first_name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael'&lt;/span&gt;
mikhail.last_name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'White'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;`greet' is also inherited from `person'.&lt;/span&gt;
mikhail.greet(&lt;span style="color: #A6E32D;"&gt;'you'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: Why, hello there, you.'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And just to be sure, we can check which properties actually&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;belong to `mikhail'&lt;/span&gt;
Object.keys(mikhail)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; [ 'first_name', 'last_name', 'age', 'gender' ]&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-2" class="outline-3"&gt;
&lt;h3 id="sec-3-2"&gt;3.2. How &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; works&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-2"&gt;


&lt;p&gt;
As you could see from the previous example, none of the properties
defined in &lt;code&gt;Person&lt;/code&gt; have flown to the &lt;code&gt;Mikhail&lt;/code&gt; object, and yet we could
access them just fine. This happens because JavaScript implements
delegated property access, that is, a property is searched through all
parents of an object.
&lt;/p&gt;
&lt;p&gt;
This parent chain is defined by a hidden slot in every object, called
&lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt;. You can't change this slot directly&lt;sup&gt;&lt;a class="footref" name="fnr.4" href="#fn.4"&gt;4&lt;/a&gt;&lt;/sup&gt;, so the only
way of setting it is when you're creating a fresh object.
&lt;/p&gt;
&lt;p&gt;
When a property is requested from the object, the engine first tries to
retrieve the property from the target object. If the property isn't
there, the search continue through the immediate parent of that object,
and the parent of that parent, and so on.
&lt;/p&gt;
&lt;p&gt;
This means that we can change the behaviours of a prototype at run time,
and have it reflected in all objects that inherit from it. For example,
let's suppose we wanted a different default greeting:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets the given person&lt;/span&gt;
person.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Harro, '&lt;/span&gt; + person + &lt;span style="color: #A6E32D;"&gt;'.'&lt;/span&gt;
}

mikhail.greet(&lt;span style="color: #A6E32D;"&gt;'you'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: Harro, you.'&lt;/span&gt;
&lt;/pre&gt;


&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-3" class="outline-3"&gt;
&lt;h3 id="sec-3-3"&gt;3.3. Overriding properties&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-3"&gt;

&lt;p&gt;So, prototypes (that is, inheritance) are used for sharing data with
other objects, and it does such in a pretty fast and memory-effective
manner too, since you'll always have only one instance of a given piece
of data lying around.
&lt;/p&gt;
&lt;p&gt;
Now what if we want to add specialised behaviours, that build upon the
data that was shared with the object? Well, we have seen before that
objects define their own behaviours by means of properties, so
specialised behaviours follow the same principle — you just assign a
value to the relevant property.
&lt;/p&gt;
&lt;p&gt;
To better demonstrate it, suppose &lt;code&gt;Person&lt;/code&gt; implements only a general
greeting, and everyone inheriting from &lt;code&gt;Person&lt;/code&gt; define their own
specialised and unique greetings. Also, let's add a new person to our
case scenario, so to outline better how objects are extended:
&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/oop-extend.png"  alt="http://dl.dropbox.com/u/4429200/blog/oop-extend.png" /&gt;
&lt;/p&gt;
&lt;p&gt;
Note that both &lt;code&gt;mikhail&lt;/code&gt; and &lt;code&gt;kristin&lt;/code&gt; define their own version of
&lt;code&gt;greet&lt;/code&gt;. In this case, whenever we call the &lt;code&gt;greet&lt;/code&gt; method on them
they'll use their own version of that behaviour, instead of the one that
was shared from &lt;code&gt;person&lt;/code&gt;.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Here we set up the greeting for a generic person&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets the given person, formally&lt;/span&gt;
person.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Hello, '&lt;/span&gt; + (person || &lt;span style="color: #A6E32D;"&gt;'you'&lt;/span&gt;)
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And a greeting for our protagonist, Mikhail&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets the given person, like a bro&lt;/span&gt;
mikhail.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': \'sup, '&lt;/span&gt; + (person || &lt;span style="color: #A6E32D;"&gt;'dude'&lt;/span&gt;)
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And define our new protagonist, Kristin&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;kristin&lt;/span&gt; = Object.create(person)
kristin.first_name = &lt;span style="color: #A6E32D;"&gt;'Kristin'&lt;/span&gt;
kristin.last_name  = &lt;span style="color: #A6E32D;"&gt;'Wei&amp;#223;'&lt;/span&gt;
kristin.age        = 19
kristin.gender     = &lt;span style="color: #A6E32D;"&gt;'Female'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Alongside with her specific greeting manners&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets the given person, sweetly&lt;/span&gt;
kristin.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': \'ello, '&lt;/span&gt; + (person || &lt;span style="color: #A6E32D;"&gt;'sweetie'&lt;/span&gt;)
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Finally, we test if everything works according to the expected&lt;/span&gt;

mikhail.greet(kristin.first_name)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: \'sup, Kristin'&lt;/span&gt;

mikhail.greet()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: \'sup, dude'&lt;/span&gt;

kristin.greet(mikhail.first_name)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Kristin Wei&amp;#223;: \'ello, Michael'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And just so we check how cool this [[Prototype]] thing is,&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;let's get Kristin back to the generic behaviour&lt;/span&gt;

&lt;span style="color: #C48DFF;"&gt;delete&lt;/span&gt; kristin.greet
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; true&lt;/span&gt;

kristin.greet(mikhail.first_name)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Kristin Wei&amp;#223;: Hello, Michael'&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-4" class="outline-3"&gt;
&lt;h3 id="sec-3-4"&gt;3.4. Mixins&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-4"&gt;

&lt;p&gt;Prototypes allow for behaviour sharing in JavaScript, and although they
are undeniably powerful, they aren't quite as powerful as they could
be. For one, prototypes only allow that one object inherit from another
single object, while extending those behaviours as they see fit.
&lt;/p&gt;
&lt;p&gt;
However, this approach quickly kills interesting things like behaviour
composition, where we could mix-and-match several objects into one, with
all the advantages highlighted in the prototypical inheritance.
&lt;/p&gt;
&lt;p&gt;
Multiple inheritance would also allow the usage of &lt;i&gt;data-parents&lt;/i&gt; —
objects that provide an example state that fulfils the requirements for
a given behaviour. Default properties, if you will.
&lt;/p&gt;
&lt;p&gt;
Luckily, since we can define behaviours directly on an object in
JavaScript, we can work-around these issues by using mixins — and adding
a little overhead at object's creation time.
&lt;/p&gt;
&lt;p&gt;
So, what are mixins anyways? Well, they are parent-less objects. That
is, they fully define their own behaviour, and are mostly designed to be
incorporated in other objects (although you could use their methods
directly).
&lt;/p&gt;
&lt;p&gt;
Continuing with our little protagonists' scenario, let's extend it to
add some capabilities to them. Let's say that every person can also be a
&lt;code&gt;pianist&lt;/code&gt; or a &lt;code&gt;singer&lt;/code&gt;. A given person can have no such abilities, be
just a pianist, just a singer or both. This is the kind of case where
JavaScript's model of prototypical inheritance falls short, so we're
going to cheat a little bit.
&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/oop-mixins.png"  alt="http://dl.dropbox.com/u/4429200/blog/oop-mixins.png" /&gt;
&lt;/p&gt;
&lt;p&gt;
For mixins to work, we first need to have a way of combining different
objects into a single one. JavaScript doesn't provide this out-of-the
box, but we can easily make one by copying all &lt;b&gt;own&lt;/b&gt; property
descriptors, the ones defined directly in the object, rather than
inherited, from one object to another.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Aliases for the rather verbose methods on ES5&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;descriptor&lt;/span&gt;  = Object.getOwnPropertyDescriptor
  , properties  = Object.getOwnPropertyNames
  , define_prop = Object.defineProperty

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(target:Object, source:Object) &amp;#8594; Object&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Copies properties from `source' to `target'&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;extend&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;target&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;source&lt;/span&gt;) {
    properties(source).forEach(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;key&lt;/span&gt;) {
        define_prop(target, key, descriptor(source, key)) })

    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; target
}
&lt;/pre&gt;


&lt;p&gt;
Basically, what &lt;code&gt;extend&lt;/code&gt; does here is taking two objects — a source and
a target, — iterating over all properties present on the &lt;code&gt;source&lt;/code&gt;
object, and copying the property descriptors over to &lt;code&gt;target&lt;/code&gt;. Note that
this is a destructive method, meaning that &lt;code&gt;target&lt;/code&gt; will be modified
in-place. It's the cheapest way, though, and usually not a problem.
&lt;/p&gt;
&lt;p&gt;
Now that we have a method for copying properties over, we can start
assigning multiple abilities to our objects (&lt;code&gt;mikhail&lt;/code&gt; e
&lt;code&gt;kristin&lt;/code&gt;):
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;A pianist is someone who can `play' the piano&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;pianist&lt;/span&gt; = Object.create(&lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;)
pianist.play = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;' starts playing the piano.'&lt;/span&gt;
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;A singer is someone who can `sing'&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;singer&lt;/span&gt; = Object.create(&lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;)
singer.sing = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;' starts singing.'&lt;/span&gt;
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Then we can move on to adding those abilities to&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;our main objects:&lt;/span&gt;
extend(mikhail, pianist)
mikhail.play()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White starts playing the piano.'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;We can see that all that ends up as an own property of&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;mikhail. It is not shared.&lt;/span&gt;
Object.keys(mikhail)
[&lt;span style="color: #A6E32D;"&gt;'first_name'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'last_name'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'age'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'gender'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'greet'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'play'&lt;/span&gt;]

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Then we can define kristin as a singer&lt;/span&gt;
extend(kristin, singer)
kristin.sing()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Kristin Wei&amp;#223; starts singing.'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Mikhail can't sing yet though&lt;/span&gt;
mikhail.sing()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; TypeError: Object #&amp;lt;Object&amp;gt; has no method 'sing'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;But mikhail will inherit the `sing' method if we&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;extend the Person prototype with it:&lt;/span&gt;
extend(person, singer)

mikhail.sing()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White starts singing.'&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-5" class="outline-3"&gt;
&lt;h3 id="sec-3-5"&gt;3.5. Accessing overwritten properties&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-5"&gt;

&lt;p&gt;Now that we're able to inherit properties from other objects and extend
the specialised objects to define their own behaviours, we have a little
problem: what if we actually wanted to access the parent behaviours that
we just overwrote?
&lt;/p&gt;
&lt;p&gt;
JavaScript provides the &lt;code&gt;Object.getPrototypeOf&lt;/code&gt; function, that returns
the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; of an object. This way, we have access to all
properties defined within the prototype chain of an object. So,
accessing a property in the parent of an object is quite simple:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;Object.getPrototypeOf(mikhail).name    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;same as `person.name'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'undefined undefined'&lt;/span&gt;

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;We can assert it's really being called on `person' by&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;giving `person' a `first_name' and `last_name'&lt;/span&gt;
person.first_name = &lt;span style="color: #A6E32D;"&gt;'Random'&lt;/span&gt;
person.last_name  = &lt;span style="color: #A6E32D;"&gt;'Person'&lt;/span&gt;
Object.getPrototypeOf(mikhail).name
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Random Person'&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
So, a naïve solution for applying a method stored in the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt;
of an object to the current one, would then follow, quite naturally, by
looking the property on the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; of &lt;code&gt;this&lt;/code&gt;:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;proto&lt;/span&gt; = Object.getPrototypeOf

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(name:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets someone intimately if we know them, otherwise use&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;the generic greeting&lt;/span&gt;
mikhail.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;name&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; name == &lt;span style="color: #A6E32D;"&gt;'Kristin Wei&amp;#223;'&lt;/span&gt;?  &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Heya, Kristty'&lt;/span&gt;
         : &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;we dunno this guy */&lt;/span&gt;  proto(&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;).greet.call(&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;, name)
}

mikhail.greet(kristin.name)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: Heya, Kristty'&lt;/span&gt;

mikhail.greet(&lt;span style="color: #A6E32D;"&gt;'Margareth'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: Hello, Margareth'&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
This looks all good and well, but there's a little catch: it will enter
in endless recursion if you try to apply this approach to more than one
parent. This happens because the methods are always applied in the
context of the message's first target, making the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; lookup
resolve always to the same object:
&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/oop-super.png"  alt="http://dl.dropbox.com/u/4429200/blog/oop-super.png" /&gt;
&lt;/p&gt;
&lt;p&gt;
The simple solution to this, then, is to make all parent look-ups
static, by passing the object where the current function is stored,
rather than the object that the function was applied to.
&lt;/p&gt;
&lt;p&gt;
So, the last example becomes:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;proto&lt;/span&gt; = Object.getPrototypeOf

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(name:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets someone intimately if we know them, otherwise use&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;the generic greeting.&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;//&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Note that now we explicitly state that the lookup should take&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;the parent of `mikhail', so we can be assured the cyclic parent&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;resolution above won't happen.&lt;/span&gt;
mikhail.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;name&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; name == &lt;span style="color: #A6E32D;"&gt;'Kristin Wei&amp;#223;'&lt;/span&gt;?  &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Heya, Kristty'&lt;/span&gt;
         : &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;we dunno this guy */&lt;/span&gt;  proto(mikhail).greet.call(&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;, name)
}

mikhail.greet(kristin.name)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: Heya, Kristty'&lt;/span&gt;

mikhail.greet(&lt;span style="color: #A6E32D;"&gt;'Margareth'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Michael White: Hello, Margareth'&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
Still, this has quite some short-commings. First, since the object is
hard-coded in the function, we can't just assign the function to any
object and have it just work, as we did up 'till now. The function would
always resolve to the parent of &lt;code&gt;mikhail&lt;/code&gt;, not of the object where it's
stored.
&lt;/p&gt;
&lt;p&gt;
Likewise, we can't just apply the function to any object. The function
is not generic anymore. Unfortunately, though, making the parent
resolution dynamic would require us to pass an additional parameter to
every function call, which is something that can't be achieved short of
ugly hacks.
&lt;/p&gt;
&lt;p&gt;
The approach proposed for the next version of JavaScript only solves the
first problem, which is the easiest. Here, we'll do the same, by
introducing a new way of defining methods. Yes, methods, not generic
functions.
&lt;/p&gt;
&lt;p&gt;
Functions that need to access the properties in the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; will
require an additional information: the object where they are
stored. This makes the lookup static, but solves our cyclic lookup
problem.
&lt;/p&gt;
&lt;p&gt;
We do this by introducing a new function — &lt;code&gt;make_method&lt;/code&gt; — which creates
a function that passes this information to the target function.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(object:Object, fun:Function) &amp;#8594; Function&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Creates a method&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;make_method&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;object&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;fun&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() { &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;args&lt;/span&gt;
        args = slice.call(&lt;span style="color: #FA2573;"&gt;arguments&lt;/span&gt;)
        args.unshift(object)        &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;insert `object' as first parameter&lt;/span&gt;
        fn.apply(&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;, args) }
}


&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Now, all functions that are expected to be used as a method&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;should remember to reserve the first parameter to the object&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;where they're stored.&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;//&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Note that, however, this is a magical parameter introduced&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;by the method function, so any function calling the method&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;should pass only the usual arguments.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;message&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;self&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;message&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;parent&lt;/span&gt;
    parent = Object.getPrototypeOf(self)
    &lt;span style="color: #C48DFF;"&gt;if&lt;/span&gt; (parent &amp;amp;&amp;amp; parent.log)
        parent.log.call(&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;, message)

    console.log(&lt;span style="color: #A6E32D;"&gt;'-- At '&lt;/span&gt; + self.name)
    console.log(&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': '&lt;/span&gt; + message)
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Here we define a prototype chain C -&amp;gt; B -&amp;gt; A&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;A&lt;/span&gt;  = Object.create(&lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;)
A.name = &lt;span style="color: #A6E32D;"&gt;'A'&lt;/span&gt;
A.log  = make_method(A, message)

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;B&lt;/span&gt;  = Object.create(A)
B.name = &lt;span style="color: #A6E32D;"&gt;'B'&lt;/span&gt;
B.log  = make_method(B, message)

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;C&lt;/span&gt;  = Object.create(B)
C.name = &lt;span style="color: #A6E32D;"&gt;'C'&lt;/span&gt;
C.log  = make_method(C, message)

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;And we can test if it works by calling the methods:&lt;/span&gt;
A.log(&lt;span style="color: #A6E32D;"&gt;'foo'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; '-- At A'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'A: foo'&lt;/span&gt;

B.log(&lt;span style="color: #A6E32D;"&gt;'foo'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; '-- At A'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'B: foo'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; '-- At B'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'B: foo'&lt;/span&gt;

C.log(&lt;span style="color: #A6E32D;"&gt;'foo'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; '-- At A'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'C: foo'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; '-- At B'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'C: foo'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; '-- At C'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'C: foo'&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;4. Constructors&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;

&lt;p&gt;Constructor functions are the old pattern for creating objects in
JavaScript, which couple inheritance with initialisation in an
imperative manner.
&lt;/p&gt;
&lt;p&gt;
Constructor functions &lt;b&gt;are not&lt;/b&gt;, however, a special construct in the
language. Any simple function can be used as a constructor function;
just like &lt;code&gt;this&lt;/code&gt;, it all depends on how the function is called.
&lt;/p&gt;
&lt;p&gt;
So, what's it about constructor functions, really? Well, every function
object in JavaScript automatically gets a &lt;code&gt;prototype&lt;/code&gt; property, that is
a simple object with a &lt;code&gt;constructor&lt;/code&gt; property pointing back to the
constructor function. And this object is used to determine the
&lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; of instances created with that constructor function.
&lt;/p&gt;
&lt;p&gt;
The following diagram shows the objects for the constructor function
&lt;code&gt;function Person(first_name, last_name)&lt;/code&gt;:
&lt;/p&gt;


&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/oop-ctor.png"  alt="http://dl.dropbox.com/u/4429200/blog/oop-ctor.png" /&gt;
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-4-1" class="outline-3"&gt;
&lt;h3 id="sec-4-1"&gt;4.1. The &lt;code&gt;new&lt;/code&gt; magic&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-4-1"&gt;

&lt;p&gt;The &lt;code&gt;prototype&lt;/code&gt; &lt;i&gt;per se&lt;/i&gt; is not a special property, however it gains
special meaning when a constructor function is used in conjunction with
the &lt;code&gt;new&lt;/code&gt; statement. As I said before, in this case the &lt;code&gt;prototype&lt;/code&gt;
property of the constructor function is used to provide the
&lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; of the instance.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Constructs a new Person&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;Person&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;first_name&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;last_name&lt;/span&gt;) {
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;If the function is called with `new', as we expect&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;`this' here will be the freshly created object&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;with the [[Prototype]] set to Person.prototype&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;//&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Of course, if someone omits new when calling the&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;function, the usual resolution of `this' &amp;#8212; as&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;explained before &amp;#8212; will take place.&lt;/span&gt;
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.first_name = first_name
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.last_name  = last_name
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets the given person&lt;/span&gt;
Person.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Harro, '&lt;/span&gt; + person + &lt;span style="color: #A6E32D;"&gt;'.'&lt;/span&gt; 
}

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;person&lt;/span&gt; = &lt;span style="color: #C48DFF;"&gt;new&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Person&lt;/span&gt;(&lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'Wei&amp;#223;'&lt;/span&gt;)


&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;We could de-sugar the constructor pattern in the following&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Taking into account that `Person' here means the `prototype'&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;property of the `Person' constructor.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;Person&lt;/span&gt; = Object.create(Object.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;)

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets the given person&lt;/span&gt;
Person.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Harro, '&lt;/span&gt; + person + &lt;span style="color: #A6E32D;"&gt;'.'&lt;/span&gt; 
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Here's what the constructor does when called with `new'&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;person&lt;/span&gt; = Object.create(Person)
person.first_name = &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;
person.last_name  = &lt;span style="color: #A6E32D;"&gt;'Wei&amp;#223;'&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
When a function is called with the &lt;code&gt;new&lt;/code&gt; statement, the following magic
happens:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a fresh &lt;code&gt;Object&lt;/code&gt;, inheriting from &lt;code&gt;Object.prototype&lt;/code&gt;, say &lt;code&gt;{ }&lt;/code&gt;

&lt;/li&gt;
&lt;li&gt;Set the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; internal property of the new object to point
    to the constructor's &lt;code&gt;prototype&lt;/code&gt; property, so it inherits those
    behaviours.

&lt;/li&gt;
&lt;li&gt;Call the constructor in the context of this fresh object, such that
    &lt;code&gt;this&lt;/code&gt; inside the constructor will be the fresh object, and pass any
    parameters given to the function.

&lt;/li&gt;
&lt;li&gt;If the function returns an &lt;code&gt;Object&lt;/code&gt;, make that be the return value of
    the function.

&lt;/li&gt;
&lt;li&gt;Otherwise, return the fresh object.
&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;
This means that the resulting value of calling a &lt;code&gt;function&lt;/code&gt; with the
&lt;code&gt;new&lt;/code&gt; operator is not necessarily the object that was created. A
function is free to return any other &lt;code&gt;Object&lt;/code&gt; value as it sees fit. This
is an interesting and — to a certain extent — powerful behaviour, but
also a confusing one for many newcomers:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;Foo&lt;/span&gt;() {
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.foo = &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt;
}

&lt;span style="color: #C48DFF;"&gt;new&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Foo&lt;/span&gt;()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; { foo: 'bar' }&lt;/span&gt;


&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;Foo&lt;/span&gt;() {
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.foo = &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt;
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; Foo
}

&lt;span style="color: #C48DFF;"&gt;new&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Foo&lt;/span&gt;()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; [Function: Foo]&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4-2" class="outline-3"&gt;
&lt;h3 id="sec-4-2"&gt;4.2. Inheritance with constructors&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-4-2"&gt;

&lt;p&gt;We've covered inheritance with plain objects through &lt;code&gt;Object.create&lt;/code&gt;,
inheritance with constructors follow quite naturally from there, the
difference being that instead of the main actor being the target of the
inheritance (the constructor function, in this case), the &lt;code&gt;prototype&lt;/code&gt;
property is:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;new Person (first_name:String, last_name:String)&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Initialises a Person object&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;Person&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;first_name&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;last_name&lt;/span&gt;) {
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.first_name = first_name
    &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.last_name  = last_name
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Defines the `name' getter/setter&lt;/span&gt;
Object.defineProperty(Person.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'name'&lt;/span&gt;, { get: get_full_name
                                                , set: set_full_name
                                                , configurable: &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt;
                                                , enumerable:   &lt;span style="color: #FA2573;"&gt;true&lt;/span&gt; })

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Greets the given person&lt;/span&gt;
Person.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': Hello, '&lt;/span&gt; + (person || &lt;span style="color: #A6E32D;"&gt;'you'&lt;/span&gt;)
}


&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;proto&lt;/span&gt; = Object.getPrototypeOf

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;new Mikhail (age:Number, gender:String)&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;Mikhail&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;age&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;gender&lt;/span&gt;) {
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Find the parent of this object and invoke its constructor&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;with the current this. We could have used:&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;//   &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Person.call(this, 'Mikhail', 'Wei&amp;#223;')&lt;/span&gt;
    &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;But we'd lose some flexibility with that.&lt;/span&gt;
    proto(Mikhail.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;).constructor.call(&lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'Mikhail'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'Wei&amp;#223;'&lt;/span&gt;)
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Inherits the properties from Person.prototype&lt;/span&gt;
Mikhail.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt; = Object.create(Person.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;)

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Resets the `constructor' property of the prototype object&lt;/span&gt;
Mikhail.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;.constructor = Mikhail

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(person:String) &amp;#8594; String&lt;/span&gt;
Mikhail.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;.greet = &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;person&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.name + &lt;span style="color: #A6E32D;"&gt;': \'sup, '&lt;/span&gt; + (person || &lt;span style="color: #A6E32D;"&gt;'dude'&lt;/span&gt;)
}


&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Instances are created with the `new' operator, as previously&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;discussed:&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;mikhail&lt;/span&gt; = &lt;span style="color: #C48DFF;"&gt;new&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Mikhail&lt;/span&gt;(19, &lt;span style="color: #A6E32D;"&gt;'Male'&lt;/span&gt;)
mikhail.greet(&lt;span style="color: #A6E32D;"&gt;'Kristin'&lt;/span&gt;)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'Mikhail Wei&amp;#223;: \'sup, Kristin'&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5" class="outline-2"&gt;
&lt;h2 id="sec-5"&gt;5. Considerations and compatibility&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;

&lt;p&gt;The functions and concepts presented up until now assumed that the code
would be running in an ECMAScript 5 environment, since the new additions
make prototypical inheritance more natural, without the initialisation
and inheritance coupling provided by constructor functions.
&lt;/p&gt;
&lt;p&gt;
However, obviously this means that code using these functions will not
work everywhere. &lt;a href="http://twitter.com/kangax"&gt;@kangax&lt;/a&gt; has a most awesome &lt;a href="http://kangax.github.com/es5-compat-table/"&gt;compatibility table&lt;/a&gt; for the
implementations that follow ECMAScript 5.
&lt;/p&gt;
&lt;p&gt;
This section provides fallbacks to some of the functionality, and point
to libraries that implement these fallbacks so you don't get to reinvent
the wheel. Note that this section only exists to highlight how the
functionality works, and how the core part of those behaviours could be
reproduced in legacy code, it's not meant to provide ready-to-use
fallbacks. Use libraries for that :3
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-5-1" class="outline-3"&gt;
&lt;h3 id="sec-5-1"&gt;5.1. Creating objects&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-5-1"&gt;

&lt;p&gt;In ECMAScript 5 we have got &lt;code&gt;Object.create&lt;/code&gt; to handle inheritance, but
constructor functions can also be used to set the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; link
for the constructed object — which is what we're interested about.
&lt;/p&gt;
&lt;p&gt;
A &lt;code&gt;clone&lt;/code&gt; function could be defined such that it would create a new
object based on the given prototype:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(proto:Object) &amp;#8594; Object&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Constructs an object and sets the [[Prototype]] to `proto'.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;clone&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;proto&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;Dummy&lt;/span&gt;() { }

    Dummy.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;             = proto
    Dummy.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;.constructor = Dummy

    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #C48DFF;"&gt;new&lt;/span&gt; &lt;span style="color: #FC951E;"&gt;Dummy&lt;/span&gt;()
}

&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;mikhail&lt;/span&gt; = clone(person)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Equivalent to `var mikhail = Object.create(person)'&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5-2" class="outline-3"&gt;
&lt;h3 id="sec-5-2"&gt;5.2. Defining properties&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-5-2"&gt;

&lt;p&gt;&lt;code&gt;Object.defineProperty&lt;/code&gt; and it's batch cousin &lt;code&gt;Object.defineProperties&lt;/code&gt;
are also new additions, and they allow properties to be defined with
internal tags, like &lt;code&gt;writable&lt;/code&gt;, &lt;code&gt;configurable&lt;/code&gt; and &lt;code&gt;enumerable&lt;/code&gt;. It's
not possible to get this behaviour in the older versions of the
language.
&lt;/p&gt;
&lt;p&gt;
All properties defined otherwise will inevitable have &lt;code&gt;writable&lt;/code&gt;,
&lt;code&gt;configurable&lt;/code&gt; and &lt;code&gt;enumerable&lt;/code&gt; set to true, which is usually not really
that much of a problem — still, not compatible with full ES5 code.
&lt;/p&gt;
&lt;p&gt;
In regards of getters and setters, they are supported to a certain
extent with non-standard syntax — the &lt;code&gt;__defineGetter__&lt;/code&gt; and
&lt;code&gt;__defineSetter__&lt;/code&gt; methods, — but are also not available
everywhere. Most notably, such methods have never been present in IE.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(target:Object, key:String, descriptor:Object) &amp;#8594; Object&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Defines a property in the target object.&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Getters and Setters are handled through the fallback&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;calls, whereas values are set directly. Tags are&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;ignored.&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;defineProperty&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;target&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;key&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;descriptor&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;if&lt;/span&gt; (descriptor.value)
        target[key] = descriptor.value
    &lt;span style="color: #C48DFF;"&gt;else&lt;/span&gt; {
        descriptor.get &amp;amp;&amp;amp; target.__defineGetter__(key, descriptor.get)
        descriptor.set &amp;amp;&amp;amp; target.__defineSetter__(key, descriptor.set) }

    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; target
}


&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;x&lt;/span&gt; = { }
defineProperty(x, &lt;span style="color: #A6E32D;"&gt;'foo'&lt;/span&gt;, { value: &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt; })
defineProperty(x, &lt;span style="color: #A6E32D;"&gt;'bar'&lt;/span&gt;, { get: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.foo }
                         , set: &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;v&lt;/span&gt;){ &lt;span style="color: #FA2573;"&gt;this&lt;/span&gt;.foo = v    }})

x.foo
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'bar'&lt;/span&gt;

x.bar
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'bar'&lt;/span&gt;

x.bar = &lt;span style="color: #A6E32D;"&gt;'foo'&lt;/span&gt;
x.foo
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'foo'&lt;/span&gt;

x.bar
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 'foo' &lt;/span&gt;
&lt;/pre&gt;




&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5-3" class="outline-3"&gt;
&lt;h3 id="sec-5-3"&gt;5.3. Listing properties&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-5-3"&gt;

&lt;p&gt;We have seen how it's possible to list the properties of an object with
&lt;code&gt;Object.getOwnPropertyNames&lt;/code&gt;, and list only the enumerable properties
through &lt;code&gt;Object.keys&lt;/code&gt;. Well, prior to ECMAScript 5, listing the
enumerable properties is the only thing one can do.
&lt;/p&gt;
&lt;p&gt;
This is achieved through the &lt;code&gt;for..in&lt;/code&gt; statement, which iterates through
all the enumerable properties of an object, either directly set in the
object, or in the prototype chain. &lt;code&gt;Object.prototype.hasOwnProperty&lt;/code&gt; may
be used to filter the properties to include only the ones set directly
in the object.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(object:Object) &amp;#8594; Array&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Lists all the own enumerable properties of an object&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;keys&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;object&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;result&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;key&lt;/span&gt;
    result = []
    &lt;span style="color: #C48DFF;"&gt;for&lt;/span&gt; (key &lt;span style="color: #C48DFF;"&gt;in&lt;/span&gt; object)
        &lt;span style="color: #C48DFF;"&gt;if&lt;/span&gt; (object.hasOwnProperty(key))  result.push(key)

    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; result
}

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Taking the mikhail object whose [[Prototype]] is person...&lt;/span&gt;
keys(mikhail)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; [ 'first_name', 'last_name', 'age', 'gender' ]&lt;/span&gt;

keys(person)
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; [ 'greet', 'name' ]&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5-4" class="outline-3"&gt;
&lt;h3 id="sec-5-4"&gt;5.4. Bound methods&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-5-4"&gt;

&lt;p&gt;Bound methods in JavaScript do much more than just assert the value of
&lt;code&gt;this&lt;/code&gt; inside a function, they can also be used for partial function
applications and behave slightly different when called as a
constructor. For this, we'll just focus on the first two.
&lt;/p&gt;
&lt;p&gt;
Basically, when calling the &lt;code&gt;bind&lt;/code&gt; method of a function, we're creating
a new function object that has a defined &lt;code&gt;thisObject&lt;/code&gt; and perhaps a
defined initial list of arguments. This can be just as well achieved
with a closure to store the given state, and a explicit function
application, through the &lt;code&gt;apply&lt;/code&gt; method.
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;slice&lt;/span&gt; = [].slice

&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;(fun:Function, bound_this:Object, args...) &amp;#8594; Function&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;//  &lt;/span&gt;&lt;span style="color: #75766A;"&gt;--&amp;gt; (args...) &amp;#8594; *mixed*&lt;/span&gt;
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Creates a bound method from the function `fn'&lt;/span&gt;
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;bind&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;fn&lt;/span&gt;, &lt;span style="color: #729FCF;"&gt;bound_this&lt;/span&gt;) { &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;bound_args&lt;/span&gt;
    bound_args = slice.call(&lt;span style="color: #FA2573;"&gt;arguments&lt;/span&gt;, 2)
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;() { &lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;args&lt;/span&gt;
        args = bound_args.concat(slice.call(&lt;span style="color: #FA2573;"&gt;arguments&lt;/span&gt;))
        &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; fn.apply(bound_this, args) }
}
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5-5" class="outline-3"&gt;
&lt;h3 id="sec-5-5"&gt;5.5. Getting the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt;&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-5-5"&gt;

&lt;p&gt;For accessing overriden properties, we need to get the a reference to
the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt;. In environments that expose such link (like
Firefox's &lt;i&gt;SpiderMonkey&lt;/i&gt; or Chrome's &lt;i&gt;V8&lt;/i&gt;), it's easy and reliable:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;proto&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;object&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; object?            object.__proto__
         : &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;not object? */&lt;/span&gt;  &lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;
}
&lt;/pre&gt;


&lt;p&gt;
However, in environments that don't expose the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; link,
things aren't quite as reliable. The only way of getting the prototype
of an object, in this case, would be by the constructor's &lt;code&gt;prototype&lt;/code&gt;
property, but we can only access that from the object given the
&lt;code&gt;constructor&lt;/code&gt; property is kept intact.
&lt;/p&gt;
&lt;p&gt;
A fallback covering most cases would look like this:
&lt;/p&gt;



&lt;pre class="src src-js"&gt;&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;proto&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;object&lt;/span&gt;) {
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; !object?                &lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;
         : &lt;span style="color: #A6E32D;"&gt;'__proto__'&lt;/span&gt; &lt;span style="color: #C48DFF;"&gt;in&lt;/span&gt; object?  object.__proto__
         : &lt;span style="color: #75766A;"&gt;/* &lt;/span&gt;&lt;span style="color: #75766A;"&gt;not exposed? */&lt;/span&gt;      object.constructor.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;
}
&lt;/pre&gt;


&lt;p&gt;
Note that the actual &lt;code&gt;Object.getPrototypeOf&lt;/code&gt; throws a &lt;code&gt;TypeError&lt;/code&gt; when
you pass something that is not an object to it.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5-6" class="outline-3"&gt;
&lt;h3 id="sec-5-6"&gt;5.6. Libraries that provide fallbacks&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-5-6"&gt;


&lt;p&gt;
&lt;a href="https://github.com/kriskowal/es5-shim"&gt;ES5-shim&lt;/a&gt; attempts to implement fallbacks for ECMAScript 5 functionality
that can be done in pure JavaScript, whilst adding minimal support
for the other ones. It's important to note, however, that the
fallbacks are intended to provide equivalent functionality that is
close to the ones defined in the specs, it's not guaranteed that they
will work exactly the same way.
&lt;/p&gt;
&lt;p&gt;
To quote the &lt;code&gt;README&lt;/code&gt;:
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;"As closely as possible to ES5" is not very close. Many of these shims
are intended only to allow code to be written to ES5 without causing
run-time errors in older engines. In many cases, this means that
these shims cause many ES5 methods to silently fail. Decide carefully
whether this is what you want. 
&lt;/p&gt;
&lt;/blockquote&gt;



&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-6" class="outline-2"&gt;
&lt;h2 id="sec-6"&gt;6. Wrapping it up&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-6"&gt;

&lt;p&gt;The object orientation model chosen for JavaScript is definitely one of
the things that makes the language expressive and powerful, however the
really poor semantics from the before-ES5 age quite killed all the fun
about it.
&lt;/p&gt;
&lt;p&gt;
With ECMAScript 5, we have got better ways to deal with objects and
inheritance, but most of the API is pretty verbose and awkward to use
out of the box, so abstracting them is the only sane way of exploring
all the power of the first-class inheritance model provided by the
language.
&lt;/p&gt;
&lt;p&gt;
Once you dwell on the depths of JavaScript's prototypical object
orientation, however, you will find it lacking on aspects that would
otherwise seem like the obvious thing to do — like multiple inheritance
and message resending, but also basic features like an easier object
extension functionality.
&lt;/p&gt;
&lt;p&gt;
Luckily most of these issues manage to have a solution, albeit not
necessarily a satisfactory one in some cases — i.e.: manual
mixins. Being able to reproduce semantics that are not provided straight
away on the language by patterns leveraging the built-in constructs is
an important part of the language, and this all is made easier because
of the way functions are treated in JavaScript.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-7" class="outline-2"&gt;
&lt;h2 id="sec-7"&gt;7. Things worth reading up next&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-7"&gt;


&lt;br&gt;

&lt;dl&gt;
&lt;dt&gt;&lt;a href="http://www.aminutewithbrendan.com/pages/20110216"&gt;Brendan Eich's "Prototypical vs Closure" rant&lt;/a&gt;&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Although not really a reading, this particular podcast from Brendan
  Eich is a must listen for anyone working with object oriented
  JavaScript. it delves on the performance of engines in regards of
  object construction, highlighting how the prototypical pattern stands
  against the &lt;a href="http://yuiblog.com/blog/2007/06/12/module-pattern/"&gt;Closure pattern&lt;/a&gt;, and discussing the specifics of how
  browsers handle prototypical code so they run &lt;b&gt;fast&lt;/b&gt;.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;dl&gt;
&lt;dt&gt;&lt;a href="http://labs.oracle.com/self/papers/organizing-programs.html"&gt;Organizing Programs Without Classes&lt;/a&gt; &lt;i&gt;(paper)&lt;/i&gt;&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Albeit not specific to JavaScript, this white-paper dwells on how the
  structuring of programs differ from the classical object orientation
  approach to the prototypical take on the subject. It provides lots of
  &lt;a href="http://selflanguage.org/"&gt;Self&lt;/a&gt; code to go with it, but they are more or less easily translated
  to JavaScript code.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-8" class="outline-2"&gt;
&lt;h2 id="sec-8"&gt;8. Acknowledgements&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-8"&gt;


&lt;p&gt;
Thanks to &lt;a href="https://github.com/hughfdjackson"&gt;@hughfdjackson&lt;/a&gt;, Alex and Taylor for the additional revision of
the article.
&lt;/p&gt;
&lt;p&gt;
Thanks to &lt;code&gt;--&lt;/code&gt; for pointing out (in the comments) that properties expect
an &lt;code&gt;IdentifierName&lt;/code&gt;, rather than a plain &lt;code&gt;Identifier&lt;/code&gt;, which would allow
reserved words (like &lt;code&gt;foo.class&lt;/code&gt; or &lt;code&gt;foo.null&lt;/code&gt;) to be used unquoted.
&lt;/p&gt;
&lt;div id="footnotes"&gt;
&lt;h2 class="footnotes"&gt;Footnotes: &lt;/h2&gt;
&lt;div id="text-footnotes"&gt;
&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.1" href="#fnr.1"&gt;1&lt;/a&gt;&lt;/sup&gt; : Some implementations have magical names, like &lt;code&gt;__proto__&lt;/code&gt;, which
        may yield undesired and unwanted results when set. For example,
        the &lt;code&gt;__proto__&lt;/code&gt; property is used to define the parent of an object
        in some implementations. As such, you wouldn't be able to set a
        string or number to that.
&lt;/p&gt;

&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.2" href="#fnr.2"&gt;2&lt;/a&gt;&lt;/sup&gt; : While an &lt;code&gt;IdentifierName&lt;/code&gt; also allows reserved words, you should
        be aware that ECMAScript engines that aren't fully compliant
        with the ECMAScript 5 specs &lt;b&gt;may&lt;/b&gt; choke on reserved words when
        they're used for property access unquoted.
&lt;/p&gt;

&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.3" href="#fnr.3"&gt;3&lt;/a&gt;&lt;/sup&gt; : It should be noted that, while ECMAScript-defined native objects
        don't throw an error when you try to access a non-existing
        property, it's not guaranteed that the same will hold true for a
        host object (i.e. an object defined by the engine implementor). After all, 
    host object semantics are not defined, they are dependent on the 
    particular run-time implementation.
&lt;/p&gt;

&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.4" href="#fnr.4"&gt;4&lt;/a&gt;&lt;/sup&gt; Some engines &lt;b&gt;do&lt;/b&gt; expose the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; slot, usually
       through a property like &lt;code&gt;__proto__&lt;/code&gt;, however no such thing is
       described in the specifications for the language, so it's
       recommended that you avoid using it, unless you're well aware
       that all platforms you code must run on will have such means of
       setting the &lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt; object directly. It should also be
       noted that messing with the prototype chain might defeat all
       look-up optimisations in the JS engine.
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>The Dark-Side of JavaScript</title>
   <link href="http://killdream.github.com//blog/2011/08/the-dark-side-of-javascript/index.html" />
   <updated>2011-08-15T00:00:00-07:00</updated>
   <id>http://killdream.github.com//blog/2011/08/the-dark-side-of-javascript/the-dark-side-of-javascript</id>
   <content type="html">&lt;p&gt;
&lt;a href="http://github.com/killdream/black"&gt;Black&lt;/a&gt; is an extension to JavaScript's standard objects, providing
missing functionality, a few aliases and making using generic functions
sweeter.
&lt;/p&gt;
&lt;p&gt;
It supports both functional style:
&lt;/p&gt;



&lt;pre class="src src-javascript"&gt;black.unpack_all([&lt;span style="color: #A6E32D;"&gt;'utils'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'generic'&lt;/span&gt;])
each(map(filter(links, selectedp)
        ,text)
    ,display)
&lt;/pre&gt;


&lt;p&gt;        
And Object Oriented style:
&lt;/p&gt;



&lt;pre class="src src-javascript"&gt;black.unpack_all([&lt;span style="color: #A6E32D;"&gt;'own'&lt;/span&gt;])
black.list.to_array(links).filter(selectedp)
                          .map(text)
                          .each(display)
&lt;/pre&gt;


&lt;p&gt;
And it can use either the global scope or patch the native object's
prototype directly, if &lt;b&gt;you&lt;/b&gt; decide to do so.
&lt;/p&gt;
&lt;p&gt;
If you're on Node.js, you can just install it from npm:
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ npm install black
&lt;/pre&gt;


&lt;p&gt;    
Otherwise, clone the github repository and follow the
&lt;a href="http://killdream.github.com/black/docs/deploy/overview/installing.html"&gt;installation instructions&lt;/a&gt;.
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ git clone htp://github.com/killdream/black.git

&lt;span style="color: #75766A;"&gt;# &lt;/span&gt;&lt;span style="color: #75766A;"&gt;Or if you use hg-git, like me:&lt;/span&gt;
$ hg clone http://github.com/killdream/black.git
&lt;/pre&gt;




&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;The issues&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
Black didn't just sprung out of nowhere, it came into existence to fix
several issues I've had while writing JavaScript code. 
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-1-1" class="outline-3"&gt;
&lt;h3 id="sec-1-1"&gt;Lack of functionality and generic sugar&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-1"&gt;


&lt;p&gt;
The main issue was that I often had to write the same functions over and over
again, because the standard library didn't provide them.
&lt;/p&gt;
&lt;p&gt;
That goes from the simple object handling, that is, merging two objects, from
sequence handling, which I do use &lt;b&gt;a lot&lt;/b&gt;. On this release, I've tried to
include the features I miss the most, for numbers, functions, objects and
sequences.
&lt;/p&gt;
&lt;p&gt;
Yes, &lt;b&gt;sequences&lt;/b&gt;, not &lt;b&gt;Arrays&lt;/b&gt;. Since JavaScript is weakly-typed to the very
core, mostly functionality is intentionally generic. However, using those
generic functions on non-arrays is usually a huge. pain. in. the. ass:
&lt;/p&gt;



&lt;pre class="src src-javascript"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;seq&lt;/span&gt; = {0: 1, 1: 2, 2: 3, length: 3}
Array.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;.map.call(seq, &lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;x&lt;/span&gt;){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; x + 1 })
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; [2, 3, 4]&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;    
That unnecessary verbosity really annoys me. Other thing that does so is the
inconsistency in own and generic methods. You can't just have
&lt;code&gt;Array.slice(sequence)&lt;/code&gt;, even though the method is supposed to be generic, you
need to apply an Array's own method to another object type if you need it. For
me, it just makes no sense.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-2" class="outline-3"&gt;
&lt;h3 id="sec-1-2"&gt;Verbosity and hatefulCamelCaseIsHateful&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-2"&gt;


&lt;p&gt;
The second major issue I have is the naming convention used in
JavaScript. Sorry guys, but camelCase sucks way too much to be taken
seriously. And on top of that, lots of functionality have unnecessary and
overly verbose names.
&lt;/p&gt;
&lt;p&gt;
Note that when I say &lt;i&gt;verbose&lt;/i&gt; I don't necessarily mean &lt;b&gt;long&lt;/b&gt;. There's a huge
difference here. Verbosity is the unnecessary extension of a name just for the
sake of having a long name. Descriptive and concise names are your friends when
you actually want to read a piece of code.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-3" class="outline-3"&gt;
&lt;h3 id="sec-1-3"&gt;Aliasing hell&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-3"&gt;


&lt;p&gt;
One thing I noticed a lot lately, is that all my JavaScript files were
starting with the following:
&lt;/p&gt;



&lt;pre class="src src-javascript"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;map&lt;/span&gt;    = [].map
  , each   = [].each
  , some   = [].some
  , every  = [].every
  , reduce = [].reduce
  , filter = [].filter
  , slice  = [].slice
  , hasp   = {}.hasOwnProperty
  , ( ... )
&lt;/pre&gt;


&lt;p&gt;
Things were coming to a point that I had written a template in &lt;a href="http://code.google.com/p/yasnippet"&gt;Yasnippet&lt;/a&gt; to
include all those lines for me. But well, even so, it was still quite
unwielding and I still raged every time I saw that shitload of aliases at the
start of every file (things only got worse when the file used external
libraries, btw).
&lt;/p&gt;
&lt;p&gt;
If only current ECMAScript had &lt;a href="https://developer.mozilla.org/En/New_in_JavaScript_1.7#Destructuring_assignment_(Merge_into_own_page.2fsection)"&gt;destructuring assignment&lt;/a&gt; support, things
wouldn't be as bad:
&lt;/p&gt;



&lt;pre class="src src-javascript"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; {map, each, reduce, filter} = Array.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;    
Which is not that much different from Python's:
&lt;/p&gt;



&lt;pre class="src src-python"&gt;&lt;span style="color: #C48DFF;"&gt;from&lt;/span&gt; module &lt;span style="color: #C48DFF;"&gt;import&lt;/span&gt; x, y, z
&lt;/pre&gt;


&lt;p&gt;    
Except you can import anything you want from any object. Having written some
code for SpiderMonkey-only projects before, I can say that JavaScript 1.7
introduces quite a lot of awesomeness I missed in the language for a long time
(and still miss&amp;hellip;)
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;What Black provides&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;



&lt;/div&gt;

&lt;div id="outline-container-2-1" class="outline-3"&gt;
&lt;h3 id="sec-2-1"&gt;Extensions&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-1"&gt;


&lt;p&gt;
The main point of Black is to include extensions for working with the
standard objects. Most of the extensions are for working with sequences,
and objects. Extensions are logically divided in modules, and can be
used separately.
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;type&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Provides type, interface and functionality testing.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;obj&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Handles objects as sets of key/values. This includes extracting
  lists of keys and values, cherry-picking and transforming them.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;list&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Handles iteration and manipulation of sequence's structure and
  items.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;str&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Provides generic aliases for built-in utilities and sugar for
  JS/CSS interop.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;num&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Provides a few mathematical functions for boundary handling and
  padding.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;fn&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Provides a few functional utilities. Currently not much useful.
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-2" class="outline-3"&gt;
&lt;h3 id="sec-2-2"&gt;Sane API naming&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-2"&gt;


&lt;p&gt;
Black's API uses a naming convention derived from Python and Lisp. Words
are written in all lower-case and separated by underscores. Names are
kept concise and descriptive.
&lt;/p&gt;
&lt;p&gt;
From Common Lisp, it uses the &lt;code&gt;p&lt;/code&gt;-suffix naming convention for predicate
functions (like &lt;code&gt;nullp&lt;/code&gt; for &lt;code&gt;null?&lt;/code&gt; or &lt;code&gt;isNull&lt;/code&gt;). And &lt;code&gt;n&lt;/code&gt;-prefix for
destructive functions when a pure alternative exists.
&lt;/p&gt;
&lt;p&gt;
For functions that construct objects, a &lt;code&gt;make_&lt;/code&gt; prefix is used.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2-3" class="outline-3"&gt;
&lt;h3 id="sec-2-3"&gt;Patching&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-2-3"&gt;


&lt;p&gt;
The actual thing about Black, though, is how it handles the aliasing
hell issue. The &lt;code&gt;core&lt;/code&gt; module provides unpacker functions that copies
properties from an Object to another one. Modules decide which
functionality they want to export and where they want it exported by
default.
&lt;/p&gt;
&lt;p&gt;
The unpackers separate functionality into &lt;code&gt;utils&lt;/code&gt;, which are exported to
the global object; &lt;code&gt;generic&lt;/code&gt;, which are exported directly into the
constructor function; and &lt;code&gt;own&lt;/code&gt;, which are exported into the
&lt;code&gt;[⁣[Prototype]⁣]&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
This patching is carried by two functions, the &lt;i&gt;just-do-the-right-thing(tm)&lt;/i&gt;
one, &lt;code&gt;unpack_all&lt;/code&gt;, which will follow the defaults the modules have provided:
&lt;/p&gt;



&lt;pre class="src src-javascript"&gt;black.unpack_all([&lt;span style="color: #A6E32D;"&gt;'utils'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'generic'&lt;/span&gt;, &lt;span style="color: #A6E32D;"&gt;'own'&lt;/span&gt;])
&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;factor&lt;/span&gt; = 2
first(range(1, 10).filter(&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;x&lt;/span&gt;){
    &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; x &amp;gt; 3 * factor }))
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 7&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;    
And the fine-tuned one, if you want control over where you want your
stuff, &lt;code&gt;unpack&lt;/code&gt;:
&lt;/p&gt;



&lt;pre class="src src-javascript"&gt;&lt;span style="color: #C48DFF;"&gt;var&lt;/span&gt; &lt;span style="color: #729FCF;"&gt;numbers&lt;/span&gt; = [1, 2, 3, 4, 5]
black.unpack([&lt;span style="color: #A6E32D;"&gt;'own'&lt;/span&gt;], &lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;              &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;global target&lt;/span&gt;
                    , &lt;span style="color: #FA2573;"&gt;null&lt;/span&gt;              &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;generic target&lt;/span&gt;
                    , Array.&lt;span style="color: #FA2573;"&gt;prototype&lt;/span&gt;   &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;own&lt;/span&gt;
                    , black.list )      &lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;source&lt;/span&gt;

&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;squared&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;x&lt;/span&gt;){ &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; x * x      }
&lt;span style="color: #C48DFF;"&gt;function&lt;/span&gt; &lt;span style="color: #67D9F0;"&gt;evenp&lt;/span&gt;(&lt;span style="color: #729FCF;"&gt;x&lt;/span&gt;)  { &lt;span style="color: #C48DFF;"&gt;return&lt;/span&gt; x % 2 == 0 }
numbers.map(squared).filter(evenp).first()
&lt;span style="color: #75766A;"&gt;// &lt;/span&gt;&lt;span style="color: #75766A;"&gt;=&amp;gt; 4&lt;/span&gt;
&lt;/pre&gt;


&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;Wrapping it up&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
This is still an early release of the library, it's usable, but not
thoroughly tested (and all tests have been done only on
Node.js). Though, it has some documentation and examples, and most
functionality I wanted for now, so I'm just making an early release.
&lt;/p&gt;
&lt;p&gt;
I'm planning checking for set difference in the unpackers to avoid
silently writing existing functionality, associative sequence handling,
and other stuff, so well, still lots of work to do :3
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Microframeworks and the state of the web</title>
   <link href="http://killdream.github.com//blog/2011/05/microframeworks-and-the-web-state/index.html" />
   <updated>2011-05-22T00:00:00-07:00</updated>
   <id>http://killdream.github.com//blog/2011/05/microframeworks-and-the-web-state/microframeworks-and-the-web-state</id>
   <content type="html">&lt;p&gt;
Micro-"&lt;i&gt;frameworks&lt;/i&gt;"&lt;sup&gt;&lt;a class="footref" name="fnr.1" href="#fn.1"&gt;1&lt;/a&gt;&lt;/sup&gt; have been quite a hot topic lately on the
JavaScript community. And by &lt;i&gt;hot&lt;/i&gt;, I mean that it has lead to some
pretty "heated" discussion (either good ones and "bad" ones), and some
&lt;a href="http://allyoucanleet.com/2011/05/09/microlibs-the-fud-challenge/"&gt;pretty interesting&lt;/a&gt; &lt;a href="http://tomdale.net/2011/05/an-uphill-battle/"&gt;replies&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
But what's all this fuzz about? Well, the &lt;i&gt;microlibs&lt;/i&gt; introduce a shift
from the monolithic libraries (think &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;, &lt;a href="http://www.prototypejs.org/"&gt;Prototype&lt;/a&gt;, etc), to
self-contained and "modular" ones.
&lt;/p&gt;
&lt;p&gt;
In this post I'll try to explain what's good and bad about either approaches,
delve a little on modular design and sum up what I think that's wrong with most
JS libs, and where we should go from here.
&lt;/p&gt;

&lt;p&gt;
&lt;b&gt;Note:&lt;/b&gt;
&lt;/p&gt;&lt;blockquote&gt;

&lt;p&gt;I'm going to use &lt;b&gt;microlibs&lt;/b&gt; thorough this article to refer to what
people call micro-"frameworks"&lt;sup&gt;&lt;a class="footref" name="fnr.1.2" href="#fn.1"&gt;1&lt;/a&gt;&lt;/sup&gt; as I think the name fits better
what they actually are.
&lt;/p&gt;
&lt;/blockquote&gt;



&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;What's wrong with JS frameworks&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
So you have a new web app to develop, and you're looking for some
building blocks because you don't want to write everything from the
scratch?
&lt;/p&gt;
&lt;p&gt;
Toughest luck, my dear.
&lt;/p&gt;
&lt;p&gt;
Welcome to the "Reproducing desktop's UI toolkit mess in the JavaScript
world" show, only worse. A thousand times worse!
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-1-1" class="outline-3"&gt;
&lt;h3 id="sec-1-1"&gt;1. Feature bloat&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-1"&gt;


&lt;p&gt;
If you take &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;, which weights around 230kb (31kb minified AND
gzipped), you will get all the following goodies out of the box:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;CSS Selector engine
&lt;/li&gt;
&lt;li&gt;DOM abstraction API
&lt;/li&gt;
&lt;li&gt;Deferreds
&lt;/li&gt;
&lt;li&gt;Ajax/XMLHttpRequest abstractions
&lt;/li&gt;
&lt;li&gt;JSONP
&lt;/li&gt;
&lt;li&gt;CSS helpers
&lt;/li&gt;
&lt;li&gt;Animation/Effects
&lt;/li&gt;
&lt;li&gt;Event abstraction
&lt;/li&gt;
&lt;li&gt;Some other small utilities to fill in some standard lib "gaps"
&lt;/li&gt;
&lt;li&gt;Lots of cross-browser hacks
&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;
That is, obviously, overwhelming. People usually don't use all of the
features the library provide, and the overhead of this "dead code" feels
even larger because you have tons of them.
&lt;/p&gt;
&lt;p&gt;
The problems are also solved at a reduced scope, as you'd otherwise have
an unwielding codebase/filesize. For example, the effects included in
jQuery only cover the most basic use-cases, whereas other features have
a lot more of coverage — DOM abstraction.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-2" class="outline-3"&gt;
&lt;h3 id="sec-1-2"&gt;2. Large filesize&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-2"&gt;


&lt;p&gt;
Having more code equals having a larger filesize, which is a
problem in web apps because you'll be sending a lot of dead code over
the wire.
&lt;/p&gt;
&lt;p&gt;
This is bad from both the end-user's perspective: pages will take more
time to load, and this hurts a good user experience. No one likes
waiting 2 minutes to see a web site.
&lt;/p&gt;
&lt;p&gt;
And from an efficiency's perspective. You should be very worried about
sending lots of code that will never be used, sir. Seriously.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-3" class="outline-3"&gt;
&lt;h3 id="sec-1-3"&gt;3. Higher learning curve&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-3"&gt;


&lt;p&gt;
As you have more features, you'll take longer to master them. This is
not really that much of a problem on the end-user dev, because you would
probably have to learn the same amount of things by stacking lots of
small libraries together.
&lt;/p&gt;
&lt;p&gt;
However, the thing is that it'll take far more time to understand all
the implications of the source code to extend it. Whereas with a simpler
and focused library, it would be quite straight forward.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-4" class="outline-3"&gt;
&lt;h3 id="sec-1-4"&gt;4. Framework-centric code&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-4"&gt;


&lt;p&gt;
But all these minor annoyances aside, there's one big issue with the way
most JavaScript frameworks are designed: they encourage you to write
Framework-centric code.
&lt;/p&gt;
&lt;p&gt;
Want to write a date handling library? You should tie the core workings
to the jQuery's DOM manipulation functions. What about a library that
abstracts browser storage? TIE IT TO THE FRAMEWORK'S DOM HANDLING LIKE A
BOSS!
&lt;/p&gt;
&lt;p&gt;
Mind you, the browser is not just about the DOM, or a framework. Writing
framework-centric code when your library does NOT extend on the problem
the framework is trying to solve is a silly thing.
&lt;/p&gt;
&lt;p&gt;
The community can't benefit from your contribution as a whole because
they can't include the DOM framework you're using in their
projects. Either because doubling the overhead with code that solves the
same problem should warrant you a death sentence, and because mixing DOM
libraries may break everything in some cases (Prototype + jQuery, for
example).
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;Microlibs to the rescue!&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
As a response to that, wild microlibs appeared. The main design goal of
these libraries is to focus on doing just a single thing — and doing
that well, while also "doing the simplest thing that could possibly
work".
&lt;/p&gt;
&lt;p&gt;
As such, there's little to no overlap on the features offered from one
microlib to another (given they don't try to solve the same problem, of
course) and you get only what you really need. No more, no less.
&lt;/p&gt;
&lt;p&gt;
In fact, microlibs would fit the &lt;a href="http://en.wikipedia.org/wiki/Unix_philosophy"&gt;Unix philosophy&lt;/a&gt; quite well (the
text streams aside, usually):
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;Write programs that do one thing and do it well. Write programs to
work together. Write programs to handle text streams, because that is
a universal interface.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;&amp;hellip;but microlibs are broken too&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
Reality is a bit harsh, though. There are a few problems with the
&lt;b&gt;current state&lt;/b&gt; of microlibs, both because they're pretty new and
because somehow this whole size-limit thing has gotten quite messed up.
&lt;/p&gt;
&lt;p&gt;
Some of these problems are quite a show stopper. 
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-3-1" class="outline-3"&gt;
&lt;h3 id="sec-3-1"&gt;1. Lack of documentation&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-1"&gt;


&lt;p&gt;
Documentation is good and shouldn't be overlooked. If you want people to
use your code, you should tell them how they should use it. No, dumping
a weird API reference on a &lt;code&gt;README&lt;/code&gt; file won't cut it. Random words
scrawled over the source won't either.
&lt;/p&gt;
&lt;p&gt;
I'm talking about good documentation on the library btw, not just some
API reference. And most microlibs I know (including my &lt;a href="http://github.com/killdream/ekho"&gt;Ekho&lt;/a&gt; lib)
have no examples, no use cases, no how to get started, no anything!
&lt;/p&gt;
&lt;p&gt;
But there are worse cases (imo). All &lt;a href="http://twitter.com/ded"&gt;@ded&lt;/a&gt;'s libraries have almost
no comments on the source code, and rely rather on dumping a little of
introductory text on an obscure &lt;code&gt;README.md&lt;/code&gt; file.
&lt;/p&gt;
&lt;p&gt;
Mind you, I want to know what are the purposes and implications of using
some function if I'm going to extend the library. I don't want to shoot
myself on the foot by accidentally calling a function that happens to
have side-effects which are not apparent from reading the function
alone.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-2" class="outline-3"&gt;
&lt;h3 id="sec-3-2"&gt;2. Lack of tests&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-2"&gt;


&lt;p&gt;
Some libraries do have a small amount of test cases, but as
&lt;a href="http://twitter.com/jDalton"&gt;@jDalton&lt;/a&gt; showed on his &lt;a href="http://allyoucanleet.com/2011/05/09/microlibs-the-fud-challenge/"&gt;screen cast&lt;/a&gt;, some don't really
have that good of a coverage.
&lt;/p&gt;
&lt;p&gt;
Some libraries (&lt;a href="http://github.com/killdream/ekho"&gt;Ekho&lt;/a&gt;) have absolutely NO unit tests, which is something you
should find outrageous&lt;sup&gt;&lt;a class="footref" name="fnr.2" href="#fn.2"&gt;2&lt;/a&gt;&lt;/sup&gt; (and, maybe, help fixing).
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-3" class="outline-3"&gt;
&lt;h3 id="sec-3-3"&gt;3. Small community&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-3"&gt;


&lt;p&gt;
This is more related to how recent all these libraries are. They don't
have a large user base like the monolithic frameworks. So it may be
quite more difficult to find an answer to problems you're facing.
&lt;/p&gt;
&lt;p&gt;
It's also likely that the library will have problems that people using
more mature and larger frameworks have already reported. And these would
take a bit to fix as well.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-4" class="outline-3"&gt;
&lt;h3 id="sec-3-4"&gt;4. Trying to solve problems they're not meant to&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-4"&gt;


&lt;p&gt;
According to the buzz on the internets, microlibs are supposed to do
&lt;i&gt;only one thing and do it well™&lt;/i&gt;, but in practice, that's not
true. Almost all of these libraries will provide fallbacks for
features that are not related to the design goals of the library,
and worse, for features that are described in the &lt;a href="http://es5.github.com"&gt;ECMAScript 5&lt;/a&gt;
specs.
&lt;/p&gt;
&lt;p&gt;
Yes, I'm looking at all of the libraries that implement
&lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf"&gt;Array#indexOf&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter"&gt;Array#filter&lt;/a&gt;, &lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind"&gt;Function#bind&lt;/a&gt; and such
fallbacks, when the library tries to solve an entire different problem.
&lt;/p&gt;
&lt;p&gt;
This creates two problems. The first is that now instead of having dead
code, you have &lt;b&gt;duplicated code&lt;/b&gt;. It's even worse if you're only
targeting platforms which are ES5-compliant, because now you have
&lt;b&gt;duplicated dead code&lt;/b&gt;.
&lt;/p&gt;
&lt;p&gt;
The second problem is that some of these libraries implement things that
don't conform with the specs. And I'm not too keen on having 20
different implementations of &lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf"&gt;Array#indexOf&lt;/a&gt; which don't work for
sparse arrays, thank you.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-5" class="outline-3"&gt;
&lt;h3 id="sec-3-5"&gt;5. API inconsistencies&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-5"&gt;


&lt;p&gt;
You can't expect to get API consistencies when using libraries from 10
different authors if they're not designed to work together, which you'd
get for free on JavaScript frameworks (or would you?&lt;sup&gt;&lt;a class="footref" name="fnr.3" href="#fn.3"&gt;3&lt;/a&gt;&lt;/sup&gt;). 
&lt;/p&gt;
&lt;p&gt;
This can be really bad when you need to have two libraries to work
together, say a CSS selector engine and a DOM manipulation library. Of
course you can abstract all of them and create a DSL on top of them,
just as you could read a minified source rather than a properly
commented one&amp;hellip;
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-6" class="outline-3"&gt;
&lt;h3 id="sec-3-6"&gt;6. Dependency hell&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-6"&gt;


&lt;p&gt;
Browsers still don't offer native support for modules, although it's
&lt;a href="http://wiki.ecmascript.org/doku.php?id=harmony:modules"&gt;being discussed for ES.Next&lt;/a&gt;, nor can you use &lt;code&gt;jspm install microlib&lt;/code&gt; and
have all the dependencies sorted out nicely for you.
&lt;/p&gt;
&lt;p&gt;
The thing is that using lots of small libraries will make your building
and testing process quite more complex. Where you had to include a
simple script tag to load everything you need, you'll need to include
loads of them (in the right order too), or have something that
automagically concatenates those files in the right order.
&lt;/p&gt;
&lt;p&gt;
Either way, it's quite some hassle.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-7" class="outline-3"&gt;
&lt;h3 id="sec-3-7"&gt;7. Focus on arbitrary filesize&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-7"&gt;


&lt;p&gt;
Having a soft-limit for filesize isn't necessarily a bad thing. It helps
you to know when you're over-engineering things. However, 
not all the problems are the same that you can happily have a
&lt;a href="http://tomdale.net/2011/05/an-uphill-battle/"&gt;5kb limit for everynyan&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
With the microlibs, things have somehow gotten screwed up and the focus
that should be into "&lt;b&gt;reducing the scope of the library for modularity&lt;/b&gt;"
(think minimalism), turned into a battle for who could fit an entire DOM
library in a tweet.
&lt;/p&gt;
&lt;p&gt;
I find this whole premature and reckless optimisation for filesize
pretty stupid, of course. &lt;a href="http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-7.html"&gt;As Sussman says&lt;/a&gt;:
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;Programs must be written for people to read, and only incidentally for
machines to execute.
&lt;/p&gt;
&lt;/blockquote&gt;


&lt;p&gt;
Programs should also be written for correctness and to effectively solve
the problems they propose to solve, but I guess everyone knows that
already&amp;hellip;
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;What can we do?&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;


&lt;p&gt;
So, large and bloated frameworks are a bad idea for web apps, but the
current state of microlibs is also pretty bad. That's no fun, and
definitely not good for the community, right?
&lt;/p&gt;
&lt;p&gt;
The obvious answer to these problems is to focus on writing modular
libraries. And when I say &lt;i&gt;modular&lt;/i&gt;, I'm referring to libraries that fit
the Unix philosophy.
&lt;/p&gt;
&lt;p&gt;
They should be designed to solve just one problem, and solve it well, so
people can decide exactly which problems they want to solve instead of
getting a large black box that tries to solve everything.
&lt;/p&gt;
&lt;p&gt;
They should also be designed to work nicely with other libraries,
otherwise what's the point in having modular stuff anyways? You'll most
likely never have to solve just a single problem when writing a web
app.
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-4-1" class="outline-3"&gt;
&lt;h3 id="sec-4-1"&gt;A new design philosophy&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-4-1"&gt;


&lt;p&gt;
In fact, we can summarise it nicely in a list of few points to keep in
mind when designing a library. Mostly stolen from Eric Raymond's The Art
of Unix Programming book:
&lt;/p&gt;
&lt;dl&gt;
&lt;dt&gt;Modularity   &lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Write simple parts connected by clean interfaces.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;Composition  &lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Design libraries to be connected to other libraries.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;Simplicity   &lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Design for simplicity; add complexity only where you must.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;Correctness  &lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  If it doesn't &lt;b&gt;fully&lt;/b&gt; solves the problem it's designed to solve, it's
  broken.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;Lazyness     &lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Don't try to solve what's already solved; stop writing broken ES5
  fallbacks.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;Parsimony    &lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Write big libraries only when it's clear by demonstration that nothing
  else will do.
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;Optimisation &lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Prototype before polishing. Get it working before you optimise it (be
  it for size, speed or anything else).
&lt;/p&gt;
&lt;/dd&gt;
&lt;dt&gt;Extensibility&lt;/dt&gt;&lt;dd&gt;
&lt;p&gt;
  Design for the future, because it will be here sooner than you think
  (modules are a good thing, and they'll be added to the language!)
&lt;/p&gt;&lt;/dd&gt;
&lt;/dl&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4-2" class="outline-3"&gt;
&lt;h3 id="sec-4-2"&gt;Problems with this philosophy&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-4-2"&gt;


&lt;p&gt;
Obviously, these points are not anything near perfect. And it gets worse
if you take the current state of browser platforms into account. The web
is quite heterogeneous. Even on browsers who do conform with the
standards, you still have some quite funny discrepancies on
the inner workings — and that affects your code.
&lt;/p&gt;
&lt;p&gt;
To make things yet worse, you don't have much things to solve the
dependency hell, aside from writing a custom build tool. And even so,
you'll likely need to download and build a set of different libraries to
get everything working.
&lt;/p&gt;
&lt;p&gt;
This is not exactly my definition of fun.
&lt;/p&gt;
&lt;p&gt;
For this to really work, we'd need a good package manager; like what &lt;a href="http://github.com/isaacs/npm"&gt;npm&lt;/a&gt;
is to &lt;a href="http://nodejs.org"&gt;Node.js&lt;/a&gt;. And we would also need either some support for loading
these external modules in a sane way (you do &lt;a href="http://github.com/killdream/jello"&gt;have&lt;/a&gt; &lt;a href="http://requirejs.org"&gt;some&lt;/a&gt; &lt;a href="http://labjs.com"&gt;libraries&lt;/a&gt; &lt;a href="http://news.ycombinator.com/item?id=2300423"&gt;for that&lt;/a&gt;), rather than having to do with build scripts.
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5" class="outline-2"&gt;
&lt;h2 id="sec-5"&gt;Wrapping it up&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;


&lt;p&gt;
There's no such thing as &lt;b&gt;one true way&lt;/b&gt;. I don't think modular
libraries/frameworks solve all the problems, but they do address quite
some of the problems we have with the current models of libraries.
&lt;/p&gt;
&lt;p&gt;
And it would be nice having a healthier environment for developing new
awesome apps on the Browser, without having to write everything from
scrath again because the thing you need is only included in another
library, and it uses a framework-centric approach.
&lt;/p&gt;
&lt;p&gt;
All in all, it's something I think would benefit the JavaScript
community as a whole :3
&lt;/p&gt;


&lt;div id="footnotes"&gt;
&lt;h2 class="footnotes"&gt;Footnotes: &lt;/h2&gt;
&lt;div id="text-footnotes"&gt;
&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.1" href="#fnr.1"&gt;1&lt;/a&gt;&lt;/sup&gt; I hate using the term "framework" to refer to libraries with all
       my guts. Seriously, people, is `library` **that** hard to spell?
&lt;/p&gt;

&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.2" href="#fnr.2"&gt;2&lt;/a&gt;&lt;/sup&gt; I'll work on tests and documentation for &lt;a href="http://github.com/killdream/ekho"&gt;Ekho&lt;/a&gt; and friends when I
       have time to do so. Probably after I finish some of the core
       concepts/early prototypes for &lt;a href="http://killdream.github.com/orpheos"&gt;OrpheOS&lt;/a&gt;. Until then, considering
       the library a beta release may be a good thing.
&lt;/p&gt;

&lt;p class="footnote"&gt;&lt;sup&gt;&lt;a class="footnum" name="fn.3" href="#fnr.3"&gt;3&lt;/a&gt;&lt;/sup&gt; I don't think PrototypeJS has a consistent API, for
       example. Though Ruby people might say otherwise, it just strikes
       me as odd if you use &lt;code&gt;bindAsEventListener&lt;/code&gt; and &lt;code&gt;gsub&lt;/code&gt; randomly on
       your API (I would never take a Ruby programmer's word on good API
       design anyways ;P). jQuery's API on the other hand (although I've
       only used a small subset of it for a short time) is just a mess
       of counter-intuitive and poorly-named methods. People should stop
       getting inspired by PHP when designing an API&amp;hellip;
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Meeting Enlightenment</title>
   <link href="http://killdream.github.com//blog/2011/03/meeting-enlightenment/index.html" />
   <updated>2011-03-05T00:00:00-08:00</updated>
   <id>http://killdream.github.com//blog/2011/03/meeting-enlightenment/meeting-enlightenment</id>
   <content type="html">&lt;p&gt;
I've used quite a few Window Managers in the past: IceWM, KDE, Gnome-Shell,
Metacity, Fluxbox, etc. but they either looked plain horrible, were bloated
like hell — yeah, KDE, take that — or were just plain and irritatingly slow.
&lt;/p&gt;
&lt;p&gt;
I've always liked lightweight and tiling window managers — which is one of the
reasons I love Emacs — but they all felt too plain to my likings. As an art
lover, I need some spark and beauty on my desktop, rather than strictly
functional things.
&lt;/p&gt;
&lt;p&gt;
So, here am I ranting about about the features of e17 that got me hooked, and
&lt;i&gt;why&lt;/i&gt; &lt;b&gt;you&lt;/b&gt; &lt;i&gt;should give it a try&lt;/i&gt;.
&lt;/p&gt;



&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;Why you should try it&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
You probably heard elsewhere that enlightenment is lightweight and
sparkles. But I don't think that is enough reason to switch to a Window
Manager — all the more when it hasn't seen a stable release yet.
&lt;/p&gt;
&lt;p&gt;
So, here's my little list of reasons:
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-1-1" class="outline-3"&gt;
&lt;h3 id="sec-1-1"&gt;Lightweight and FAST&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-1"&gt;


&lt;p&gt;
I have a kinda old PC with an on-board graphic card, which means that not
even OpenGL gets some love here, thus being lightweight is a must for
me. Not only this, but Enlightenment manages to be a lot faster than any
other window manager I've tried so far — taking the delicious amount of
eye-candiness into account, obviously.
&lt;/p&gt;
&lt;p&gt;
I also have a &lt;a href="http://macpup.org"&gt;MacPup&lt;/a&gt; that runs directly from my thumb-drive. The distro is
bundled with IceWM, Enlightenment and another window manager I can't
remember the name for life, still it fits nicely in less than 228MB, and
runs entirely on the RAM, leaving all the remaining space on my thumb-drive
for &lt;b&gt;actual data&lt;/b&gt;.
&lt;/p&gt;
&lt;p&gt;
It doesn't makes much of a difference if you have a bleeding-edge PC, or use
any KDE application out there — I try to stay clear of them, by the way.
&lt;/p&gt;


&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-2" class="outline-3"&gt;
&lt;h3 id="sec-1-2"&gt;Fully configurable&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-2"&gt;


&lt;p&gt;
Which is one of the reasons I use Linux in the first place. I like
configuring everything to fit my workflow, and Enlightenment lets me do so
quite easily with the settings GUI.
&lt;/p&gt;
&lt;p&gt;
Most of this configuration is quite straight forward, but some of the UI are
not that good.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/settings.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/settings.jpg" /&gt;
&lt;/p&gt;
&lt;p&gt;
You can also configure everything from the command line, or editing the
configuration files — though in this case you have to decompile them and
recompile when you're done.
&lt;/p&gt;
&lt;p&gt;
Another good thing about Enlightenment's configuration is the profiles. This
allows you to have as many different configurations as you need, and allows
you to safely test stuff away, while having your previous configuration
safely stored.
&lt;/p&gt;
&lt;p&gt;
For example, I use three different profiles for my MacPup: Netbook, Notebook
and Desktop. Each providing a slight different set of keybindings, mouse
adjustments and screen/modules. So any time I plug it in a different
computer, I just have to select a suitable profile instead of reconfiguring
everything.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-3" class="outline-3"&gt;
&lt;h3 id="sec-1-3"&gt;Modularity&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-3"&gt;


&lt;p&gt;
Since Enlightenment is built up on a modular structure, you can choose
exactly which features you use instead of wasting RAM on useless stuff. This
also means that plugins blend in really nice with the entire Window Manager,
which is always a good thing.
&lt;/p&gt;
&lt;p&gt;
Alongside with the configuration profiles, this lets you test modules away
without having to worry about breaking the Window Manager — since you can
always go back to your previous sane profile.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-4" class="outline-3"&gt;
&lt;h3 id="sec-1-4"&gt;Shelves and Gadgets&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-4"&gt;


&lt;p&gt;
Metacity has &lt;code&gt;desklets&lt;/code&gt; and &lt;code&gt;panels&lt;/code&gt;, Enlightenment has &lt;code&gt;shelves&lt;/code&gt; and
&lt;code&gt;gadgets&lt;/code&gt;. Gadgets are just small applications you can use on your desktop,
and shelves are a way to group a collection of gadgets.
&lt;/p&gt;
&lt;p&gt;
Shelves also have a few interesting features that I like. The most important
of them is the ability of hiding a shelf until I actually &lt;b&gt;need it&lt;/b&gt; — by
either moving my mouse over the edge of the screen, or clicking on it. I
just hate clutter and think my screen space is better used to hold the
windows of the applications I use, instead of a taskbar or dock.
&lt;/p&gt;
&lt;p&gt;
Another interesting stuff about shelves is that, aside of the screen
position, you can also specify the stacking position. So shelves can be
placed above or below everything. You can also configure whether the
shelves' space should be available for windows or not.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/desktop-thumb.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/desktop-thumb.jpg" /&gt;
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;Top, top-left and bottom-right are shelves. The other ones are plain gadgets
placed over the desktop.
&lt;/p&gt;
&lt;/blockquote&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-5" class="outline-3"&gt;
&lt;h3 id="sec-1-5"&gt;Charmingly beautiful&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-5"&gt;


&lt;p&gt;
Even if you don't have a powerful enough computer to run the compositing
stuff (Enlightenment ships with Ecomorph, which is a port of Compiz), there
are still plenty of eye-candy for you, including animated backgrounds,
animated icons and transitions.
&lt;/p&gt;
&lt;p&gt;
Better still, everything is theme-able, including the third-party
modules. And you can build your own theme with parts of various other
themes.
&lt;/p&gt;
&lt;p&gt;
For Gtk applications you'll need to install a suitable Gtk theme though.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/themes-thumb.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/themes-thumb.jpg" /&gt;
&lt;/p&gt;
&lt;p&gt;
Currently, I'm using &lt;b&gt;Detour&lt;/b&gt; as my base Enlightenment theme, which parts of
Simply White, Imago and a few others. For the Gtk part, the Orta theme
blends in quite nicely and looks hella sweet :3
&lt;/p&gt;
&lt;p&gt;
To &lt;b&gt;feel&lt;/b&gt; the beauty of Enlightenment you have to try it though. It's not
something that can be portrayed easily with images. You can have a go at
some youtube videos, though.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-6" class="outline-3"&gt;
&lt;h3 id="sec-1-6"&gt;Everything a few keystrokes away&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-6"&gt;


&lt;p&gt;
I &lt;b&gt;loathe&lt;/b&gt; going through &lt;i&gt;xyz&lt;/i&gt; menus just to launch an application — I hate
most menus, actually. Enlightenment comes with a nice QuickSilver-like
launcher though, called Everything. If you're not familiar with the Mac
world, it's something like Gnome-Do, but with far more levels of awesomeness
— and frankly, I've always found Gnome-Do frustrating.
&lt;/p&gt;
&lt;p&gt;
Everything follows the modularity structure of Enlightenment, so you can add
any plugin to it to make doing some tasks faster and easier.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/everything.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/everything.jpg" /&gt;
&lt;/p&gt;
&lt;p&gt;
You can have a quick calculator, spell checker, web search, file search and
any other thing you want with a plugin. It's &lt;b&gt;really&lt;/b&gt; handy (even for
someone who almost uses Emacs as his OS).
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-7" class="outline-3"&gt;
&lt;h3 id="sec-1-7"&gt;Shortcuts for anything&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-7"&gt;


&lt;p&gt;
Another thing that I dislike are mouses. I just can't get used to them and
feel that they get too much on my way. Well, when using Enlightenment I
don't need it most of the time, because there's keybindings for almost
everything.
&lt;/p&gt;
&lt;p&gt;
The input dialogues allows for shortcuts for usual system commands, as you
would expect, but also to manipulate virtual desktops and windows. And when
I say &lt;i&gt;manipulate windows&lt;/i&gt; I mean you can resize, position and switch
between these windows using your keyboard in a sane way.
&lt;/p&gt;
&lt;p&gt;
I have almost all possible cute mnemonic combinations with the useless Super
key (Super+key, Ctrl+Super+key, Shift+Super+key, Alt+Super+key, &amp;hellip;) to do
some actual useful stuff, so I guess you can get an idea of the
awesomeness :3
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-8" class="outline-3"&gt;
&lt;h3 id="sec-1-8"&gt;Tiling&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-8"&gt;


&lt;p&gt;
I believe this is bundled in e17 in the Illume2, but since the Illume stuff
is for embed and mobile systems, I haven't dig into this. Instead I use the
tiling module that's on the SVN.
&lt;/p&gt;
&lt;p&gt;
The module isn't perfect, but I don't have that many complaints against it.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-1-9" class="outline-3"&gt;
&lt;h3 id="sec-1-9"&gt;Handling errors gracefully&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-9"&gt;


&lt;p&gt;
Yes, the Enlightenment's window manager (e17) is still in pre-alpha, so it's
not all &lt;b&gt;that&lt;/b&gt; stable — although I've been using it for months now without
much trouble.
&lt;/p&gt;
&lt;p&gt;
However, you're likely to experience some segfaults if you play around too
much:
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/esegvd.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/esegvd.jpg" /&gt;
&lt;/p&gt;
&lt;p&gt;
It's not something you should worry that much though, only the window
manager is affected, not your data. And it gracefully recovers itself to the
previous state in less than a second, which is a pretty nice thing.
&lt;/p&gt;

&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;The drawbacks&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
Yeah, Enlightenment is great and all, but it has its drawbacks. The worst issue
is really stability. Not that it will crash each minute, but you're likely to
experience a few minor issues: like an auto-hide shelf that won't hide and stuff like that.
&lt;/p&gt;
&lt;p&gt;
This can all be solved by restarting the Window Manager — and I mean the WM
not your DM, which means that your application state won't be affected. You
can restart e17 from the main menu in &lt;code&gt;Enlightenment → Restart&lt;/code&gt;.  I
personally prefer binding a shortcut to it (&lt;code&gt;Super-r&lt;/code&gt;) and using that.
&lt;/p&gt;
&lt;p&gt;
You may also miss some features. I think the modules have a great coverage of
the features &lt;b&gt;I&lt;/b&gt; need — even though the tiling isn't perfect — but they may
not cover well your use case.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;Getting it up and running&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
There are quite some ways to try Enlightenment out now. You don't need to
compile it out yourself if you don't want to, or are just to lazy to do so.
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-3-1" class="outline-3"&gt;
&lt;h3 id="sec-3-1"&gt;The easy way&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-1"&gt;


&lt;p&gt;
You can grab any Linux distro that comes with Enlightenment as their default
Window Manager and run from the LiveCD or install it on a spare HDD partition
or a VM. Just get any of the following and try it away:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="http://www.unity-linux.hu/unite17"&gt;Unite17&lt;/a&gt; — Unity-based desktop distro
&lt;/li&gt;
&lt;li&gt;&lt;a href="http://macpup.org"&gt;MacPup&lt;/a&gt;  — Puppy linux distro (uses Ubuntu Lucid Lynx)
&lt;/li&gt;
&lt;li&gt;&lt;a href="http://moonos.org"&gt;moonOS&lt;/a&gt;  — Ubuntu-based. Neak's main edition uses e17.
&lt;/li&gt;
&lt;li&gt;&lt;a href="http://bodhilinux.com/index.html"&gt;Bodhi&lt;/a&gt;   — Minimalist Ubuntu 10.04-based distro.
&lt;/li&gt;
&lt;/ul&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-2" class="outline-3"&gt;
&lt;h3 id="sec-3-2"&gt;The hard way&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-3-2"&gt;


&lt;p&gt;
If you want to try your luck, you can grab the source from the SVN
repository and compile it yourself&amp;hellip;  Okay, it's not so hard, it just takes
a little bit of time depending on your internet connection and computer
resources.
&lt;/p&gt;
&lt;p&gt;
I'll try to describe a little the steps to get e17 up and running, and while
these concepts should apply to any distro, bear in mind that I'm assuming an
Ubuntu one as basis for package list and specific commands.
&lt;/p&gt;


&lt;/div&gt;

&lt;div id="outline-container-3-2-1" class="outline-4"&gt;
&lt;h4 id="sec-3-2-1"&gt;Before installing&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-3-2-1"&gt;


&lt;p&gt;
e17 depends on quite a few libraries and tools. The &lt;a href="http://svn.enlightenment.org"&gt;svn repository&lt;/a&gt; lists most
of it, but depending on your distro and the amount of C-coding/compiling you've
done, it may vary. At least, make sure you have all of the tools and libraries
listed there.
&lt;/p&gt;
&lt;p&gt;
If you happen to be using an Ubuntu distro, you can get away with getting the
following from apt (just copy/paste it on the terminal):
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ sudo apt-get install subversion gcc autoconf &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
autopoint automake libtool make gettext &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
libpam0g-dev libfreetype6-dev libpng-dev &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
libjpeg-dev zlib1g-dev libdbus-1-dev &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
liblua5.1-0-dev libx11-dev libxcursor-dev &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
libxrender-dev libxrandr-dev libxfixes-dev &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
libxdamage-dev libxcomposite-dev libxss-dev &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
libxp-dev libxext-dev libxinerama-dev &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
libxkbfile-dev libxtst-dev libtiff-dev &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
librsvg2-dev libgif-dev libcurl4-gnutls-dev &lt;span style="color: #A6E32D;"&gt;\&lt;/span&gt;
libgnutls-dev libxml2-dev libudev-dev
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-2-2" class="outline-4"&gt;
&lt;h4 id="sec-3-2-2"&gt;Building it&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-3-2-2"&gt;


&lt;p&gt;
You can just checkout from the svn repository and run &lt;code&gt;autogen.sh &amp;amp;&amp;amp; make &amp;amp;&amp;amp; sudo make install&lt;/code&gt; for every project. BUT THERE ARE BAZILLIONS OF THEM. To
automate this annoying chore, I just use the &lt;code&gt;easy_e17.sh&lt;/code&gt; script.
&lt;/p&gt;
&lt;p&gt;
So, to get it the easy way:
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ wget http://omicron.homeip.net/projects/easy_e17/easy_e17.sh
$ chmod +x easy_e17.sh
$ sudo ./easy_e17.sh --install
&lt;/pre&gt;


&lt;p&gt;
This will install the the most basic functionality of e17 — the core EFL and
the WM.
&lt;/p&gt;
&lt;p&gt;
If you want to get more power, you can install all of the extra modules, by
providing &lt;code&gt;--packagelist=half&lt;/code&gt; to the installer, or simply everything, by
providing &lt;code&gt;--packagelist=full&lt;/code&gt;. Be warned that some of these additional
features may not compile, or may be unmaintained and conflict with your stuff.
&lt;/p&gt;
&lt;p&gt;
To peek on the things you can configure in the install script, just use
&lt;code&gt;--help&lt;/code&gt;.
&lt;/p&gt;
&lt;p&gt;
If you'd rather have control over all of the build process, or want a sane way
of getting the newest features of the repository, you can use the sample script
on the &lt;a href="http://svn.enlightenment.org"&gt;svn repository&lt;/a&gt; as basis and modify it as you need.
&lt;/p&gt;
&lt;blockquote&gt;

&lt;p&gt;Note that for building the &lt;code&gt;ewheather&lt;/code&gt; module you'll need to build and
install the &lt;code&gt;libeweather&lt;/code&gt; (on &lt;code&gt;trunk/PROTO&lt;/code&gt;) first. For the &lt;code&gt;places&lt;/code&gt; module
you'll need &lt;code&gt;HAL&lt;/code&gt; too.
&lt;/p&gt;
&lt;/blockquote&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-2-3" class="outline-4"&gt;
&lt;h4 id="sec-3-2-3"&gt;Setting the environment&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-3-2-3"&gt;


&lt;p&gt;
If all goes well, you'll have enlightenment and its libraries (and perhaps a
few extra modules too) fully installed on your system. If you've used a
non-standard install path, which is probably the case with running the
&lt;code&gt;easy_e17.sh&lt;/code&gt; script, you'll need to export the paths so your OS can find all
that stuff.
&lt;/p&gt;
&lt;p&gt;
You can just copy the lines &lt;code&gt;easy_e17.sh&lt;/code&gt; tells you and paste them in your
&lt;code&gt;.bashrc&lt;/code&gt; file, then reload it:
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ nano ~/.bashrc
$ source ~/.bashrc
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3-2-4" class="outline-4"&gt;
&lt;h4 id="sec-3-2-4"&gt;4. Telling your OS to use e17&lt;/h4&gt;
&lt;div class="outline-text-4" id="text-3-2-4"&gt;


&lt;p&gt;
If you're using a Desktop Manager, you should probably look for how to add more
sessions to it. If you just don't care about all that, you can set a simple
&lt;code&gt;.xsession&lt;/code&gt; file on your &lt;code&gt;$HOME&lt;/code&gt; folder and restart X:
&lt;/p&gt;



&lt;pre class="src src-shell-script"&gt;$ echo &lt;span style="color: #A6E32D;"&gt;"exec `which enlightenment_start`"&lt;/span&gt; &amp;gt; ~/.xsession
&lt;/pre&gt;


&lt;blockquote&gt;

&lt;p&gt;if you're using a Desktop Manager, you'll need to logout and select &lt;code&gt;User Defined Session&lt;/code&gt;.
&lt;/p&gt;
&lt;/blockquote&gt;



&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;Wrapping it up&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;


&lt;p&gt;
As you can see, there's much more to Enlightenment than the "&lt;b&gt;lightweight&lt;/b&gt;" or
"&lt;b&gt;beauty at your fingertips&lt;/b&gt;" stuff you hear everywhere, but most of the nice
things about it can't be easily (if at all) put into words. It's about the
UX — about how you feel while interacting with the system, — and as such it's
something you really have to try out to understand.
&lt;/p&gt;
&lt;p&gt;
Don't forget to drop by &lt;code&gt;#e@irc.freenode.org&lt;/code&gt; if you have any doubt or problems
with EFL or e17.
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>My workspace in 7 items</title>
   <link href="http://killdream.github.com//blog/2011/02/my-workspace-in-7-items/index.html" />
   <updated>2011-02-12T00:00:00-08:00</updated>
   <id>http://killdream.github.com//blog/2011/02/my-workspace-in-7-items/my-workspace-in-7-items</id>
   <content type="html">&lt;p&gt;
&lt;a href="http://diskchocolate.com/blog/2011/02/02/meu-ambiente-de-trabalho-em-7-itens/"&gt;Cindy&lt;/a&gt;, that cutie, invited me for this meme, so here I am, describing the
things I use in my little everyday life to get my work done. Those are &lt;b&gt;very important things&lt;/b&gt; for my work.
&lt;/p&gt;
&lt;p&gt;
I mean, seriously :3
&lt;/p&gt;


&lt;div id="outline-container-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;GNU/Linux: Ubuntu 10.10&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;


&lt;p&gt;
I should start with the most important of all these things: my OS. I've been
using the Ubuntu distro for quite some time now and I'm quite happy with
it. Though I still dislike some of its organisation, most of the things are
nicer than the other GNU/Linux distros I've tried so far.
&lt;/p&gt;
&lt;p&gt;
And yes, I'm saying that being &lt;i&gt;noob friendly&lt;/i&gt; (as in &lt;i&gt;user friendly&lt;/i&gt;) isn't a
bad thing — &lt;i&gt;au contraire&lt;/i&gt;, it's one of its strengths. At any rate, it's still
a GNU/Linux at its core, and you can customise it to your likings, just like
any other distro. Which, mind you, it's the important part.
&lt;/p&gt;
&lt;p&gt;
Metacity sucks balls though, so I've switched to using &lt;b&gt;Enlightenment&lt;/b&gt; &lt;i&gt;e17&lt;/i&gt;
as my Window Manager. Despite being a dev version, it's actually pretty stable
and the core libraries just hit a stable release a while ago. The thing I like
about it are the customisable key bindings for everything, so I almost don't
need to use the mouse for anything. Not to mention it's hella fast and
beautiful :D
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/desktop-thumb.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/desktop-thumb.jpg" /&gt;
&lt;/p&gt;


&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-2" class="outline-2"&gt;
&lt;h2 id="sec-2"&gt;Emacs&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-2"&gt;


&lt;p&gt;
My second OS&amp;hellip; I mean, my text editor of choice! Emacs, like GNU/Linux is
pretty customisable and robust. Vim people like saying it lacks a good text
editor — I beg to disagree. I love most of Emacs text editing features, and I
have gotten so used to its shortcuts that they just make sense (and I keep
pressing &lt;code&gt;C-s&lt;/code&gt; to search texts in Chrome now)
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;Kill ring&lt;/b&gt; and &lt;b&gt;mark ring&lt;/b&gt; are one of my favourite features. The first one
allows you to cycle through previous text copied to the clipboard, the second
lets you save positions in the buffer and cycle through it.
&lt;/p&gt;
&lt;p&gt;
For programming, I use &lt;code&gt;js2-mode&lt;/code&gt; with &lt;code&gt;yasnippet&lt;/code&gt; and &lt;code&gt;auto-complete-mode&lt;/code&gt; for
JavaScript, and &lt;code&gt;python-mode&lt;/code&gt; with &lt;code&gt;pymacs/ropemacs&lt;/code&gt; for Python. I use the
standard modes for most of the other languages, where available.
&lt;/p&gt;
&lt;p&gt;
Obviously, I don't use Emacs just for programming. As a good Emacs hipster, I
try to do everything from it, so I use &lt;code&gt;ERC&lt;/code&gt; for chatting with people on IRC,
&lt;code&gt;Twittering-mode&lt;/code&gt; for posting to twitter and stalking people, &lt;code&gt;Tumble-mode&lt;/code&gt; for
posting to my Tumblelog, &lt;code&gt;aHG&lt;/code&gt; for versioning my files through Mercurial, and a
few other things.
&lt;/p&gt;
&lt;p&gt;
For e-mail I decided I'm better off sticking with Thunderbird though&amp;hellip;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/emacs-thumb.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/emacs-thumb.jpg" /&gt;
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-3" class="outline-2"&gt;
&lt;h2 id="sec-3"&gt;Mercurial&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-3"&gt;


&lt;p&gt;
Simply the best VCS. Ever. Period. Take that, git!
&lt;/p&gt;
&lt;p&gt;
Erm, anyways, I use Mercurial for my versioning. I used to rely on ol' SVN pal
back then, and the transition to Mercurial was really pretty smooth. Everything
in Hg feels pretty natural if you have some background in &lt;i&gt;version control&lt;/i&gt;,
and when it doesn't feel that intuitive, the great help is there to save the
day.
&lt;/p&gt;
&lt;p&gt;
Since Mercurial's design involves having each command focused in doing just one
thing — and doing that well — you can quickly discover what the command does by
reading an one-line summary about it.
&lt;/p&gt;
&lt;p&gt;
Those are basically the things that made me fall in love with Hg. And yeah,
I've used Git, but it's just not my thing. Each command in Git is like a swiss
army knife, and it's not anything near intuitive until you get familiar with
the git-way of doing things (which I never did).
&lt;/p&gt;
&lt;p&gt;
For interacting with Git repositories I prefer using the &lt;code&gt;hg-git&lt;/code&gt; extension and
sticking with Mercurial for my local versioning.
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/mercurial-thumb.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/mercurial-thumb.jpg" /&gt;
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-4" class="outline-2"&gt;
&lt;h2 id="sec-4"&gt;Headphones&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-4"&gt;


&lt;p&gt;
Now these are more important than my OS. REALLY, I'm one to listen to music all
day long. I just can't live without it. When I'm not listening to music on the
'puter, I'm doing so on my mobile, so yeah, I'm a &lt;i&gt;bit&lt;/i&gt; of a music freak :3
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/me.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/me.jpg" /&gt;
&lt;/p&gt;
&lt;p&gt;
Bad mobile pictures plus messy hair for the win :3
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-5" class="outline-2"&gt;
&lt;h2 id="sec-5"&gt;Compass&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-5"&gt;


&lt;p&gt;
Compass is a framework on top of Sass to take out a bit of the DRY from CSS. I
can't work with CSS without using it anymore, it provides lots of useful mixins
that you can use to workaround including lots of vendor prefix properties in
your stylesheets.
&lt;/p&gt;
&lt;p&gt;
In short, it keeps everything cleaner. And as a clean code junkie, there's just
no way I wouldn't love it :3
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-6" class="outline-2"&gt;
&lt;h2 id="sec-6"&gt;Dropbox&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-6"&gt;


&lt;p&gt;
I love Dropbox. I really do. I keep there lots of private backups and use it to
share content with the doujin group I'm helping with.
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-7" class="outline-2"&gt;
&lt;h2 id="sec-7"&gt;Cute pictures folder&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-7"&gt;


&lt;p&gt;
This is also a pretty important item on my workspace. I have slight more than
10GB of cute anime pictures, Japanese idols (most of which are from the sweet
Nozomi Sasaki) and cute fashion clothes.
&lt;/p&gt;
&lt;p&gt;
And what do I do with them? Well, I just stare and go d'awww, of course, then
get motivated to do work :3
&lt;/p&gt;
&lt;p&gt;
It goes without saying that all my pictures include properly dressed girls (and
traps)&amp;hellip;
&lt;/p&gt;
&lt;p&gt;
&lt;img src="http://dl.dropbox.com/u/4429200/blog/images-thumb.jpg"  alt="http://dl.dropbox.com/u/4429200/blog/images-thumb.jpg" /&gt;
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id="outline-container-8" class="outline-2"&gt;
&lt;h2 id="sec-8"&gt;Wrapping it up&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-8"&gt;


&lt;p&gt;
That's about it. But now that I've gone through it all, I'm curious to see how
my fella programmers' workspace looks like.
&lt;/p&gt;
&lt;p&gt;
No, this is not an implicit invitation for a meme. In fact, it's not
&lt;b&gt;implicit&lt;/b&gt; at all, so go write it up now :D
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 
</feed>
