<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Irakli Gozalishvili</title>
    <subtitle>?.</subtitle>
    <link href="http://www.jeditoolkit.com/atom.xml" rel="self" type="application/atom+xml" />
    <link href="http://www.jeditoolkit.com" rel="alternate" type="text/html" />
    <id>http://www.jeditoolkit.com</id>
    <updated>2012-04-20T07:56:58-07:00</updated>
    <author>
        <name>Irakli Gozalishvili</name>
    </author>
    
        <entry>
            <title>Write logic, not mechanics</title>
            <link href="http://www.jeditoolkit.com/2012/04/26/code-logic-not-mechanics.html" rel="alternate" type="text/html" />
            <updated>2012-04-26T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2012/04/26/code-logic-not-mechanics</id>
            <content type="html">&lt;p&gt;It strikes me that developers in JS community tend to choose patterns for solving recurring problems over abstractions.&lt;/p&gt;

&lt;p&gt;If we are not busy with &lt;a href='https://github.com/twitter/bootstrap/issues/3057'&gt;semicolon debates&lt;/a&gt;, we argue about widely used &amp;#8220;callback pattern&amp;#8221; for dealing with asynchronous API. Many have learned / invented ways to avoid &amp;#8220;pyramids of doom&amp;#8221;, but I believe they miss the point: pyramids are not the issue, it&amp;#8217;s an indication that we have one.&lt;/p&gt;

&lt;p&gt;In order to describe what I consider to be a real issues, I have to move back a little first:&lt;/p&gt;

&lt;h2 id='function'&gt;Function&lt;/h2&gt;

&lt;p&gt;In mathematics, a &lt;a href='http://en.wikipedia.org/wiki/Function_%28mathematics%29' title='Function in Mathematics'&gt;function&lt;/a&gt; is a relation between a set of &lt;strong&gt;inputs&lt;/strong&gt; and a set of potential &lt;strong&gt;outputs&lt;/strong&gt; with the property that each input is related to exactly one output.&lt;/p&gt;

&lt;h2 id='black_box'&gt;Black box&lt;/h2&gt;

&lt;p&gt;In science and engineering, a &lt;a href='http://en.wikipedia.org/wiki/Black_box' title='Black box abstraction'&gt;&amp;#8220;black box&amp;#8221;&lt;/a&gt; abstraction is used to model systems as set of components which can be viewed solely in terms of its &lt;strong&gt;input&lt;/strong&gt;, &lt;strong&gt;output&lt;/strong&gt; and &lt;a href='http://en.wikipedia.org/wiki/Transfer_function'&gt;transfer characteristics&lt;/a&gt;, without any knowledge of its internal workings. This components are opaque (black) boxes.&lt;/p&gt;

&lt;p&gt;&lt;img src='/resources/images/black-box.png' alt='black box' /&gt;&lt;/p&gt;

&lt;p&gt;Bigger boxes can be created out of smaller ones just by describing a data flow with in them (connecting inputs and outputs):&lt;/p&gt;

&lt;p&gt;&lt;img src='/resources/images/composite-black-box.png' alt='composite black box' /&gt;&lt;/p&gt;

&lt;p&gt;This allows one to reduce details as necessary and change internal implementation of any box without affecting other parts of the system as long as transfer characteristics remain same.&lt;/p&gt;

&lt;h2 id='functions_in_js'&gt;Functions in JS&lt;/h2&gt;

&lt;p&gt;In programing functions are modeled around the same concepts, even though we messed up an &lt;strong&gt;input&lt;/strong&gt; sets by adding implicit parts that may change over time. In JS function &lt;code&gt;input&lt;/code&gt; set consists of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Given arguments.&lt;/li&gt;

&lt;li&gt;Pseudo-variable &lt;code&gt;this&lt;/code&gt;.&lt;/li&gt;

&lt;li&gt;Scope bindings.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id='problems'&gt;Problems&lt;/h2&gt;

&lt;h3 id='1_no_output'&gt;1. No output&lt;/h3&gt;

&lt;p&gt;Since functions in JS are first class, they can be part of both &lt;strong&gt;input&lt;/strong&gt; and &lt;strong&gt;output&lt;/strong&gt; sets. Most of the asynchronous APIs take advantage of this fact and require special &lt;code&gt;callback&lt;/code&gt; function argument for continuation passing:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
require(&amp;#39;fs&amp;#39;).open(path, &amp;#39;r+&amp;#39;, function(error, fd) {
  // ...
})&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see such functions no longer have have any useful output, which means that they can&amp;#8217;t be used for building systems as black boxes. Such functions don&amp;#8217;t return values that can be passed over to other boxes.&lt;/p&gt;

&lt;h3 id='2_error_handling_on_each_step'&gt;2. Error handling on each step&lt;/h3&gt;

&lt;p&gt;I have heard many times people criticizing how in Java exceptions are caught, wrapped and re-thrown again. This reminds me of following:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
function readJSON(path, callback) {
  fs.readFile(path, function(error, content) {
    if (error) callback(error);
    else callback(null, JSON.parse(content));
  })
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Basically error propagation in &amp;#8220;callback&amp;#8221; style APIs is done manually. Note, that in some cases you may want to &lt;code&gt;try catch&lt;/code&gt; actual function body as well.&lt;/p&gt;

&lt;h3 id='3_polygamy'&gt;3. Polygamy&lt;/h3&gt;

&lt;p&gt;It easy to end up with two types of functions: synchronous and asynchronous. While it&amp;#8217;s possible to make sync function async it&amp;#8217;s not the case other way round. This usually means that if one the functions had being converted to be asynchronous all of it&amp;#8217;s users will have to be converted as well:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
function readJSON(path) {
  var data = fs.readFileSync(path)
  return JSON.parse(String(data))
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;You can&amp;#8217;t simply switch to &lt;code&gt;readFile&lt;/code&gt; if becomes necessary.&lt;/em&gt;&lt;/p&gt;

&lt;h3 id='4_progress_tracking'&gt;4. Progress tracking&lt;/h3&gt;

&lt;p&gt;Finally if your function depends on multiple asynchronous inputs then you will have to manually track each.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
function makeView(templateURI, dataURI, callback) {
  var template, data, pending = 2
  readURI(templateURI, function(error, content) {
    if (error) return callback(error)
    template = content
    if (!--pending) Mustache.render(template, data)
  })
  readURI(dataURI, function(error, content) {
    if (error) return callback(error)
    data = content
    if (!--pending) Mustache.render(template, data)
  })
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also note that this code assumes that &lt;code&gt;readURI&lt;/code&gt; will call a callback only once, which is not guaranteed.&lt;/p&gt;

&lt;h1 id='describe_logic_not_mechanics'&gt;Describe logic not mechanics&lt;/h1&gt;

&lt;p&gt;Now consider our last example. Most of the code there is for handling mechanics rather than describing a logic, which feels absolutely very wrong. As a matter of fact actual logic can be expressed as:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
function makeView(templateURI, dataURI) {
  var template = readURI(templateURI)
  var data = readURI(dataURI)
  return Mustache.render(template, data)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So, would not it be better to abstract timing out of logic when it&amp;#8217;s not necessary rather than keep solving all this issues in each and every function ? As a mater of fact solution has being there for ages in a form of &lt;a href='http://wiki.commonjs.org/wiki/Promises/A'&gt;promises&lt;/a&gt;, but for some reason people and web standards tend to use callbacks instead. Maybe because they feel complicated, but that does not necessary has to be the case:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
// Everyone knows how to write a function:
function sum(a, b) { return a + b }
console.log(sum(1, 2))  // =&amp;gt; 3

// Working with promises should not require nothing more
// than marker telling that function can accept input in
// form of promises
sum = promised(sum)
console.log = promised(console.log)

// Will continue to work with plain old values
console.log(sum(1, 2))  // =&amp;gt; 3

// Will also accept promises as arguments
var deferred = defer()          // make promise
var a = deferred.promise
var b = sum(a, 1)
var c = sum(b, 5)
console.log(c)                  // eventually prints =&amp;gt; 17
deferred.resolve(11)            // fulfill promise&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We should not be handling and propagating exceptions manually in each function, we should only handle them when we plan to recover:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
// Utility funciton that throws exceptions
var raise = promised(function(_) { throw Error(_) })

var a = raise(&amp;#39;Boom !&amp;#39;)   // Now we got an exception
var b = sum(a, 2)         // Now it has propagated to b
var c = sum(b, 12)        // Now it has propagaed to c

// Finally if when ready we handle exception in computation
c.then(null, console.error) // =&amp;gt; Error: Boom !&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If we just want to group multiple values into one there is an &lt;code&gt;Array&lt;/code&gt; for that no need to track progress of each eventual value if we just care about a group!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
Array = promised(Array)
var results = Array(readAsync(a), readAsync(b))
console.log(sum.apply(sum, rusults))&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Noticed a pattern ? We just write a logic, and if it needs to handle asynchronous input we wrap it into &lt;code&gt;promised(logic)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Whats really important here is that such functions can be used to build systems in black boxes, since they do have &lt;strong&gt;input&lt;/strong&gt; and &lt;strong&gt;output&lt;/strong&gt;. Demonstration of that is an example from above (assuming that &lt;code&gt;readURI&lt;/code&gt; returns promise):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;#!/bin/javascript
function makeView(templateURI, dataURI) {
  var template = readURI(templateURI)
  var data = readURI(dataURI)
  // Assuming Mustache.render = promised(Mustache.render)
  return Mustache.render(template, data)
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Implementing such a solution takes about 100 lines of code (ignoring comments), and that&amp;#8217;s more or less what any other control flow library costs anyway. I wish all of you to have more time to concentrate on logic of your program instead of mechanics &amp;amp; small &lt;a href='https://github.com/Gozala/micro-promise' title='Micro promise library'&gt;promise&lt;/a&gt; library may be a good first step!&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>protocol based polymorphism</title>
            <link href="http://www.jeditoolkit.com/2012/03/21/protocol-based-polymorphism.html" rel="alternate" type="text/html" />
            <updated>2012-03-21T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2012/03/21/protocol-based-polymorphism</id>
            <content type="html">&lt;p&gt;Not a long time ago I learned about &lt;a href='http://clojure.org/' title='Dynamic programming language targeting JVM, CLR &amp;amp;  JS'&gt;clojure&lt;/a&gt;&amp;#8217;s &lt;a href='http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29' title='Wikipedia: Polymorphism (computer science'&gt;polymorphism&lt;/a&gt; constructs and &lt;a href='https://vimeo.com/11236603' title='A quick overview of Clojure protocols by Stuart Halloway'&gt;protocols&lt;/a&gt;. I was so inspired by a porwer and flexibility of &lt;a href='http://www.infoq.com/interviews/hickey-clojure-protocols' title='Rich Hickey explains Clojure polymorphism'&gt;protocol based polymorphism&lt;/a&gt; that I decide to &lt;a href='https://github.com/Gozala/protocol' title='Protocol based polymorphism for javascript'&gt;prototyped it for JS&lt;/a&gt;. In this post I will try to give you a taste of protocols and maybe even motivate you give them a try.&lt;/p&gt;

&lt;h1 id='rationale'&gt;Rationale&lt;/h1&gt;

&lt;p&gt;In programing we usually write and consume various abstractions. Typically in OOP languages abstractions are defined via (class / object) interfaces and have a nasty expression problems. Imagine that you have &lt;code&gt;A&lt;/code&gt; and &lt;code&gt;B&lt;/code&gt; sets of abstractions and sets of implementations of those abstractions. &lt;code&gt;A&lt;/code&gt; should be able to work with &lt;code&gt;B&lt;/code&gt;&amp;#8217;s abstractions, and vice versa &lt;strong&gt;without modifications of the original code&lt;/strong&gt;. While it may not sound as a problem at first, it usually is in practice. Sometimes &lt;code&gt;A&lt;/code&gt; can&amp;#8217;t use &lt;code&gt;B&lt;/code&gt;, either because they were not designed to work with each other as they were written by a different authors or because one is newer than the other. Either way such cases require code changes, which may be difficult because code is old, or complicated or has a license restrictions and there could be millions of other reasons. Any code hits these issue in some form and it&amp;#8217;s just matter of time. When that happens we&amp;#8217;re left only with a few possible solutions:&lt;/p&gt;

&lt;h4 id='feature_detection'&gt;Feature detection&lt;/h4&gt;

&lt;p&gt;Typically this is a code that is written not in terms of abstractions, but entities, that do runtime branching by &amp;#8220;feature detection&amp;#8221;. Which may be a type (&lt;code&gt;if (value instanceof Type)&lt;/code&gt;) or shape (&lt;code&gt;if (value &amp;amp;&amp;amp; typeof(value.length) === &amp;#39;number&amp;#39;)&lt;/code&gt;) based. This not only makes code harder to read &amp;amp; reason about, but it also closed. In other words every new abstraction will require rewrite of those entities, in order to accumulate more conditions and branches.&lt;/p&gt;

&lt;h4 id='wrappers'&gt;Wrappers&lt;/h4&gt;

&lt;p&gt;Typically this means that entities implementing abstraction &lt;code&gt;A&lt;/code&gt; need to be wrapped by a &amp;#8220;glue code&amp;#8221; implementing abstraction from &lt;code&gt;B&lt;/code&gt; and vice versa, if consumption is bidirectional. Unfortunately this introduces lot&amp;#8217;s of incidental complexity as wrappers ruin identity &amp;amp; don&amp;#8217;t compose (every new abstraction requires wrappers for all existing ones and vice versa). Finally problem and required changes grow progressively with a number of abstractions used.&lt;/p&gt;

&lt;h4 id='monkey_patching'&gt;Monkey patching&lt;/h4&gt;

&lt;p&gt;Typically this means that implementation of &lt;code&gt;A&lt;/code&gt; abstraction is patched with a &amp;#8220;glue code&amp;#8221; implementing support for &lt;code&gt;B&lt;/code&gt; abstraction. This still introduces complexity by ruining namespacing (different abstractions may have conflicting names). Again problem gets worth with an amount of code. Also in some languages this is not even possible.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note: For more details I would recommend watching a &amp;#8220;A quick overview of &lt;a href='https://vimeo.com/11236603' title='A quick overview of Clojure protocols by Stuart Halloway'&gt;clojure protocols&lt;/a&gt;&amp;#8221; by &lt;a href='https://twitter.com/#!/stuarthalloway'&gt;Stuart Halloway&lt;/a&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;h1 id='protocols'&gt;Protocols&lt;/h1&gt;

&lt;p&gt;In &lt;a href='http://www.infoq.com/interviews/hickey-clojure-protocols' title='Rich Hickey explains Clojure polymorphism'&gt;Clojure polymorphism&lt;/a&gt; is achieved using protocols. They provide a powerful way for decoupling abstraction interface definition from an actual implementation per type, without risks of interference with other libraries. Protocols allow to add polymorphic behavior to things that already exist without changing them. I&amp;#8217;ll go into more details on protocols, but for code examples I will use my &lt;a href='https://github.com/Gozala/protocol' title='Protocol based polymorphism for javascript'&gt;JS prototype&lt;/a&gt; implementation instead of clojure code.&lt;/p&gt;

&lt;p&gt;There are several motivations for &lt;a href='https://github.com/Gozala/protocol' title='Protocol based polymorphism for javascript'&gt;JS protocol&lt;/a&gt; library:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Provide a high-performance, dynamic polymorphism construct as an alternative to an existing object inheritance that does not provides any mechanics for dealing with name conflicts.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Provide the best parts of interfaces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Specification only, no implementation&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Single type can implement multiple protocols&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Allow independent extension of types, protocols and implementations of protocols on types, by different parties.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id='define_protocol'&gt;Define protocol&lt;/h2&gt;

&lt;p&gt;A protocol is a named set of functions and their signatures defined by calling &lt;code&gt;protocol&lt;/code&gt; function:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Comment'&gt;/*jshint asi:true */&lt;/span&gt;
&lt;span class='Comment'&gt;// module: ./event-protocol&lt;/span&gt;

&lt;span class='Identifier'&gt;var&lt;/span&gt; protocol &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'protocol/core'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;protocol

&lt;span class='Comment'&gt;// Defining a protocol for working with an event listeners / emitters.&lt;/span&gt;
module&lt;span class='Operators'&gt;.&lt;/span&gt;&lt;span class='Keyword'&gt;exports&lt;/span&gt; &lt;span class='Operators'&gt;=&lt;/span&gt; protocol&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt;
  &lt;span class='Comment'&gt;// Function on takes event `target` object implementing&lt;/span&gt;
  &lt;span class='Comment'&gt;// `Event` protocol as first argument, event `type` string&lt;/span&gt;
  &lt;span class='Comment'&gt;// as second argument and `listener` function as a third&lt;/span&gt;
  &lt;span class='Comment'&gt;// argument. Optionally forth boolean argument can be&lt;/span&gt;
  &lt;span class='Comment'&gt;// specified to use a capture. Function allows registration&lt;/span&gt;
  &lt;span class='Comment'&gt;// of event `listeners` on the event `target` for the given&lt;/span&gt;
  &lt;span class='Comment'&gt;// event `type`.&lt;/span&gt;
  on&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; protocol&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Type'&gt;String&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Type'&gt;Function&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; &lt;span class='Type'&gt;Boolean&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;

  &lt;span class='Comment'&gt;// Function allows registration of single shot event `listener`&lt;/span&gt;
  &lt;span class='Comment'&gt;// on the event `target` of the given event `type`.&lt;/span&gt;
  once&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; protocol&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='String'&gt;'type'&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='String'&gt;'listener'&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; &lt;span class='String'&gt;'capture=false'&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;

  &lt;span class='Comment'&gt;// Unregisters event `listener` of the given `type` from the given&lt;/span&gt;
  &lt;span class='Comment'&gt;// event `target` (implementing this protocol) with a given `capture`&lt;/span&gt;
  &lt;span class='Comment'&gt;// face. Optional `capture` argument falls back to `false`.&lt;/span&gt;
  off&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; protocol&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='String'&gt;'type'&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='String'&gt;'listener'&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; &lt;span class='String'&gt;'capture=false'&lt;/span&gt;&lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;

  &lt;span class='Comment'&gt;// Emits given `event` for the listeners of the given event `type`&lt;/span&gt;
  &lt;span class='Comment'&gt;// of the given event `target` (implementing this protocol) with a given&lt;/span&gt;
  &lt;span class='Comment'&gt;// `capture` face. Optional `capture` argument falls back to `false`.&lt;/span&gt;
  emit&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; protocol&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='String'&gt;'type'&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='String'&gt;'event'&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; &lt;span class='String'&gt;'capture=false'&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;No implementations are provided&lt;/li&gt;

&lt;li&gt;Code above returns a set of polymorphic functions and a protocol object&lt;/li&gt;

&lt;li&gt;Resulting functions dispatch on the argument with an index denoted in a signature via special &lt;code&gt;protocol&lt;/code&gt; element.&lt;/li&gt;

&lt;li&gt;Other array elements of the signature represent interface definition, and does not yet carry any special meaning. (You could use functions to highlight types or strings to highlight names or come up with something more creative).&lt;/li&gt;

&lt;li&gt;Protocol implementations can be provided at any time from any scope that has access to defined protocol.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id='implement_protocol'&gt;Implement protocol&lt;/h2&gt;

&lt;p&gt;Defined protocols can be implemented per type bases. Since everything in JS is an &lt;code&gt;Object&lt;/code&gt; protocol implementation for &lt;code&gt;Object&lt;/code&gt; type can be though as a default, since all values will automatically support protocol via that implementation:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Comment'&gt;/*jshint asi:true */&lt;/span&gt;
&lt;span class='Comment'&gt;// module: ./event-object&lt;/span&gt;

&lt;span class='Identifier'&gt;var&lt;/span&gt; Event &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'./event-protocol'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; on &lt;span class='Operators'&gt;=&lt;/span&gt; Event&lt;span class='Operators'&gt;.&lt;/span&gt;on

&lt;span class='Comment'&gt;// Weak registry of listener maps associated&lt;/span&gt;
&lt;span class='Comment'&gt;// to event targets.&lt;/span&gt;
&lt;span class='Identifier'&gt;var&lt;/span&gt; map &lt;span class='Operators'&gt;=&lt;/span&gt; WeakMap&lt;span class='Parens'&gt;()&lt;/span&gt;

&lt;span class='Comment'&gt;// Returns listeners of the given event `target`&lt;/span&gt;
&lt;span class='Comment'&gt;// for the given `type` with a given `capture` face.&lt;/span&gt;
&lt;span class='Function'&gt;function&lt;/span&gt; getListeners&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  &lt;span class='Comment'&gt;// If there is no listeners map associated with&lt;/span&gt;
  &lt;span class='Comment'&gt;// this target then create one.&lt;/span&gt;
  &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Operators'&gt;!&lt;/span&gt;map&lt;span class='Operators'&gt;.&lt;/span&gt;has&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Parens'&gt;))&lt;/span&gt; map&lt;span class='Operators'&gt;.&lt;/span&gt;set&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;create&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Keyword'&gt;null&lt;/span&gt;&lt;span class='Parens'&gt;))&lt;/span&gt;

  &lt;span class='Identifier'&gt;var&lt;/span&gt; listeners &lt;span class='Operators'&gt;=&lt;/span&gt; map&lt;span class='Operators'&gt;.&lt;/span&gt;get&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Comment'&gt;// prefix event type with a capture face flag.&lt;/span&gt;
  &lt;span class='Identifier'&gt;var&lt;/span&gt; address &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;capture &lt;span class='Operators'&gt;?&lt;/span&gt; &lt;span class='String'&gt;'!'&lt;/span&gt; &lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='String'&gt;'-'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Operators'&gt;+&lt;/span&gt; type
  &lt;span class='Comment'&gt;// If there is no listeners array for the given type &amp;amp; capture&lt;/span&gt;
  &lt;span class='Comment'&gt;// face than create one and return.&lt;/span&gt;
  &lt;span class='Statement'&gt;return&lt;/span&gt; listeners&lt;span class='Braces'&gt;[&lt;/span&gt;address&lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Operators'&gt;||&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;listeners&lt;span class='Braces'&gt;[&lt;/span&gt;address&lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Braces'&gt;[]&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;

Event&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  on&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Identifier'&gt;var&lt;/span&gt; listeners &lt;span class='Operators'&gt;=&lt;/span&gt; getListeners&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Comment'&gt;// Add listener if not registered yet.&lt;/span&gt;
    &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Operators'&gt;!&lt;/span&gt;~listeners&lt;span class='Operators'&gt;.&lt;/span&gt;indexOf&lt;span class='Parens'&gt;(&lt;/span&gt;listener&lt;span class='Parens'&gt;))&lt;/span&gt; listeners&lt;span class='Operators'&gt;.&lt;/span&gt;push&lt;span class='Parens'&gt;(&lt;/span&gt;listener&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  once&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    on&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt;
    on&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt; cleanup&lt;span class='Parens'&gt;()&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
      off&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  off&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Identifier'&gt;var&lt;/span&gt; listeners &lt;span class='Operators'&gt;=&lt;/span&gt; getListeners&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Identifier'&gt;var&lt;/span&gt; index &lt;span class='Operators'&gt;=&lt;/span&gt; listeners&lt;span class='Operators'&gt;.&lt;/span&gt;indexOf&lt;span class='Parens'&gt;(&lt;/span&gt;listener&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Comment'&gt;// Remove listener if registered.&lt;/span&gt;
    &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;~index&lt;span class='Parens'&gt;)&lt;/span&gt; listeners&lt;span class='Operators'&gt;.&lt;/span&gt;splice&lt;span class='Parens'&gt;(&lt;/span&gt;index&lt;span class='Operators'&gt;,&lt;/span&gt; 1&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  emit&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Keyword'&gt;event&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Identifier'&gt;var&lt;/span&gt; listeners &lt;span class='Operators'&gt;=&lt;/span&gt; getListeners&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;slice&lt;span class='Parens'&gt;()&lt;/span&gt;
    &lt;span class='Comment'&gt;// &lt;/span&gt;&lt;span class='Todo'&gt;TODO&lt;/span&gt;&lt;span class='Comment'&gt;: Exception handling&lt;/span&gt;
    &lt;span class='Statement'&gt;while&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;listeners&lt;span class='Operators'&gt;.&lt;/span&gt;length&lt;span class='Parens'&gt;)&lt;/span&gt; listeners&lt;span class='Operators'&gt;.&lt;/span&gt;shift&lt;span class='Parens'&gt;()&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;call&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Keyword'&gt;event&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Note: Implementing protocol for &lt;code&gt;Object&lt;/code&gt; type is not a requirement&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id='extend_existing_types'&gt;Extend existing types&lt;/h2&gt;

&lt;p&gt;Existing types (prototypes or constructors / classes) may be extended to implement certain protocol by providing type specific implementation. For example our protocol functions would work with &lt;a href='http://nodejs.org/' title='Platform for building fast, scalable network applications'&gt;node.js&lt;/a&gt;&amp;#8217;s &lt;a href='http://nodejs.org/api/events.html#events_class_events_eventemitter' title='EventEmitter API documentation'&gt;EventEmitter&lt;/a&gt; objects, but in a funny way. Listeners registered by a standard API won&amp;#8217;t be called when emitting events with protocol function and vice versa. To fix that we can implement our protocol for the &lt;a href='http://nodejs.org/api/events.html#events_class_events_eventemitter' title='EventEmitter API documentation'&gt;EventEmitter&lt;/a&gt; type:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Comment'&gt;/*jshint asi:true */&lt;/span&gt;
&lt;span class='Comment'&gt;// module: ./event-emitter&lt;/span&gt;

&lt;span class='Identifier'&gt;var&lt;/span&gt; EventProtocol &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'./event-protocol'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Identifier'&gt;var&lt;/span&gt; EventEmitter &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'events'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;EventEmitter

EventProtocol&lt;span class='Parens'&gt;(&lt;/span&gt;EventEmitter&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  on&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    target&lt;span class='Operators'&gt;.&lt;/span&gt;on&lt;span class='Parens'&gt;(&lt;/span&gt;type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  once&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    target&lt;span class='Operators'&gt;.&lt;/span&gt;once&lt;span class='Parens'&gt;(&lt;/span&gt;type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  off&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    target&lt;span class='Operators'&gt;.&lt;/span&gt;removeListener&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  emit&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Keyword'&gt;event&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    target&lt;span class='Operators'&gt;.&lt;/span&gt;emit&lt;span class='Parens'&gt;(&lt;/span&gt;type&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Keyword'&gt;event&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Now this is cool, we managed to add support for our event abstraction to a type that was not designed to work with it without changing a single line of code. But this is just a tip of the iceberg, we could implement this protocol for more types, let&amp;#8217;s try to do it for &lt;a href='https://developer.mozilla.org/en/DOM/element'&gt;DOM elements&lt;/a&gt;:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Comment'&gt;/*jshint asi:true latedef: true */&lt;/span&gt;
&lt;span class='Comment'&gt;// module: ./event-dom&lt;/span&gt;

&lt;span class='Identifier'&gt;var&lt;/span&gt; Event &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'./event-protocol'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

Event&lt;span class='Parens'&gt;(&lt;/span&gt;Element&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  on&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    target&lt;span class='Operators'&gt;.&lt;/span&gt;addEventListener&lt;span class='Parens'&gt;(&lt;/span&gt;type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  off&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    target&lt;span class='Operators'&gt;.&lt;/span&gt;removeListener&lt;span class='Parens'&gt;(&lt;/span&gt;type&lt;span class='Operators'&gt;,&lt;/span&gt; listener&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  emit&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;target&lt;span class='Operators'&gt;,&lt;/span&gt; type&lt;span class='Operators'&gt;,&lt;/span&gt; option&lt;span class='Operators'&gt;,&lt;/span&gt; capture&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Comment'&gt;// Note: This is simplified implementation for demo purposes.&lt;/span&gt;
    &lt;span class='Identifier'&gt;var&lt;/span&gt; &lt;span class='Keyword'&gt;document&lt;/span&gt; &lt;span class='Operators'&gt;=&lt;/span&gt; target&lt;span class='Operators'&gt;.&lt;/span&gt;ownerDocument
    &lt;span class='Identifier'&gt;var&lt;/span&gt; &lt;span class='Keyword'&gt;event&lt;/span&gt; &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;document&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;createEvent&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'UIEvents'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Keyword'&gt;event&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;initUIEvent&lt;span class='Parens'&gt;(&lt;/span&gt;type&lt;span class='Operators'&gt;,&lt;/span&gt; option&lt;span class='Operators'&gt;.&lt;/span&gt;bubbles&lt;span class='Operators'&gt;,&lt;/span&gt; option&lt;span class='Operators'&gt;.&lt;/span&gt;cancellable&lt;span class='Operators'&gt;,&lt;/span&gt;
                      &lt;span class='Keyword'&gt;document&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;defaultView&lt;span class='Operators'&gt;,&lt;/span&gt; 1&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Keyword'&gt;event&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;data &lt;span class='Operators'&gt;=&lt;/span&gt; option&lt;span class='Operators'&gt;.&lt;/span&gt;data
    target&lt;span class='Operators'&gt;.&lt;/span&gt;dispatchEvent&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Keyword'&gt;event&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Think of all the different JS frameworks (&lt;a href='http://backbonejs.org/#Events-trigger' title='Backbone for JS Apps with Models, Views, Collections, and Events'&gt;Backbone&lt;/a&gt;, &lt;a href='http://yuilibrary.com/yui/docs/event/' title='YUI Library'&gt;YUI&lt;/a&gt;, &lt;a href='http://mrdoob.github.com/three.js/docs/48/#EventTarget' title='three.js - JavaScript 3D library'&gt;Three.js&lt;/a&gt;, &lt;a href='http://thejit.org/static/v20/Docs/files/Options/Options-Events-js.html' title='JavaScript InfoVis Toolkit'&gt;InfoVis&lt;/a&gt;, &lt;a href='http://raphaeljs.com/reference.html#eve' title='Raphaël - JavaScript library for working with vector graphics on the web'&gt;Raphaël&lt;/a&gt;, &lt;a href='http://mootools.net/docs/core/Types/DOMEvent' title='MooTools is a compact, modular, Object-Oriented JavaScript framework'&gt;Moo Tools&lt;/a&gt;, &amp;#8230;) that have their own flavored API for working with events, you could easily extend them to support our event protocol and make their abstractions interchangeable through the rest of the codebase (that makes use of protocols) without original code changes.&lt;/p&gt;

&lt;h2 id='multiple_protocols'&gt;Multiple protocols&lt;/h2&gt;

&lt;p&gt;All the examples above showed how support for a given protocol may be added to a different types, but it&amp;#8217;s not only that, any type may be extended to implement multiple protocols with absolutely no risks of naming conflicts. Here is pretty dummy, but still an example illustrating this point:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Comment'&gt;/*jshint asi:true latedef: true */&lt;/span&gt;
&lt;span class='Comment'&gt;// module: ./installable&lt;/span&gt;

&lt;span class='Comment'&gt;// Protocol for working with installable application components.&lt;/span&gt;
&lt;span class='Identifier'&gt;var&lt;/span&gt; Installable &lt;span class='Operators'&gt;=&lt;/span&gt; protocol&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt;
  &lt;span class='Comment'&gt;// Installs given `component` implementing this protocol. Takes optional&lt;/span&gt;
  &lt;span class='Comment'&gt;// configuration options.&lt;/span&gt;
  install&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; protocol&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; &lt;span class='String'&gt;'options:Object'&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Braces'&gt;]&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  &lt;span class='Comment'&gt;// Uninstall given `component` implementing this protocol.&lt;/span&gt;
  uninstall&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; protocol &lt;span class='Braces'&gt;]&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  &lt;span class='Comment'&gt;// Activate given `component` implementing this protocol.&lt;/span&gt;
  on&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; protocol &lt;span class='Braces'&gt;]&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  &lt;span class='Comment'&gt;// Disable given `component` implementing this protocol.&lt;/span&gt;
  off&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; protocol &lt;span class='Braces'&gt;]&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

Installable&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  install&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;component&lt;span class='Operators'&gt;,&lt;/span&gt; options&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Comment'&gt;// Implementation details...&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  uninstall&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;component&lt;span class='Operators'&gt;,&lt;/span&gt; options&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Comment'&gt;// Implementation details...&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  on&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;component&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    component&lt;span class='Operators'&gt;.&lt;/span&gt;enabled &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Boolean'&gt;true&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  off&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;component&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    component&lt;span class='Operators'&gt;.&lt;/span&gt;enabled &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Boolean'&gt;false&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

module&lt;span class='Operators'&gt;.&lt;/span&gt;&lt;span class='Keyword'&gt;exports&lt;/span&gt; &lt;span class='Operators'&gt;=&lt;/span&gt; Installable
&lt;/pre&gt;
&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; That even though both &lt;code&gt;Event&lt;/code&gt; and &lt;code&gt;Installable&lt;/code&gt; protocols define functions &lt;code&gt;on&lt;/code&gt; and &lt;code&gt;off&lt;/code&gt;. Also &lt;code&gt;Object&lt;/code&gt; implements both still protocols, but there no conflicts arise and functions defined by both protocols can be used without any issues!&lt;/p&gt;

&lt;h1 id='summary'&gt;Summary&lt;/h1&gt;

&lt;p&gt;I hope you find this interesting &amp;amp; I&amp;#8217;m looking forward to your feedback. All the code examples from this post can be found in the &lt;a href='https://github.com/Gozala/protocol/tree/master/examples' title='Code examples'&gt;project repository&lt;/a&gt;. At the moment library is tested and can be used on node.js &amp;amp; browser, also there are no reasons why it would not work in other JS environments.&lt;/p&gt;

&lt;p&gt;I personally think that protocols are much better feet for a JS language than redundant &lt;a href='http://wiki.ecmascript.org/doku.php?id=harmony:classes' title='Classes proposal for ES.next'&gt;classes&lt;/a&gt; and I really wish &lt;a href='http://wiki.ecmascript.org/doku.php?id=harmony:proposals' title='ECMAScript Harmony proposals'&gt;ES.next&lt;/a&gt; was considering them instead!&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>(clojurescripting :intro)</title>
            <link href="http://www.jeditoolkit.com/2012/03/17/clojurescripting-intro.html" rel="alternate" type="text/html" />
            <updated>2012-03-17T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2012/03/17/clojurescripting-intro</id>
            <content type="html">&lt;p&gt;This is a first in a series of posts I&amp;#8217;m planning to write about my &lt;a href='https://github.com/clojure/clojurescript'&gt;clojurescript&lt;/a&gt; learning experience. If you have not heard of clojurescript yet, it&amp;#8217;s a &lt;a href='http://en.wikipedia.org/wiki/Lisp_%28programming_language%29'&gt;lispy&lt;/a&gt; programing language and a flavor of pretty popular &lt;a href='http://clojure.org/'&gt;clojure&lt;/a&gt; that compile targets JS.&lt;/p&gt;

&lt;p&gt;You can find a many impressive &lt;a href='http://clojurescriptone.com/' title='Build single-page, single-language app without refreshes from repl'&gt;demos&lt;/a&gt; and &lt;a href='http://www.chris-granger.com/2012/02/20/overtone-and-clojurescript/' title='Overtone controller in ClojureScript'&gt;blog posts&lt;/a&gt; about clojurescript, but most of them assume you&amp;#8217;re coming from clojure. Even though I read a &lt;a href='http://pragprog.com/book/shcloj/programming-clojure' title='Programming Clojure by Stuart Halloway'&gt;book&lt;/a&gt; and have watched all the &lt;a href='http://blip.tv/clojure' title='Screencasts, talks and tutorials on the Clojure programming language.'&gt;clojure screencasts&lt;/a&gt;, I never managed to dive into it. Now with clojurescript it&amp;#8217;s even more tempting, so I decided to start over again and document every iteration.&lt;/p&gt;

&lt;h2 id='objectives'&gt;Objectives&lt;/h2&gt;

&lt;h4 id='1_reduce_dependencies_to_minimum'&gt;1. Reduce dependencies to minimum&lt;/h4&gt;

&lt;p&gt;Most of the JS developers are spoiled by using a language that has a brilliant runtime, it requires absolutely nothing to get started. Also, nature of JS (everything usually comes over the wire) makes us pretty picky when it comes to introducing dependencies. No matter if it&amp;#8217;s a library or tool we&amp;#8217;d like to avoid it, unless absolutely necessary. So in order to be comfortable hacking with &lt;strong&gt;clojurescript&lt;/strong&gt; my first objective is to &lt;a href='https://github.com/emezeske/lein-cljsbuild/blob/eefe08fe165d6e998a235b20672ec108820abc44/example-projects/simple/project.clj#L4-8'&gt;reduce dependencies&lt;/a&gt; I&amp;#8217;ll use to an absolute minimum.&lt;/p&gt;

&lt;h4 id='2_write_hello_world'&gt;2. Write &amp;#8220;Hello world&amp;#8221;&lt;/h4&gt;

&lt;p&gt;Another objective is of course &amp;#8220;hello world&amp;#8221; application, which in this case will mean writing a clojurescript program that writes &amp;#8220;hello world&amp;#8221; into an html document.&lt;/p&gt;

&lt;h2 id='start_a_package'&gt;Start a package&lt;/h2&gt;

&lt;p&gt;First of all we need to create a project / package. Assuming you know and love &lt;a href='http://npmjs.org/' title='Node package manager'&gt;npm&lt;/a&gt; first thing you will consider doing is finding an equivalent for a given language, which turned out to be a &lt;a href='https://github.com/technomancy/leiningen' title='Leiningen is for automating Clojure projects without setting your hair on fire.'&gt;leiningen&lt;/a&gt;! There are multiple ways one can install it. I used &lt;code&gt;brew install leiningen&lt;/code&gt; since &lt;a href='http://mxcl.github.com/homebrew/' title='The missing package manager for OS X'&gt;homebrew&lt;/a&gt; is a package manager of my choice. Once leiningen is installed, &lt;code&gt;lein new clojurescripting&lt;/code&gt; can be run to generate a blank package. Generated &lt;a href='https://github.com/Gozala/clojurescripting/blob/889645f22b67a9f4c9e421e13cd081b2f24bce2f/project.clj' title='Simple package manifest'&gt;project.clj&lt;/a&gt; file is a package descriptor containing package metadata, equivalent of &lt;a href='http://package.json.nodejitsu.com/' title='CommonJS package descriptor'&gt;package.json&lt;/a&gt; in JS. After some tweaking I ended up with something like this:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Special'&gt;(&lt;/span&gt;defproject clojurescripting &lt;span class='String'&gt;&amp;quot;1.0.0-SNAPSHOT&amp;quot;&lt;/span&gt;
  &lt;span class='Operator'&gt;:description&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;Learning clojurescript&amp;quot;&lt;/span&gt;
  &lt;span class='Operator'&gt;:url&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;&lt;a href='http://documentup.com/gozala/clojurescripting/'&gt;http://documentup.com/gozala/clojurescripting/&lt;/a&gt;&amp;quot;&lt;/span&gt;
  &lt;span class='Operator'&gt;:license&lt;/span&gt; &lt;span class='Special'&gt;{&lt;/span&gt; &lt;span class='Operator'&gt;:name&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;MIT&amp;quot;&lt;/span&gt;
             &lt;span class='Operator'&gt;:url&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;&lt;a href='http://jeditoolkit.com/LICENSE'&gt;http://jeditoolkit.com/LICENSE&lt;/a&gt;&amp;quot;&lt;/span&gt; &lt;span class='Special'&gt;})&lt;/span&gt;
&lt;/pre&gt;
&lt;h2 id='write_some_code'&gt;Write some code&lt;/h2&gt;

&lt;p&gt;The most basic thing I could think of was an &lt;code&gt;alert&lt;/code&gt; dialog with &amp;#8220;Hello World&amp;#8221; message in it. Note that it requires calling JS function out from the clojurescript, which turned out to be trivial:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Special'&gt;(&lt;/span&gt;&lt;span class='PreProc'&gt;ns&lt;/span&gt; clojurescripting.core&lt;span class='Special'&gt;)&lt;/span&gt;

&lt;span class='Special'&gt;(&lt;/span&gt;js/alert &lt;span class='String'&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;&lt;span class='Special'&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;h2 id='compile_to_js'&gt;Compile to JS&lt;/h2&gt;

&lt;p&gt;Now that the code is there, we need to compile it to JS. Clojurescript &lt;a href='https://github.com/clojure/clojurescript/wiki/Quick-Start' title='ClojureScript quick start wiki page'&gt;quick start&lt;/a&gt; document describes how to do that manually, but luckily there is a &lt;a href='https://github.com/emezeske/lein-cljsbuild' title='Leiningen plugin to make ClojureScript development easy.'&gt;lein-cljsbuild&lt;/a&gt; plugin for &lt;a href='https://github.com/technomancy/leiningen' title='Leiningen is for automating Clojure projects without setting your hair on fire.'&gt;leiningen&lt;/a&gt; which can be used to automates this process. To do that you need to configure &lt;a href='https://github.com/Gozala/clojurescripting/blob/043befbc8b73d7506a6742ad9b2fdc2e26830efe/project.clj#L6-10' title='Configuration for lein-cljsbuild'&gt;project.clj&lt;/a&gt; accordingly:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Special'&gt;(&lt;/span&gt;defproject clojurescripting &lt;span class='String'&gt;&amp;quot;1.0.0-SNAPSHOT&amp;quot;&lt;/span&gt;
  &lt;span class='Operator'&gt;:description&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;Learning clojurescript&amp;quot;&lt;/span&gt;
  &lt;span class='Operator'&gt;:url&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;&lt;a href='http://documentup.com/gozala/clojurescripting/'&gt;http://documentup.com/gozala/clojurescripting/&lt;/a&gt;&amp;quot;&lt;/span&gt;
  &lt;span class='Operator'&gt;:license&lt;/span&gt; &lt;span class='Special'&gt;{&lt;/span&gt; &lt;span class='Operator'&gt;:name&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;MIT&amp;quot;&lt;/span&gt;
             &lt;span class='Operator'&gt;:url&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;&lt;a href='http://jeditoolkit.com/LICENSE'&gt;http://jeditoolkit.com/LICENSE&lt;/a&gt;&amp;quot;&lt;/span&gt; &lt;span class='Special'&gt;}&lt;/span&gt;
  &lt;span class='Operator'&gt;:plugins&lt;/span&gt; &lt;span class='Special'&gt;[[&lt;/span&gt;lein-cljsbuild &lt;span class='String'&gt;&amp;quot;0.1.2&amp;quot;&lt;/span&gt;&lt;span class='Special'&gt;]]&lt;/span&gt;
  &lt;span class='Operator'&gt;:cljsbuild&lt;/span&gt; &lt;span class='Special'&gt;{&lt;/span&gt; &lt;span class='Operator'&gt;:builds&lt;/span&gt; &lt;span class='Special'&gt;[{&lt;/span&gt; &lt;span class='Operator'&gt;:source-path&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;src&amp;quot;&lt;/span&gt;
                          &lt;span class='Operator'&gt;:compiler&lt;/span&gt; &lt;span class='Special'&gt;{&lt;/span&gt; &lt;span class='Operator'&gt;:output-to&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;lib/app.js&amp;quot;&lt;/span&gt;
                                      &lt;span class='Operator'&gt;:optimizations&lt;/span&gt; &lt;span class='Operator'&gt;:whitespace&lt;/span&gt;
                                      &lt;span class='Operator'&gt;:pretty-print&lt;/span&gt; &lt;span class='Boolean'&gt;true&lt;/span&gt; &lt;span class='Special'&gt;}}]})&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Once project is configured we need to tell leiningen to install all the dependencies by running &lt;code&gt;lein deps&lt;/code&gt;. The best way to compile cljs to JS is by running &lt;code&gt;lein cljsbuild auto&lt;/code&gt; which will watch source files and automatically recompile to JS on changes. This way ergonomics of refresh-driven development is preserved when working in clojurescript.&lt;/p&gt;

&lt;p&gt;Unfortunately I had to straggle for some time before I figured out why JS file was not generated. The problem is that &lt;code&gt;lein new&lt;/code&gt; generates &lt;code&gt;core.clj&lt;/code&gt; which is a clojure file not a clojurescript one, so one needs to make sure to rename it to &lt;code&gt;core.cljs&lt;/code&gt; instead.&lt;/p&gt;

&lt;h2 id='play'&gt;Play&lt;/h2&gt;

&lt;p&gt;In order to play with a result I needed an html page, so I&amp;#8217;ve created most basic &lt;a href='https://github.com/Gozala/clojurescripting/blob/intro/index.html'&gt;index.html&lt;/a&gt; in the root of the project directory:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Function'&gt;&amp;lt;&lt;/span&gt;&lt;span class='Statement'&gt;body&lt;/span&gt;&lt;span class='Function'&gt;&amp;gt;&lt;/span&gt;&lt;span class='Identifier'&gt;&amp;lt;/&lt;/span&gt;&lt;span class='Statement'&gt;body&lt;/span&gt;&lt;span class='Identifier'&gt;&amp;gt;&lt;/span&gt;
&lt;span class='Function'&gt;&amp;lt;&lt;/span&gt;&lt;span class='Exception'&gt;script&lt;/span&gt;&lt;span class='Function'&gt; &lt;/span&gt;&lt;span class='Type'&gt;src&lt;/span&gt;&lt;span class='Function'&gt;=&lt;/span&gt;&lt;span class='String'&gt;./lib/app.js&lt;/span&gt;&lt;span class='Function'&gt;&amp;gt;&lt;/span&gt;&lt;span class='Identifier'&gt;&amp;lt;/&lt;/span&gt;&lt;span class='Exception'&gt;script&lt;/span&gt;&lt;span class='Identifier'&gt;&amp;gt;&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;As you would expect opening it in a browser displayed alert dialog. But since I wanted to write more clojurescript code I have decided to advance my example just a little bit so it writes &amp;#8220;Hello World&amp;#8221; into document body:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Special'&gt;(&lt;/span&gt;&lt;span class='PreProc'&gt;ns&lt;/span&gt; clojurescripting.core&lt;span class='Special'&gt;)&lt;/span&gt;

&lt;span class='Special'&gt;(&lt;/span&gt;&lt;span class='Define'&gt;defn&lt;/span&gt; set-html
  &lt;span class='String'&gt;&amp;quot;Sets `.innerHTML` of the given tagert element to the give `html`&amp;quot;&lt;/span&gt;
  &lt;span class='Special'&gt;[&lt;/span&gt;target &lt;span class='Special'&gt;&amp;amp;&lt;/span&gt; html&lt;span class='Special'&gt;]&lt;/span&gt;
  &lt;span class='clojureParen1'&gt;(&lt;/span&gt;&lt;span class='Function'&gt;set!&lt;/span&gt; target.innerHTML &lt;span class='clojureParen2'&gt;(&lt;/span&gt;&lt;span class='Function'&gt;apply&lt;/span&gt; &lt;span class='Function'&gt;str&lt;/span&gt; html&lt;span class='clojureParen2'&gt;)&lt;/span&gt;&lt;span class='clojureParen1'&gt;)&lt;/span&gt;&lt;span class='Special'&gt;)&lt;/span&gt;

&lt;span class='Special'&gt;(&lt;/span&gt;&lt;span class='Define'&gt;defn&lt;/span&gt; set-text
  &lt;span class='String'&gt;&amp;quot;Sets `.textContent` of the given `tagret`  to the given `text`&amp;quot;&lt;/span&gt;
  &lt;span class='Special'&gt;[&lt;/span&gt;target &lt;span class='Special'&gt;&amp;amp;&lt;/span&gt; text&lt;span class='Special'&gt;]&lt;/span&gt;
  &lt;span class='clojureParen1'&gt;(&lt;/span&gt;&lt;span class='Function'&gt;set!&lt;/span&gt; target.textContent &lt;span class='clojureParen2'&gt;(&lt;/span&gt;&lt;span class='Function'&gt;apply&lt;/span&gt; &lt;span class='Function'&gt;str&lt;/span&gt; text&lt;span class='clojureParen2'&gt;)&lt;/span&gt;&lt;span class='clojureParen1'&gt;)&lt;/span&gt;&lt;span class='Special'&gt;)&lt;/span&gt;

&lt;span class='Comment'&gt;;; Ineject &amp;quot;Hello world!&amp;quot; into document body.&lt;/span&gt;
&lt;span class='Special'&gt;(&lt;/span&gt;set-html document.body
          &lt;span class='String'&gt;&amp;quot;&amp;lt;div style='background: black; color: white;'&amp;gt;&amp;quot;&lt;/span&gt;
          &lt;span class='String'&gt;&amp;quot;&amp;lt;p&amp;gt;Hello world!&amp;lt;/p&amp;gt;&amp;quot;&lt;/span&gt;
          &lt;span class='String'&gt;&amp;quot;&amp;lt;/div&amp;gt;&amp;quot;&lt;/span&gt;&lt;span class='Special'&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Just a refresh and changes are applied! In fact, clojurescript has a better alternative than page refresh but that&amp;#8217;s topic of the next post!&lt;/p&gt;

&lt;h1 id='summary'&gt;Summary&lt;/h1&gt;

&lt;p&gt;Overall I&amp;#8217;m pretty happy with amount of tooling I had to use in order to write this basic clojurescript powered page. I also really liked &lt;a href='https://github.com/technomancy/leiningen' title='Leiningen is for automating Clojure projects without setting your hair on fire.'&gt;leiningen&lt;/a&gt;&amp;#8217;s plugin system and I hope &lt;a href='http://npmjs.org/' title='Node package manager'&gt;npm&lt;/a&gt; will get something similar at some point in a future. Working with pure JS and DOM on the page is seamless and ergonomics of refresh driven development is preserved! On the flip side, I found leiningen to be a painfully slow (as it&amp;#8217;s written in clojure and runs on JVM). Hopefully they will speed it up by switch to &lt;a href='https://github.com/clojure/clojurescript/wiki/Quick-Start' title='Running ClojureScript on Node.js'&gt;clojurescript on node.js&lt;/a&gt; or &lt;a href='https://github.com/takeoutweight/clojure-scheme' title='Clojure to Scheme to C to the bare metal.'&gt;clojure on bare metal&lt;/a&gt; sometime in a future.&lt;/p&gt;

&lt;p&gt;Code used in this post can be found under my &lt;a href='https://github.com/gozala/clojurescripting/tree/intro'&gt;clojurescripting&lt;/a&gt; repository on github.&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>namespaces</title>
            <link href="http://www.jeditoolkit.com/2012/03/15/namespaces.html" rel="alternate" type="text/html" />
            <updated>2012-03-15T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2012/03/15/namespaces</id>
            <content type="html">&lt;p&gt;I have &lt;a href='/2011/04/11/shareable-private-properties.html#post'&gt;previously blogged&lt;/a&gt; about inability of haveing properties with limited, controlled accessibility &amp;amp; described a hack I prototyped to overcome this limitation. Unfortunately I have failed to explain why one would need such properties, so I plan to explain it in this post. In addition, we already have a &lt;a href='http://wiki.ecmascript.org/doku.php?id=harmony:weak_maps'&gt;WeakMaps&lt;/a&gt; in spidermonkey (and also in V8) that made it possible to implement sharable privates in a proper way, you&amp;#8217;ll find more details about how we did it in Add-on SDK also in this post.&lt;/p&gt;

&lt;h2 id='why_do_we_need_privates'&gt;Why do we need privates&lt;/h2&gt;

&lt;p&gt;Sometimes program is written to work with third party, potentially malicious, code which may use variety of &lt;a href='http://code.google.com/p/google-caja/wiki/AttackVectors'&gt;attack vectors&lt;/a&gt; in order to escalate privileges and do something harmful. This is exact scenario we have in add-on SDK, where we wrap &lt;a href='http://xkcd.com/149/'&gt;sudo powered&lt;/a&gt; browser internals into higher level APIs with reduced capabilities, such that by looking at add-on&amp;#8217;s module graph we&amp;#8217;re able to say what it&amp;#8217;s capable of doing. For example if add-on only requires &lt;a href='https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/addon-kit/docs/notifications.html'&gt;notifications module&lt;/a&gt; we know that at most it can spam user with notifications. Now if we&amp;#8217;ve just stored &lt;a href='http://xkcd.com/149/'&gt;sudo powered&lt;/a&gt; components used to implement these API under &lt;strong&gt;pseudo private&lt;/strong&gt; (&lt;code&gt;_&lt;/code&gt; prefixed) properties we would&amp;#8217;ve had no way of saying what it&amp;#8217;s capable of. There would be no guarantee that add-on code won&amp;#8217;t use those &lt;strong&gt;pseudo privates&lt;/strong&gt; to wipe users hard drive (accidentally or intentionally).&lt;/p&gt;

&lt;p&gt;This is not the only scenario where one would need to have controlled access to the implementation details. Any JS library may be used in an environment where it&amp;#8217;s exposed to code that wishes to override it&amp;#8217;s behavior by monkey-patching it&amp;#8217;s &lt;strong&gt;pseudo privates&lt;/strong&gt;, which is absolutely fine as long as, there is no other code that tries to do the same in a conflicting way. It&amp;#8217;s not to say that we should be paranoid about it, it&amp;#8217;s just there may be things that are not meant to be exposed in order to guarantee desired behavior.&lt;/p&gt;

&lt;h2 id='controlled_access_via_namespaces'&gt;Controlled access via namespaces&lt;/h2&gt;

&lt;p&gt;In Add-on SDK we have a &lt;a href='https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/api-utils/docs/namespace.html'&gt;namespace&lt;/a&gt; module, that may be used to create a namespace functions. These functions are used to access any objects namespaced sub-objects where properties, that are not part of public interface, may be saved:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Identifier'&gt;let&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt; ns &lt;span class='Braces'&gt;}&lt;/span&gt; &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'namespace'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Identifier'&gt;let&lt;/span&gt; foo &lt;span class='Operators'&gt;=&lt;/span&gt; ns&lt;span class='Parens'&gt;()&lt;/span&gt;

foo&lt;span class='Parens'&gt;(&lt;/span&gt;myObject&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;secret &lt;span class='Operators'&gt;=&lt;/span&gt; secret
&lt;/pre&gt;
&lt;p&gt;Now only parties that have access to both &lt;code&gt;foo&lt;/code&gt; and &lt;code&gt;myObject&lt;/code&gt; are capable of seeing a &lt;code&gt;secret&lt;/code&gt;. This approach allows us to create groups of internal properties that may be shared with other components of the program by giving access to the namespace functions. Also note that not only we can use &lt;code&gt;foo&lt;/code&gt; namespace with multiple objects, but we also can use multiple namespaces with the same object:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Identifier'&gt;let&lt;/span&gt; bar &lt;span class='Operators'&gt;=&lt;/span&gt; ns&lt;span class='Parens'&gt;()&lt;/span&gt;

bar&lt;span class='Parens'&gt;(&lt;/span&gt;myObject&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;baz &lt;span class='Operators'&gt;=&lt;/span&gt; secret&lt;span class='Operators'&gt;.&lt;/span&gt;baz

&lt;/pre&gt;
&lt;p&gt;This way we can create namespace per role that objects play in the program and associate properties to that objects in groups based on the roles. As a side effect we also eliminated naming conflicts with in the role object plays as properties are defined in different namespaces. This is very powerful, as we can define &lt;a href='https://github.com/mozilla/addon-sdk/blob/master/packages/api-utils/docs/event/core.md'&gt;event emitter&lt;/a&gt; and many other APIs that may work with a same object safely no matter what&amp;#8217;s their prototypes or own properties look like and no matter how many roles they play at the same time in program.&lt;/p&gt;

&lt;h2 id='under_the_hood'&gt;Under the hood&lt;/h2&gt;

&lt;p&gt;&lt;a href='https://github.com/mozilla/addon-sdk/blob/master/packages/api-utils/lib/namespace.js#L31-39'&gt;Implementation&lt;/a&gt; under the hood is very trivial. In nutshell namespace functions are sugared wrappers around &lt;code&gt;WeakMap&lt;/code&gt; instances, each holding a reference to a &lt;code&gt;WeakMap&lt;/code&gt; instance, which are used to map objects to an associated &amp;#8220;namespaced sub-objects&amp;#8221;, where namespaced properties are stored. Also, since objects are used as keys associated &amp;#8220;namespaced sub-objects&amp;#8221; can be claimed by garbage collector as soon as objects are collected. As an early adopters we had to face a &lt;a href='https://bugzilla.mozilla.org/show_bug.cgi?id=673468'&gt;platform bug&lt;/a&gt; requiring an &lt;a href='https://github.com/mozilla/addon-sdk/blob/master/packages/api-utils/lib/namespace.js#L7-20'&gt;ugly workaround&lt;/a&gt;, but now it&amp;#8217;s fixed and it&amp;#8217;s just matter of time when it ships!&lt;/p&gt;

&lt;h2 id='future'&gt;Future&lt;/h2&gt;

&lt;p&gt;While this is a good enough solution that we plan to migrate all the existing SDK code to, it still has limitations. For example properties are instance specific, or in other words namespaced properties of ancestors (objects in the prototype chain) are not inherited and have to be explicitly accessed, which may feel bit awkward:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Identifier'&gt;let&lt;/span&gt; decedent &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;create&lt;span class='Parens'&gt;(&lt;/span&gt;myObject&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;;&lt;/span&gt;
&lt;span class='String'&gt;'secret'&lt;/span&gt; &lt;span class='Statement'&gt;in&lt;/span&gt; foo&lt;span class='Parens'&gt;(&lt;/span&gt;decedent&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Comment'&gt;// =&amp;gt; false&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;In a future we may have even better solution via &lt;a href='http://wiki.ecmascript.org/doku.php?id=harmony:private_name_objects'&gt;private names&lt;/a&gt;, implementation for spidermonkey is &lt;a href='https://bugzilla.mozilla.org/show_bug.cgi?id=645416'&gt;in progress&lt;/a&gt; so we&amp;#8217;re looking forward!&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>JS Guards</title>
            <link href="http://www.jeditoolkit.com/2011/07/03/guards.html" rel="alternate" type="text/html" />
            <updated>2011-07-03T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2011/07/03/guards</id>
            <content type="html">&lt;p&gt;I’ve written micro library, to solve a very common problem in JS code today. Most likely following example looks familiar:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Function'&gt;function&lt;/span&gt; Constructor&lt;span class='Parens'&gt;(&lt;/span&gt;options&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  options &lt;span class='Operators'&gt;=&lt;/span&gt; options &lt;span class='Operators'&gt;||&lt;/span&gt; &lt;span class='Braces'&gt;{}&lt;/span&gt;
  &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Operator'&gt;typeof&lt;/span&gt; options&lt;span class='Operators'&gt;.&lt;/span&gt;url &lt;span class='Operators'&gt;!==&lt;/span&gt; &lt;span class='String'&gt;'string'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Exception'&gt;throw&lt;/span&gt; &lt;span class='Operator'&gt;new&lt;/span&gt; &lt;span class='Error'&gt;TypeError&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'url is required!'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

  &lt;span class='Comment'&gt;// Finally, your logic here!&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;First of all, code is wrong. If falsy value is passed to a function it will misbehave. Secondly, boilerplate code makes it less readable. It takes three lines to validate just one property (very likely to have more) of the given data structure. Finally, validation code does not even belongs there, as it has nothing to do with an actual logic of a function.&lt;/p&gt;

&lt;p&gt;What we actually need is an annotation enforcing certain constraints. We don&amp;#8217;t have annotations in JS, but we have functions that are omnipotent! Just about everything can be solved everything using functions.&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Function'&gt;function&lt;/span&gt; ConstructorOptions&lt;span class='Parens'&gt;(&lt;/span&gt;options&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;options &lt;span class='Operators'&gt;===&lt;/span&gt; &lt;span class='Keyword'&gt;null&lt;/span&gt; &lt;span class='Operators'&gt;||&lt;/span&gt; options &lt;span class='Operators'&gt;===&lt;/span&gt; &lt;span class='Keyword'&gt;undefined&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
    options &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Braces'&gt;{}&lt;/span&gt;
  &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Operator'&gt;typeof&lt;/span&gt; options &lt;span class='Operators'&gt;!==&lt;/span&gt; &lt;span class='String'&gt;'object'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Exception'&gt;throw&lt;/span&gt; &lt;span class='Operator'&gt;new&lt;/span&gt; &lt;span class='Error'&gt;TypeError&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'Options must an object!'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

  &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Operator'&gt;typeof&lt;/span&gt; options&lt;span class='Operators'&gt;.&lt;/span&gt;url &lt;span class='Operators'&gt;!==&lt;/span&gt; &lt;span class='String'&gt;'string'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
    &lt;span class='Exception'&gt;throw&lt;/span&gt; &lt;span class='Operator'&gt;new&lt;/span&gt; &lt;span class='Error'&gt;TypeError&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'url is required!'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;

&lt;span class='Function'&gt;function&lt;/span&gt; Constructor&lt;span class='Parens'&gt;(&lt;/span&gt;options&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  options &lt;span class='Operators'&gt;=&lt;/span&gt; ConstructorOptions&lt;span class='Parens'&gt;(&lt;/span&gt;options&lt;span class='Parens'&gt;)&lt;/span&gt;

  &lt;span class='Comment'&gt;// Your logic here!&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;This example is much better, because reader of &lt;code&gt;Constructor&lt;/code&gt; function can concentrate on function logic, without being distracted by a validation boilerplate. In addition, &lt;code&gt;options&lt;/code&gt;&amp;#8217; validation code can be independently unit tested and in some cases reused.&lt;/p&gt;

&lt;p&gt;JS &lt;a href='https://github.com/Gozala/guards' title='JS library for data type &amp;amp; data structure validations'&gt;guards&lt;/a&gt;, is a small library that takes this idea to the next level. It provides declarative and recomposable way to define data type &amp;amp; data structure validations.&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Identifier'&gt;var&lt;/span&gt; guards &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'&lt;a href='https://raw.github.com/Gozala/guards/v0.3.0/guards.js'&gt;https://raw.github.com/Gozala/guards/v0.3.0/guards.js&lt;/a&gt;'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

&lt;span class='Comment'&gt;// Define a { x, y } data structure, where x and y fallback to 0.&lt;/span&gt;
&lt;span class='Identifier'&gt;var&lt;/span&gt; Point &lt;span class='Operators'&gt;=&lt;/span&gt; guards&lt;span class='Operators'&gt;.&lt;/span&gt;Schema&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt;
  x&lt;span class='Operators'&gt;:&lt;/span&gt; guards&lt;span class='Operators'&gt;.&lt;/span&gt;&lt;span class='Type'&gt;Number&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;0&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  y&lt;span class='Operators'&gt;:&lt;/span&gt; guards&lt;span class='Operators'&gt;.&lt;/span&gt;&lt;span class='Type'&gt;Number&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;0&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

&lt;span class='Comment'&gt;// Define any type of guard as a function&lt;/span&gt;
&lt;span class='Function'&gt;function&lt;/span&gt; color&lt;span class='Parens'&gt;(&lt;/span&gt;value&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  &lt;span class='Comment'&gt;// If validates just return value&lt;/span&gt;
  &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Operator'&gt;typeof&lt;/span&gt; value &lt;span class='Operators'&gt;===&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;number&amp;quot;&lt;/span&gt; &lt;span class='Operators'&gt;&amp;amp;&amp;amp;&lt;/span&gt; value &lt;span class='Operators'&gt;&amp;lt;=&lt;/span&gt; 255 &lt;span class='Operators'&gt;&amp;amp;&amp;amp;&lt;/span&gt; value &lt;span class='Operators'&gt;&amp;gt;=&lt;/span&gt; 0&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Statement'&gt;return&lt;/span&gt; value
  &lt;span class='Comment'&gt;// If not throw TypeError&lt;/span&gt;
  &lt;span class='Exception'&gt;throw&lt;/span&gt; &lt;span class='Operator'&gt;new&lt;/span&gt; &lt;span class='Error'&gt;TypeError&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;&amp;quot;Color is a number between 0 and 255&amp;quot;&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;

&lt;span class='Comment'&gt;// Define a [0-255, 0-255, 0-255] data structure guard.&lt;/span&gt;
&lt;span class='Identifier'&gt;var&lt;/span&gt; RGB &lt;span class='Operators'&gt;=&lt;/span&gt; guards&lt;span class='Operators'&gt;.&lt;/span&gt;Tuple&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;[&lt;/span&gt; color&lt;span class='Operators'&gt;,&lt;/span&gt; color&lt;span class='Operators'&gt;,&lt;/span&gt; color &lt;span class='Braces'&gt;]&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

&lt;span class='Comment'&gt;// Compose data structure out of existing guards.&lt;/span&gt;
&lt;span class='Identifier'&gt;var&lt;/span&gt; Segment &lt;span class='Operators'&gt;=&lt;/span&gt; guards&lt;span class='Operators'&gt;.&lt;/span&gt;Schema&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt;
  start&lt;span class='Operators'&gt;:&lt;/span&gt; Point&lt;span class='Operators'&gt;,&lt;/span&gt;
  end&lt;span class='Operators'&gt;:&lt;/span&gt; Point&lt;span class='Operators'&gt;,&lt;/span&gt;
  color&lt;span class='Operators'&gt;:&lt;/span&gt; RGB&lt;span class='Operators'&gt;,&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

Segment&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt; end&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt; y&lt;span class='Operators'&gt;:&lt;/span&gt; 23 &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; color&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt;17&lt;span class='Operators'&gt;,&lt;/span&gt; 255&lt;span class='Operators'&gt;,&lt;/span&gt; 0&lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Comment'&gt;// { start: { x: 0, y: 0 }, end: { x: 0, y: 23 }, color: [ 17, 255, 0 ] }&lt;/span&gt;
Segment&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt; start&lt;span class='Operators'&gt;:&lt;/span&gt; 0&lt;span class='Operators'&gt;,&lt;/span&gt; end&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt; y&lt;span class='Operators'&gt;:&lt;/span&gt; 23 &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; color&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt;17&lt;span class='Operators'&gt;,&lt;/span&gt; 255&lt;span class='Operators'&gt;,&lt;/span&gt; 0&lt;span class='Braces'&gt;]&lt;/span&gt; &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Comment'&gt;// TypeError: Object expected instead of number `0`&lt;/span&gt;
Segment&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt; color&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Braces'&gt;[&lt;/span&gt; 10&lt;span class='Operators'&gt;,&lt;/span&gt; 40&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='String'&gt;'30'&lt;/span&gt; &lt;span class='Braces'&gt;]}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Comment'&gt;// TypeError: Color is a number between 0 and 255&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;You can use this library in browsers, jetpack and nodejs. Also, you can try it out right now via &lt;a href='http://jsfiddle.net/gozala/pfnAM/' title='JSFiddle interactive example'&gt;interactive example&lt;/a&gt; on JSFiddle. For more details and examples check out library &lt;a href='http://jeditoolkit.com/guards/docs/' title='Documentation'&gt;documentation&lt;/a&gt;. Finally, there is a &lt;a href='http://wiki.ecmascript.org/doku.php?id=strawman:guards' title='EcmaScript proposal'&gt;guards proposal&lt;/a&gt; for EcmaScript and chances are we&amp;#8217;ll get a syntax sugar some day (It will take a while though as it&amp;#8217;s not even in &lt;a href='http://wiki.ecmascript.org/doku.php?id=harmony:proposals'&gt;Harmony&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;As always I&amp;#8217;m more than happy to hear your feedback!&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>Packageless modules</title>
            <link href="http://www.jeditoolkit.com/2011/06/16/packageless-modules.html" rel="alternate" type="text/html" />
            <updated>2011-06-16T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2011/06/16/packageless-modules</id>
            <content type="html">&lt;h1 id='packages'&gt;Packages&lt;/h1&gt;

&lt;p&gt;Every time I write code in javasciprt for jetpack or &lt;a href='http://nodejs.org/' title='Evented I/O for V8 JavaScript'&gt;nodejs&lt;/a&gt; I question myself: &amp;#8220;Do we really need packages ?&amp;#8221;. Even with amazing package manager like &lt;a href='http://npmjs.org/' title='NodeJS package manager'&gt;npm&lt;/a&gt; it still feels wrong to me for a few reasons:&lt;/p&gt;

&lt;h3 id='1_size_and_scope'&gt;1. Size and scope&lt;/h3&gt;

&lt;p&gt;Writing code for the web taught us keeping line numbers tight, to a bare minimum. It could be a platform limitation, but more importantly it&amp;#8217;s a &lt;a href='http://en.wikipedia.org/wiki/Best_practice' title='Methods or processes that have proven themselves over time'&gt;best practice&lt;/a&gt; that has multiple advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You never overgeneralize by trying to solve each and every problem.&lt;/li&gt;

&lt;li&gt;You reduce a surface for bugs, less lines =&amp;gt; less bugs.&lt;/li&gt;

&lt;li&gt;You build reusable building blocks, with a focus on one thing only. Also, such blocks are easy to mix and match.&lt;/li&gt;

&lt;li&gt;Faster to download, parse, execute.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Most of the modern js libraries / frameworks consist of one file, solve one problem and do it well. &lt;a href='http://microjs.com/' title='Fantastic Micro-Frameworks and Micro-Libraries for Fun and Profit'&gt;MicroJS&lt;/a&gt; started a great job of promoting this trend by building a nice spot for discovering fantastic micro frameworks and libraries!&lt;/p&gt;

&lt;p&gt;Package definition, on the other hand, implies wrapping collection of things into a single form. So no matter how great package manager is, packages will tend to grow in size attempting to solve bigger set of tasks.&lt;/p&gt;

&lt;h3 id='2_explicitness'&gt;2. Explicitness&lt;/h3&gt;

&lt;p&gt;Looking at source should be enough to understand what program does, what it depends on, etc.. Unfortunately this is not the case with packages as it requires looking at the package descriptors and understanding non trivial module search logic. To put it other way &lt;pre&gt;bar &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'foo/bar'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;/pre&gt; expression depends on too many things: requirer module, package descriptor, existence of various files in various places. In jetpack &lt;a href='https://github.com/mozilla/addon-sdk/blob/master/static-files/md/dev-guide/addon-development/module-search.md'&gt;module search&lt;/a&gt; logic ended up being very complex. NodeJS has a better story, but still there more than two axis involved. It should be simple &amp;amp; straight forward.&lt;/p&gt;

&lt;h3 id='3_simplicity'&gt;3. Simplicity&lt;/h3&gt;

&lt;p&gt;When I find some useful &amp;amp; well maintained module on the web, I just want to be able to use it straight away, without downloading and bundling with my code, or even worth creating a package out of it publishing into some centralized registry in order to finally be able to require it! And when I want to alter some functionality in third party module I don&amp;#8217;t want to go through change / rename / publish routine, I just want to fork on github and require!&lt;/p&gt;

&lt;h3 id='4_interoperability'&gt;4. Interoperability&lt;/h3&gt;

&lt;p&gt;Packages are not for browsers, because they have dependencies that have their own dependencies etc.. All this dependencies are expressed in a separate &lt;code&gt;package.json&lt;/code&gt; files and it&amp;#8217;s impossible to interpret &lt;code&gt;require&lt;/code&gt; without reading package descriptor or knowledge of a requirer and I won&amp;#8217;t even go into details of search paths and dependency duplications. Only visible solution for using packages in browsers requires build step on each file change or a server that does this at runtime. It&amp;#8217;s a big compromise to make even though we do builds before deployments.&lt;/p&gt;

&lt;h3 id='5_far_from_harmony'&gt;5. Far from harmony&lt;/h3&gt;

&lt;p&gt;No packages are in harmony, just a &lt;a href='http://wiki.ecmascript.org/doku.php?id=harmony:modules' title='Modules proposal for ES.next'&gt;simple modules&lt;/a&gt; with compile-time resolution and linking. Hopefully very soon, I heard that by end of this year, we will have a first class modules in JavaScript and that will be the ultimate way to distribute building blocks with built-in support from js engines, so closer we stick to the model cheaper will be migration.&lt;/p&gt;

&lt;h1 id='alternative'&gt;Alternative&lt;/h1&gt;

&lt;p&gt;Web is awesome, it&amp;#8217;s cross platform, fully distributed, yet connected via links / URLs.&lt;/p&gt;

&lt;p&gt;I think we should be building ecosystem for building web, with a same spirit: cross-platform interoperable modules, that are fully distributed across the web, still connected via URLs. Each doing one thing only, but doing it well. No need for complicated module search logic or central authority for distribution! I&amp;#8217;m very happy that, &lt;a href='http://wiki.ecmascript.org/doku.php?id=harmony:modules_examples#remote_modules_on_the_web_1' title='Remote module loading examples'&gt;that&amp;#8217;s exactly how modules&lt;/a&gt; are thought to work in Harmony.&lt;/p&gt;

&lt;h1 id='solution'&gt;Solution&lt;/h1&gt;

&lt;p&gt;In my spare time I have prototyped a tool called &lt;a href='https://github.com/Gozala/graphquire/'&gt;graphquire&lt;/a&gt; that makes it possible to write and use such modules, that will work across different platforms including: jetpack, nodejs and browsers. Unfortunately different platforms limitations made it impossible to require modules with an actual URLs, but it very close:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Comment'&gt;// Relative&lt;/span&gt;
&lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'./foo/bar'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'./bla.js'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'../baz'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

&lt;span class='Comment'&gt;// Absolute&lt;/span&gt;
&lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'http!foo.org/bar'&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'https!bla.org/baz.js)&lt;/span&gt;

&lt;/pre&gt;
&lt;p&gt;You might have some constraints:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Long require statements:&lt;/p&gt;

&lt;p&gt;Can be solved in many ways, starting from URL shortening services, finished with a private module registries, similar to &lt;a href='http://microjs.com/' title='Fantastic Micro-Frameworks and Micro-Libraries for Fun and Profit'&gt;MicroJS&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Updating require statements more than 1 place:&lt;/p&gt;

&lt;p&gt;Not really an issue with right set of tools like &lt;a href='https://github.com/harthur/replace'&gt;replace&lt;/a&gt; for example, also editors and even &lt;a href='https://github.com/Gozala/graphquire/'&gt;graphquire&lt;/a&gt; might get build-in support for this. In any case that&amp;#8217;s something we will have to face in harmony.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Content under URL can change:&lt;/p&gt;

&lt;p&gt;This is true, but same can happen in package registries. We just need to make sure to require modules from URLs that are guaranteed to have the same content: modules under version tags, registries, own servers.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Please let me know what you think!&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>Addons in multi process future</title>
            <link href="http://www.jeditoolkit.com/2011/06/04/Addons-in-multi-process-future.html" rel="alternate" type="text/html" />
            <updated>2011-06-04T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2011/06/04/Addons-in-multi-process-future</id>
            <content type="html">&lt;p&gt;The most frequently asked questions on jetpack mailing list concern content scripts. It&amp;#8217;s not obvious for users why these scripts are powerless, why modules don&amp;#8217;t have access to the content&amp;#8217;s dom and why they need to communicate between them by message passing. The short answer is: Mozilla platform is moving towards a model were separate processes are used for chrome, content and add-ons. Therefore SDK modules were designed with &lt;a href='https://wiki.mozilla.org/Electrolysis' title='The Mozilla platform that uses separate processes to display browser UI, web content, and plugins.'&gt;Electrolysis&lt;/a&gt; in mind, to guarantee painless transition to a new model. Much more details about this may be found in the &lt;a href='https://jetpack.mozillalabs.com/sdk/latest/docs/dev-guide/addon-development/web-content.html'&gt;documentation of content scripts&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Even though we won&amp;#8217;t ship 1.0 with support for &lt;a href='https://wiki.mozilla.org/Electrolysis' title='The Mozilla platform that uses separate processes to display browser UI, web content, and plugins.'&gt;Electrolysis&lt;/a&gt;, some of us (&lt;a href='https://github.com/toolness/jetpack-e10s' title='Jetpack + Electrolysis Integration Package'&gt;Atul&lt;/a&gt;, &lt;a href='https://github.com/ochameau/jetpack-oop' title='Experimental use of Out-Of-Process capabilities in firefox 4'&gt;Alex&lt;/a&gt; and &lt;a href='https://github.com/Gozala/vats' title='Prototype implementation of vats for Jetpack'&gt;me&lt;/a&gt;) have explored some ideas, but until few month ago I was still unsure we were heading a right path.&lt;/p&gt;

&lt;p&gt;Several month ago I was reading Peter Michaux&amp;#8217;s blog post about &lt;a href='http://peter.michaux.ca/articles/mvc-architecture-for-javascript-applications' title='I really reccomnd to read this'&gt;MVC Architecture for JavaScript Applications&lt;/a&gt; from which I&amp;#8217;d like to quote following description of &amp;#8220;Real MVC&amp;#8221;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&amp;#8220;In a nutshell the classic MVC architecture works like this. There is a model that is at the heart of the whole thing. If the model changes, it notifies its observers that a change occurred. The view is the stuff you can see and the view observes the model. When the view is notified that the model has changed, the view changes its appearance. The user can interact with the view (e.g. clicking stuff) but the view doesn’t know what to do. So the view tells the controller what the user did and assumes the controller knows what to do. The controller appropriately changes the model. And around and around it goes.&amp;#8221;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Even though I&amp;#8217;ve been writing apps with &lt;a href='http://en.wikipedia.org/wiki/Model-view-controller' title='Model View Controller'&gt;MVC&lt;/a&gt; architecture for a while, it never occurred to me to look at jetpack in MVC.&lt;/p&gt;

&lt;h3 id='light_side'&gt;Light side&lt;/h3&gt;

&lt;p&gt;Ironically, most of Add-on SDK APIs provide access to the things that are basically models, associated with a different views of the browser UI (like &lt;a href='https://jetpack.mozillalabs.com/sdk/latest/docs/packages/addon-kit/docs/windows.html' title='Windows module documentation'&gt;windows&lt;/a&gt;, &lt;a href='https://jetpack.mozillalabs.com/sdk/latest/docs/packages/addon-kit/docs/tabs.html' title='Tabs module documentation'&gt;tabs&lt;/a&gt;, &lt;a href='https://jetpack.mozillalabs.com/sdk/latest/docs/packages/addon-kit/docs/widget.html' title='Widget module documentation'&gt;widgets&lt;/a&gt;). Also, low level modules can be seen as controllers of two types:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Model observers that propagate changes to the associated views.&lt;/li&gt;

&lt;li&gt;View observers that update associated models.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Such perspective also makes whole multi-process architecture switch way more simple. All we need to do is to move models to the other process and make sure that changes on views and models are synchronized via controllers.&lt;/p&gt;

&lt;h3 id='dark_side'&gt;Dark side&lt;/h3&gt;

&lt;p&gt;Unfortunately big part of the code base was not written with such MVC perspective, or in other words we don&amp;#8217;t have a strict separation between models and outlined controller types.&lt;/p&gt;

&lt;h3 id='technical_side'&gt;Technical side&lt;/h3&gt;

&lt;p&gt;This part of the blog is probably too technical to enjoy, unless you plan to hack jetpack itself, so feel free to skip :)&lt;/p&gt;

&lt;p&gt;Overall, everything boils down to few key components:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pipe for delivering messages across processes, something like &lt;code&gt;port&lt;/code&gt; on our worker objects.&lt;/li&gt;

&lt;li&gt;Models that are serializeable and fully reconstructible via JSON. This is important as models are going to live in add-on processes. Model synchronization logic can be encapsulated in one function that just emits events on the port.&lt;/li&gt;

&lt;li&gt;Controllers observing views, or in other words, event listeners that extract and serialize interesting data from events and emits to the pipe so that models can react.&lt;/li&gt;

&lt;li&gt;Controllers observing model changes, via listeners on the pipe and updating views.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Outlined strong separation of roles is a good practice anyway, so I tried to stick to it to make transition to E10S easier.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Models that can be serialized / recreated: &lt;a href='https://github.com/mozilla/addon-sdk/blob/master/packages/addon-kit/lib/hotkeys.js'&gt;hotkey&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Controllers that emit events on view (Browser UI) changes: &lt;a href='https://github.com/mozilla/addon-sdk/blob/master/packages/api-utils/lib/tabs/observer.js'&gt;tabs/observer&lt;/a&gt;, &lt;a href='https://github.com/mozilla/addon-sdk/blob/master/packages/api-utils/lib/keyboard/observer.js'&gt;keyboard/observer&lt;/a&gt;, &lt;a href='https://github.com/mozilla/addon-sdk/blob/master/packages/api-utils/lib/windows/observer.js'&gt;windows/observer&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;I don&amp;#8217;t have examples of controllers that update views on model changes.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally here is a link to &amp;#8220;in progress&amp;#8221; &lt;a href='https://github.com/Gozala/models/blob/master/lib/models.js' title='M form MVC '&gt;implementation of models&lt;/a&gt; that I hope to get into jetpack in a future.&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>Yet another take on inheritance</title>
            <link href="http://www.jeditoolkit.com/2011/04/23/Yet-another-take-on-inheritance.html" rel="alternate" type="text/html" />
            <updated>2011-04-23T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2011/04/23/Yet-another-take-on-inheritance</id>
            <content type="html">&lt;p&gt;Every now and then you need to use inheritance in JavaScript. When you do, you suffer, because there&amp;#8217;s just too many ways to do that and regardless of your choice, syntax noise will make your eyes bleed! Well you may consider saving your eyes by using a library, but this is not going to save you as you&amp;#8217;ll be drown in an ocean of endless choices!&lt;/p&gt;

&lt;p&gt;Choice is good, but when it&amp;#8217;s in manageable quantities. Anyway only reasonable solution is: Yet another new library that does exact same thing in a slightly different fashion.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s a short story of how &lt;a href='https://github.com/Gozala/extendables'&gt;extendables&lt;/a&gt; micro-library started. It is super minimalistic. Function &lt;code&gt;Extendable&lt;/code&gt; is only thing it exports, which is just like built-in &lt;code&gt;Object&lt;/code&gt;. Only difference is &lt;code&gt;extend&lt;/code&gt; own property that behaves similar to &lt;a href='http://documentcloud.github.com/backbone/'&gt;backbone&lt;/a&gt;&amp;#8217;s &lt;code&gt;Model.extend&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Extendable.extend&lt;/code&gt; may be called with an object containing properties that will be defined on the resulting constructor&amp;#8217;s &lt;code&gt;prototype&lt;/code&gt;. Which by the way, will inherit from the target function&amp;#8217;s (function who&amp;#8217;s &lt;code&gt;extend&lt;/code&gt; was called) &lt;code&gt;prototype&lt;/code&gt;. In addition all own properties of a target will be copied to the resulting constructor.&lt;/p&gt;

&lt;p&gt;That&amp;#8217;s actually all of what this library does! In contrast to backbone, library is built with ES5 in mind, which makes it aware of new goodies: non-enumerable, non-writable and non-configurable properties. This also means that it requires ES5 engine to run. But don&amp;#8217;t get broken hearted yet, as you still can use it on IE6 (Please note that you&amp;#8217;ll burn in hell if you do!) and friends, with a help of another micro-lib &lt;a href='https://github.com/kriskowal/es5-shim'&gt;es5-shim&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Library is known to work in browsers via AMD loaders, in &lt;a href='https://jetpack.mozillalabs.com/'&gt;jetpack&lt;/a&gt; and in &lt;a href='http://nodejs.org/'&gt;nodejs&lt;/a&gt;. Likely it&amp;#8217;s going to work on any other CommonJS compliant platform as well. Finally here is a &lt;a href='http://jsfiddle.net/gh/gist/jquery/edge/937354/'&gt;jsfiddle&lt;/a&gt; and source &lt;a href='https://gist.github.com/937354'&gt;gist&lt;/a&gt; showing it off with a few examples:&lt;/p&gt;
&lt;pre&gt;
&lt;span class='Comment'&gt;/* vim:set ts=2 sw=2 sts=2 expandtab */&lt;/span&gt;
&lt;span class='Comment'&gt;/*jshint asi: true undef: true es5: true node: true devel: true&lt;/span&gt;
&lt;span class='Comment'&gt;         forin: true latedef: false supernew: true */&lt;/span&gt;
&lt;span class='Comment'&gt;/*global define: true */&lt;/span&gt;


define&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;'demo'&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Keyword'&gt;exports&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; module&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Keyword'&gt;undefined&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;

&lt;span class='String'&gt;'use strict'&lt;/span&gt;&lt;span class='Operators'&gt;;&lt;/span&gt;

&lt;span class='Identifier'&gt;var&lt;/span&gt; Extendable &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;&amp;quot;&lt;a href='https://github.com/Gozala/extendables/raw/v0.1.2/lib/extendables.js'&gt;https://github.com/Gozala/extendables/raw/v0.1.2/lib/extendables.js&lt;/a&gt;&amp;quot;&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;Extendable

&lt;span class='Identifier'&gt;var&lt;/span&gt; Base &lt;span class='Operators'&gt;=&lt;/span&gt; Extendable&lt;span class='Operators'&gt;.&lt;/span&gt;extend&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt;
  inherited&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt; inherited&lt;span class='Parens'&gt;()&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Statement'&gt;return&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;inherited property&amp;quot;&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  overridden&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt; overridden&lt;span class='Parens'&gt;()&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Statement'&gt;return&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;property to override&amp;quot;&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  &lt;span class='Comment'&gt;// No supers by default, use prototype and be proud, but if you really want&lt;/span&gt;
  &lt;span class='Comment'&gt;// super get one!&lt;/span&gt;
  _super&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt; _super&lt;span class='Parens'&gt;()&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Statement'&gt;return&lt;/span&gt; &lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;getPrototypeOf&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;getPrototypeOf&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Parens'&gt;))&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Comment'&gt;// Adding static method.&lt;/span&gt;
Base&lt;span class='Operators'&gt;.&lt;/span&gt;implement &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt; implement&lt;span class='Parens'&gt;(&lt;/span&gt;source&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
  &lt;span class='Comment'&gt;// Going through each argument to copy properties from each source.&lt;/span&gt;
  &lt;span class='Type'&gt;Array&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;&lt;span class='Keyword'&gt;prototype&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;forEach&lt;span class='Operators'&gt;.&lt;/span&gt;call&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Keyword'&gt;arguments&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;source&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Comment'&gt;// Going through each own property of the source to copy it.&lt;/span&gt;
    &lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;getOwnPropertyNames&lt;span class='Parens'&gt;(&lt;/span&gt;source&lt;span class='Parens'&gt;)&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;forEach&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Function'&gt;function&lt;/span&gt;&lt;span class='Parens'&gt;(&lt;/span&gt;key&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
      &lt;span class='Comment'&gt;// If property is already owned then skip it.&lt;/span&gt;
      &lt;span class='Statement'&gt;if&lt;/span&gt; &lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;&lt;span class='Keyword'&gt;prototype&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;hasOwnProperty&lt;span class='Operators'&gt;.&lt;/span&gt;call&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;&lt;span class='Keyword'&gt;prototype&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; key&lt;span class='Parens'&gt;))&lt;/span&gt; &lt;span class='Statement'&gt;return&lt;/span&gt; &lt;span class='Keyword'&gt;null&lt;/span&gt;
      &lt;span class='Comment'&gt;// Otherwise define property.&lt;/span&gt;
      &lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;defineProperty&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;&lt;span class='Keyword'&gt;prototype&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; key&lt;span class='Operators'&gt;,&lt;/span&gt;
                            &lt;span class='Type'&gt;Object&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;getOwnPropertyDescriptor&lt;span class='Parens'&gt;(&lt;/span&gt;source&lt;span class='Operators'&gt;,&lt;/span&gt; key&lt;span class='Parens'&gt;))&lt;/span&gt;
    &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt; &lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;

&lt;span class='Identifier'&gt;var&lt;/span&gt; b1 &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Operator'&gt;new&lt;/span&gt; Base
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;b1 &lt;span class='Operator'&gt;instanceof&lt;/span&gt; Base&lt;span class='Parens'&gt;)&lt;/span&gt;              &lt;span class='Comment'&gt;// -&amp;gt; true&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;b1 &lt;span class='Operator'&gt;instanceof&lt;/span&gt; Extendable&lt;span class='Parens'&gt;)&lt;/span&gt;        &lt;span class='Comment'&gt;// -&amp;gt; true&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;b1&lt;span class='Operators'&gt;.&lt;/span&gt;inherited&lt;span class='Parens'&gt;())&lt;/span&gt;                  &lt;span class='Comment'&gt;// -&amp;gt; &amp;quot;inherited property&amp;quot;&lt;/span&gt;

&lt;span class='Identifier'&gt;var&lt;/span&gt; b2 &lt;span class='Operators'&gt;=&lt;/span&gt; Base&lt;span class='Parens'&gt;()&lt;/span&gt;                             &lt;span class='Comment'&gt;// -&amp;gt; Works same as without `new`&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;b2 &lt;span class='Operator'&gt;instanceof&lt;/span&gt; Base&lt;span class='Parens'&gt;)&lt;/span&gt;             &lt;span class='Comment'&gt;// -&amp;gt; true&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;b2 &lt;span class='Operator'&gt;instanceof&lt;/span&gt; Extendable&lt;span class='Parens'&gt;)&lt;/span&gt;       &lt;span class='Comment'&gt;// -&amp;gt; true&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;b2&lt;span class='Operators'&gt;.&lt;/span&gt;inherited&lt;span class='Parens'&gt;())&lt;/span&gt;                 &lt;span class='Comment'&gt;// -&amp;gt; &amp;quot;inherited property&amp;quot;&lt;/span&gt;


&lt;span class='Identifier'&gt;var&lt;/span&gt; Decedent &lt;span class='Operators'&gt;=&lt;/span&gt; Base&lt;span class='Operators'&gt;.&lt;/span&gt;extend&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt;
  &lt;span class='Keyword'&gt;constructor&lt;/span&gt;&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt; Decedent&lt;span class='Parens'&gt;(&lt;/span&gt;options&lt;span class='Parens'&gt;)&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;name &lt;span class='Operators'&gt;=&lt;/span&gt; options&lt;span class='Operators'&gt;.&lt;/span&gt;name&lt;span class='Operators'&gt;;&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  overridden&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt; override&lt;span class='Parens'&gt;()&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Comment'&gt;// I'd rather copied `overridden` with a diff name overriddenBase for&lt;/span&gt;
    &lt;span class='Comment'&gt;// example or used `Base.prototype.overridden.call(this)`&lt;/span&gt;
    &lt;span class='Comment'&gt;// But this works as well :)&lt;/span&gt;
    &lt;span class='Statement'&gt;return&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;No longer &amp;quot;&lt;/span&gt; &lt;span class='Operators'&gt;+&lt;/span&gt; &lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;_super&lt;span class='Parens'&gt;()&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;overridden&lt;span class='Operators'&gt;.&lt;/span&gt;call&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
  &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Operators'&gt;,&lt;/span&gt;
  &lt;span class='Comment'&gt;// overriddenBase: Base.prototype.overridden&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
Decedent&lt;span class='Operators'&gt;.&lt;/span&gt;implement&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt;
  bye&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='Function'&gt;function&lt;/span&gt; bye&lt;span class='Parens'&gt;()&lt;/span&gt; &lt;span class='Braces'&gt;{&lt;/span&gt;
    &lt;span class='Statement'&gt;return&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;Buy my dear &amp;quot;&lt;/span&gt; &lt;span class='Operators'&gt;+&lt;/span&gt; &lt;span class='Keyword'&gt;this&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;name
  &lt;span class='Braces'&gt;}&lt;/span&gt;
&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;

&lt;span class='Identifier'&gt;var&lt;/span&gt; d1 &lt;span class='Operators'&gt;=&lt;/span&gt; &lt;span class='Operator'&gt;new&lt;/span&gt; Decedent&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='Braces'&gt;{&lt;/span&gt; name&lt;span class='Operators'&gt;:&lt;/span&gt; &lt;span class='String'&gt;&amp;quot;friend&amp;quot;&lt;/span&gt; &lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;d1 &lt;span class='Operator'&gt;instanceof&lt;/span&gt; Decedent&lt;span class='Parens'&gt;)&lt;/span&gt;       &lt;span class='Comment'&gt;// -&amp;gt; true&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;d1 &lt;span class='Operator'&gt;instanceof&lt;/span&gt; Base&lt;span class='Parens'&gt;)&lt;/span&gt;           &lt;span class='Comment'&gt;// -&amp;gt; true&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;d1 &lt;span class='Operator'&gt;instanceof&lt;/span&gt; Extendable&lt;span class='Parens'&gt;)&lt;/span&gt;     &lt;span class='Comment'&gt;// -&amp;gt; true&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;d1&lt;span class='Operators'&gt;.&lt;/span&gt;inherited&lt;span class='Parens'&gt;())&lt;/span&gt;               &lt;span class='Comment'&gt;// -&amp;gt; &amp;quot;inherited property&amp;quot;&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;d1&lt;span class='Operators'&gt;.&lt;/span&gt;overridden&lt;span class='Parens'&gt;())&lt;/span&gt;              &lt;span class='Comment'&gt;// -&amp;gt; No longer a property to override&lt;/span&gt;
&lt;span class='Keyword'&gt;console&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;log&lt;span class='Parens'&gt;(&lt;/span&gt;d1&lt;span class='Operators'&gt;.&lt;/span&gt;bye&lt;span class='Parens'&gt;())&lt;/span&gt;                     &lt;span class='Comment'&gt;// -&amp;gt; &amp;quot;Bye my dear friend&amp;quot;&lt;/span&gt;

&lt;span class='Braces'&gt;}&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;span class='Keyword'&gt;require&lt;/span&gt;&lt;span class='Operators'&gt;.&lt;/span&gt;main&lt;span class='Parens'&gt;(&lt;/span&gt;&lt;span class='String'&gt;&amp;quot;demo&amp;quot;&lt;/span&gt;&lt;span class='Parens'&gt;)&lt;/span&gt;
&lt;/pre&gt;
&lt;p&gt;Finally, if you are a &lt;a href='http://jashkenas.github.com/coffee-script/'&gt;coffee&lt;/a&gt; fan like me, you&amp;#8217;ll love the fact that regular coffee class syntax may be used to extend &lt;code&gt;Extendable&lt;/code&gt; and it&amp;#8217;s decedents. Also all coffee classes that extend &lt;code&gt;Extendable&lt;/code&gt; can be extended further from javascript.&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>Shareable private properties</title>
            <link href="http://www.jeditoolkit.com/2011/04/11/shareable-private-properties.html" rel="alternate" type="text/html" />
            <updated>2011-04-11T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2011/04/11/shareable-private-properties</id>
            <content type="html">&lt;p&gt;In JavaScript it is not possible to create properties that have limited or controlled accessibility. It is possible to create non-enumerable and non-writable properties, but still they can be discovered and accessed. Usually so called &amp;#8220;closure capturing&amp;#8221; pattern is used to encapsulate such properties in lexical scope:&lt;/p&gt;
&lt;script src='https://gist.github.com/915169.js?file=closure-capturing.js'&gt;
function Base(options) {
  var _secret = options.secret
  this.hello = function hello() {
    return 'Hello ' + _secret
  }
}

function Decedent(options) {
  var _secret = options.secret
  Base.call(this, options)
  this.bye = function bye() {
    return 'Bye ' + _secret
  }
  this.boom = function boom() {
    _secret = 'boom !'
  }
}
Decedent.prototype = Object.create(Base.prototype)
&lt;/script&gt;
&lt;p&gt;Unfortunately given approach does not works very well with OO inheritance:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code readability degrades and some code duplication becomes necessary.&lt;br /&gt;&lt;em&gt;(See firs lines of &lt;code&gt;Base&lt;/code&gt; and &lt;code&gt;Decedent&lt;/code&gt; constructors)&lt;/em&gt;.&lt;/li&gt;

&lt;li&gt;Prototypes do not contain any properties and there for each instance gets it&amp;#8217;s own fresh copy of them.&lt;br /&gt;&lt;em&gt;(Not very good for performance)&lt;/em&gt;.&lt;/li&gt;

&lt;li&gt;Own and inherited methods access different copies of private properties and there for changes do not propagate.&lt;br /&gt;&lt;em&gt;(&lt;code&gt;boom&lt;/code&gt; method of &lt;code&gt;Decedent&lt;/code&gt; illustrates the issue).&lt;/em&gt;&lt;/li&gt;

&lt;li&gt;Access to a private properties can not be shared with a limited group.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I believe because of these reasons &amp;#8221;&lt;code&gt;-&lt;/code&gt;&amp;#8221; prefix was established as a &amp;#8220;de facto&amp;#8221; standard for private property names. Unfortunately it&amp;#8217;s just a coding style that does not implies any property access restrictions.&lt;/p&gt;

&lt;p&gt;In Jetpack we&amp;#8217;ve explored &lt;a href='https://jetpack.mozillalabs.com/sdk/1.0b4/docs/packages/api-utils/docs/cortex.html'&gt;different techniques&lt;/a&gt; to overcome issues mentioned above, but property access sharing to the limited groups of consumers remained painful and unobvious.&lt;/p&gt;

&lt;p&gt;Recently I got an &lt;a href='https://github.com/mozilla/addon-sdk/blob/fe8f35933c5f71b3fdc698e6768d9e51e6b5ad4e/packages/addon-kit/lib/context-menu.js#L342-348'&gt;interesting idea&lt;/a&gt; inspired by &lt;a href='http://wiki.ecmascript.org/doku.php?id=strawman:private_names'&gt;private names proposal&lt;/a&gt; for ECMAScript &lt;em&gt;(I recommend to reading &lt;a href='http://www.wirfs-brock.com/allen/posts/32'&gt;Allen Wirfs blog post&lt;/a&gt; explaining details of the proposal)&lt;/em&gt;. Here is an illustration of the idea with a same example:&lt;/p&gt;
&lt;script src='https://gist.github.com/915169.js?file=idea.js'&gt;
var KEY = {};   // key for accessing private properties.

function Base(options) {
  // Defining all the private properties on the local object.
  var privates = { secret: options.secret }
  // Override `Object.prototype.valueOf` to share access to the private
  // properties with a limited group of consumers who has an access to the
  // `KEY`.
  this.valueOf = function valueOf(key) {
    // If consumer has a right `key` we give access to our private properties,
    // otherwise we fall back to a default behaviour.
    return key === KEY ? privates : Object.prototype.valueOf.call(this)
  }
}
Base.prototype.hello = function hello() {
  return 'Hello ' + this.valueOf(KEY).secret
}

function Decedent(options) {
  Base.call(this, options)
}
Decedent.prototype = Object.create(Base.prototype)
Decedent.prototype.bye = function bye() {
  return 'Bye ' + this.valueOf(KEY).secret
}
Decedent.prototype.boom = function boom() {
  this.valueOf(KEY).secret = 'boom !'
}
&lt;/script&gt;
&lt;p&gt;Please note that this solves all the issues outlined. Also access to the privates can be shared with any group, just by giving them a &lt;code&gt;KEY&lt;/code&gt;. After tinkering with this idea for some time, I&amp;#8217;m happy to announce birth of &lt;a href='https://github.com/Gozala/namespace'&gt;namespace&lt;/a&gt; micro-library. Below you can see same example, but this time coded as a commonjs module and using mentioned library:&lt;/p&gt;
&lt;script src='https://gist.github.com/915169.js?file=fiddle.js'&gt;
define('demo', function(require, exports, module, undefined) {

'use strict'

// importing library.
var Namespace = require('http://jeditoolkit.com/namespace/lib/namespace.js').Namespace
// Creating namespace for private properties.
var _ = new Namespace

function Base(options) {
  _(this).secret = options.secret
}
Base.prototype.hello = function hello() {
  return 'Hello ' + _(this).secret
}

function Decedent(options) {
  Base.call(this, options)
}
Decedent.prototype = Object.create(Base.prototype)
Decedent.prototype.bye = function bye() {
  return 'Bye ' + _(this).secret
}
Decedent.prototype.boom = function boom() {
  _(this).secret = 'boom !'
}

var base = new Base({ secret: 'Father' })

alert(base.secret)      // -&gt; undefined
alert(base.hello())     // -&gt; 'Hello Father'

var decedent = new Decedent({ secret: 'Yoda' })

alert(decedent.hello())   // -&gt; 'Hello Yoda'
alert(decedent.bye())     // -&gt; 'Bye Yoda'
decedent.boom();

alert(decedent.hello())   // -&gt; 'Hello boom !'
alert(decedent.bye())     // -&gt; 'Bye boom !'

})

// Loading module &quot;demo&quot;
require.main(&quot;demo&quot;)
&lt;/script&gt;
&lt;p&gt;Library not only wraps outlined idea into a nice API, but also allows provides a way to use multiple namespaces with the same object.&lt;/p&gt;

&lt;p&gt;Finally here is a &lt;a href='http://jsfiddle.net/gh/gist/jquery/edge/915169/'&gt;jsfiddle link&lt;/a&gt; containing this example if you wan to play with it. Of course comments / feedback / ideas is more then welcome!&lt;/p&gt;</content>
        </entry>
    
        <entry>
            <title>Evolving VS Adjusting</title>
            <link href="http://www.jeditoolkit.com/2010/10/20/evolving-VS-adjusting.html" rel="alternate" type="text/html" />
            <updated>2010-10-20T00:00:00-07:00</updated>
            <id>http://www.jeditoolkit.com/2010/10/20/evolving-VS-adjusting</id>
            <content type="html">&lt;p&gt;We often have to make the choice between adjusting to conditions or paying a price for improving them. Usually it is safer to adjust, but still we have to be very careful we don&amp;#8217;t miss an opportunity to improve. Probably the best way to do that is to compare the price and improvements.&lt;/p&gt;

&lt;p&gt;&lt;a href='http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition'&gt;Asynchronous Module Definition&lt;/a&gt; is one of the hottest topics on the &lt;a href='http://www.commonjs.org/'&gt;CommonJS&lt;/a&gt; &lt;a href='http://groups.google.com/group/commonjs' title='CommonJS mailing list'&gt;mailing list&lt;/a&gt; this days. Unfortunately my impression is that most of the participants of these debates forget to measure price VS improvement and this can be harmful.&lt;/p&gt;

&lt;p&gt;I believe there are two sides:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;People that believe that &lt;a href='http://wiki.commonjs.org/wiki/Modules/1.0' title='Modules/1.0 - CommonJS Spec'&gt;CommonJS modules&lt;/a&gt; are not well suited for in-browser usage (suggesting an alternative, &amp;#8220;browser friendly&amp;#8221; syntax).&lt;/li&gt;

&lt;li&gt;People who believe that &lt;a href='http://wiki.commonjs.org/wiki/Modules/1.0' title='Modules/1.0 - CommonJS Spec'&gt;CommonJS modules&lt;/a&gt; are perfectly usable within browser without any changes.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I&amp;#8217;m one among few others who uses CommonJS modules both on the server and in the browser already. With that in mind it&amp;#8217;s not easy to convince me, that change in well established standard is absolutely necessary.&lt;/p&gt;

&lt;h3 id='issues_with_current_modules'&gt;Issues with current modules&lt;/h3&gt;

&lt;p&gt;There were many arguments why current modules are not suited for in-browser usage, but unfortunately I did not manage to convince enough people to make a list of them, so I will summarise them as: &amp;#8220;Non of the existing module loading techniques provides perfect debugging experience across the different browsers without involvement of a server component or a building step.&amp;#8221;&lt;/p&gt;

&lt;h3 id='different_view_on_these_issues'&gt;Different view on these issues&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Debugging story is not that bad actually (much better than it used to be not a long time ago), but it&amp;#8217;s true, rarely, but still strange issues occur. In any case those are bugs in debuggers not with a modules. Only way to fix bugs is by report them. I&amp;#8217;m pretty confident that &lt;a href='http://getfirebug.com/'&gt;firebug&lt;/a&gt; team is going to make their best, to make debugging experience as good as possible. I also hope that it&amp;#8217;s true for a web inspector.&lt;/li&gt;

&lt;li&gt;Dismissing a server as a show stopper is also inadequate IMO, since there are few other things like &lt;a href='http://www.w3.org/TR/offline-webapps/#offline'&gt;Offline Application Caching&lt;/a&gt; that would still depend on it and won&amp;#8217;t work without. And in the end it just matter of running one simple command before starting development. For instance to hack on jetpack you need to run &lt;code&gt;source bin/activate&lt;/code&gt; and I believe it was show stopper for anyone.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id='issues_with_proposed_changes'&gt;Issues with proposed changes&lt;/h3&gt;

&lt;p&gt;Proposed changes are not perfect either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Some people will argue against this statement, but there is nothing in common between proposed &lt;a href='http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition'&gt;Asynchronous Module Definition&lt;/a&gt; and existing &lt;a href='http://wiki.commonjs.org/wiki/Modules/1.0' title='Modules/1.0 - CommonJS Spec'&gt;module&lt;/a&gt; specs other then the CommonJS label. This will be a source of confusion and community fragmentation.&lt;/p&gt;

&lt;p&gt;Below you can find a simple module being rewritten according to two different specs:&lt;/p&gt;

&lt;p&gt;Asynchronous definition:&lt;/p&gt;
&lt;script src='http://gist.github.com/636422.js?file=asincdefinition.js'&gt;
     define(&quot;foo&quot;, [&quot;bar&quot;], function (bar) {
         return {
           doAnotherThing: function() {
             return bar.doSomething() + 2;
           }
         };
     });
&lt;/script&gt;
&lt;p&gt;CommonJS module:&lt;/p&gt;
&lt;script src='http://gist.github.com/636422.js?file=commonjs-module.js'&gt;
     var bar = require(&quot;bar&quot;);
     exports.doAnotherThing = function() {
       return bar.doSomething() + 2;
     }
&lt;/script&gt;&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;Asynchronous definition seem to encourage handwritten boilerplate but what is puzzling here is, why would any programmer prefer writing a boilerplate code by hand over writing a program that would do that for him. Yes it&amp;#8217;s nice to refresh browser and be ready to go without any build step, but nothing prevents us form writing programs that support this workflow.&lt;/p&gt;
&lt;/li&gt;

&lt;li&gt;
&lt;p&gt;A most important issue is that the new proposal suggests to adjust to the unpleasant situation browser are in now, and what is even worse, it brings this mess to the server. Adjustment to broken tooling is only going to delay innovation! I truly believe that if we had had such a big adoption of CommonJS modules a few years ago, we would have perfect debugging story today (well maybe not in all browsers :). I also think that wide adoption of CommonJS modules already triggered some innovation and partially that is the reason why we might get native modules in harmony. And you know what ? Once we&amp;#8217;ll get them, no magic will happen, as it did not happened when Web Workers arrived. It will take some time to get a decent tooling support. Again it should not stop innovation by trying to adjust new amazing platforms to the mess we are at with browsers.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id='evolving'&gt;Evolving&lt;/h3&gt;

&lt;p&gt;I think that the only sane thing to do now is to bring the innovation that happens in server side javascript back to the browser without compromising anything. With that in mind I&amp;#8217;m planning to put a lot of effort in &lt;span&gt;teleport&lt;/span&gt;, a tool that I wrote some time ago. It allowed few projects to share CommonJS modules across server and client. I don&amp;#8217;t believe there is any perfect solution to all problems, but each problem has a solution and I hope that &lt;span&gt;teleport&lt;/span&gt; will be a set of solutions addressing CommonJS module loading problems. The &lt;a href='http://github.com/Gozala/teleport/issues'&gt;list of tasks&lt;/a&gt; I&amp;#8217;ll be working on, is probably the best way to get a better idea what is it all about. Feel free to fork me, suggest new ideas or just comment here.&lt;/p&gt;</content>
        </entry>
    
</feed>
