<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Infrequently Noted &#187; javascript</title>
	<atom:link href="http://infrequently.org/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://infrequently.org</link>
	<description></description>
	<lastBuildDate>Fri, 18 Nov 2011 16:25:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>JavaScript UXO Removal Updated</title>
		<link>http://infrequently.org/2010/09/javascript-uxo-removal/</link>
		<comments>http://infrequently.org/2010/09/javascript-uxo-removal/#comments</comments>
		<pubDate>Tue, 14 Sep 2010 21:07:00 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[dom]]></category>

		<guid isPermaLink="false">http://infrequently.org/?p=1425</guid>
		<description><![CDATA[JavaScript is a lovable language. Real closures, first class functions, incredibly dynamic behavior&#8230;it&#8217;s a joy when you know it well. Less experienced JS programmers often feel as though they&#8217;re waltzing in a minefield, though. At many steps along the path to JS enlightenment everything feels like it&#8217;s breaking down around you. The lack of block [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript is a lovable language. Real closures, first class functions, incredibly dynamic behavior&#8230;it&#8217;s a joy when you know it well.</p>
<p>Less experienced JS programmers often feel as though they&#8217;re waltzing in a minefield, though. At many steps along the path to JS enlightenment everything feels like it&#8217;s breaking down around you. The lack of block lexical scope sends you on pointless errands, the various OO patterns give you fits as you try to do anything but what&#8217;s in the examples, and before you know it even the trusty &#8220;dot&#8221; operator starts looking suspect. What do you <em>mean</em> that <code>this</code> doesn&#8217;t point to the object I got the function from?</p>
<p>Repairs for some of the others are on the way in ES6 so I want to focus on the badly  botched situation regarding &#8220;promiscuous <code>this</code>&#8220;, in particular how ES5 has done us few favors and why we&#8217;re slated to continue the cavalcade of failure should parts of the language sprout auto-binding.</p>
<p>Here&#8217;s the problem in 5 lines:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  _counter<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
  inc<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._counter<span style="color: #339933;">++;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
node.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> obj.<span style="color: #660066;">inc</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>See the issue? <code>obj.inc</code> results in a reference to the <code>inc</code> method <em>without</em> any handle or reference to its original context (<code>obj</code>). This is asymmetric with the behavior we see when we directly call methods since in that case the dot operator populates the <code>ThisBinding</code> scope. We can see it clearly when we assign to intermediate variables:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// global &quot;_counter&quot;, we'll see why later</span>
<span style="color: #003366; font-weight: bold;">var</span> inc <span style="color: #339933;">=</span> obj.<span style="color: #660066;">inc</span><span style="color: #339933;">;</span>
obj.<span style="color: #660066;">inc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 1</span>
obj.<span style="color: #660066;">inc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 2</span>
inc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 1</span></pre></div></div>

<p>Reams have been written on the topic, and ES5&#8242;s belated and weak answer is to directly transcribe what JS libraries have been doing by providing a <code>bind()</code> method that returns <em>a new function object</em> that carries the correct <code>ThisBinding</code>. Notably, you can&#8217;t un-bind a bound function object, nor can you treat a bound function as equal to its unbound ancestor. This, then, is just an API formalism around the pattern of using closures to carry the <code>ThisBinding</code> object around:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> bind <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> <span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">return</span> obj<span style="color: #009900;">&#91;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> arguments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Event handling now looks like:</span>
<span style="color: #006600; font-style: italic;">//   node.addEventListener(&quot;click&quot;, bind(obj, &quot;inc&quot;));</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> inc <span style="color: #339933;">=</span> bind<span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;inc&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
obj.<span style="color: #660066;">inc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 1</span>
obj.<span style="color: #660066;">inc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 2</span>
inc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 3</span>
&nbsp;
inc <span style="color: #339933;">===</span> obj.<span style="color: #660066;">inc</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></div></div>

<p>ES5&#8242;s syntax is little better but it is built-in and can potentially perform much better:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> inc <span style="color: #339933;">=</span> obj.<span style="color: #660066;">inc</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// In a handler:</span>
node.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> obj.<span style="color: #660066;">inc</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Syntax aside, we didn&#8217;t actually solve the big problems since unbound functions <em><b>can still exist</b></em>, meaning we still have to explain to developers that they need to think of the dot operator doing different things based on what charachters happen to come <em>after</em> the thing on the right-hand side of the dot. Worse, when you get a function it can either be strongly-bound (i.e., it breaks the <code>.call(otherThis, ...)</code> convention) or unbound &#8212; potentially executing in the &#8220;wrong&#8221; <code>ThisBinding</code>. And there&#8217;s no way to tell which is which.</p>
<p>So what would be better?</p>
<p>It occurs to me that what we need isn&#8217;t automatic binding for some methods, syntax for easier binding, or even automatic binding for all methods. No, what we really want is <em>weak binding</em>; the ability to retrieve a function object through the dot operator and have it do the right thing <em>until you say otherwise</em>.</p>
<p>We can think of weak binding as adding an annotation about the source object to a reference. Each de-reference via [[Get]] creates a new weak binding which is then used when a function is called. This has the side effect of describing current [[Get]] behavior when calling methods (since the de-reference would carry the binding and execution can be described separately). As a bonus, this gives us the re-bindability that JS seems to imply should be possible thanks to the <code>.call(otherThis)</code> contract:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> o <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  log<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">msg</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
  msg<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;hello, world!&quot;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> o2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  msg<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;howdy, pardner!&quot;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
o.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;hello, world!&quot;</span>
o2.<span style="color: #660066;">log</span> <span style="color: #339933;">=</span> o.<span style="color: #660066;">log</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// calling log through o2 replaces weak binding</span>
o2.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// &quot;howdy, pardner!&quot;</span></pre></div></div>

<p>But won&#8217;t this break the entire interwebs!?!?</p>
<p>Maybe not. Hear me out.</p>
<p>We&#8217;ve already seen our pathological case in earlier examples. Here&#8217;s the node listener use-case again, this time showing us exactly what context is being used for unbound methods:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">document.<span style="color: #660066;">body</span>.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span> <span style="color: #339933;">==</span> document.<span style="color: #660066;">body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true in Chrome and FF today</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can think of dispatch of the event calling the anonymous function with explicit <code>ThisBinding</code>, using something like <code>listener.call(document.body, evt);</code> as the call signature for each registered handler in the capture phase. Now, it&#8217;s pretty clear that this is whack. DOM dispatch changing the <code>ThisBinding</code> of passed listeners is an incredibly strange side-effect and means that even if we add weak binding, this context doesn&#8217;t change. At this point though we can clearly talk about the DOM API bug in the context of sane, consistent language behavior. The fact that event listeners won&#8217;t preserve weak binding and will continue to require something like this is an issue that can be wrestled down in <em>one</em> working group:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">node.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> 
       <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>otherThis<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
       <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The only case I can think of when weak bindings will change program semantics is when unbound method calls in the global object do work on <code>this</code> in a way that is intentional. We have this contrived example from before too, but as you can see, it sure looks like a bug, no?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> _counter <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// a.k.a.: &quot;this._counter&quot;, a.k.a.: &quot;window._counter&quot;</span>
<span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  _counter<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span>
  inc<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>._counter<span style="color: #339933;">++;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> inc <span style="color: #339933;">=</span> obj.<span style="color: #660066;">inc</span><span style="color: #339933;">;</span>
obj.<span style="color: #660066;">inc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 1</span>
obj.<span style="color: #660066;">inc</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 2</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>obj._counter<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>._counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 2, 0</span>
inc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 1</span>
inc<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 2</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>obj._counter<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>._counter<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// 2, 2</span></pre></td></tr></table></div>

<p>If this turns out to be a problem in real code, we can just hide weak bindings behind some <code>use</code> directive.</p>
<p>Weak binding now gives us a middle ground: functions that are passed to non-pathological callback systems &#8220;do the right thing&#8221;, most functions that would otherwise need to have been bound explicitly can Just Work (and can be rebound to boot), and the wonky [[Get]] vs. [[Call]] behavior of the dot operator is resolved in a tidy way. One less bit of unexploded ordinance removed.</p>
<p>So the question now is: why won&#8217;t this work? TC39 members, what&#8217;s to keep us from doing this in ES6?</p>
<p><b>Update:</b> Mark Miller flags what looks to be a critical flaw:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  callbacks<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
  register<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> i <span style="color: #339933;">&lt;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">callbacks</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">callbacks</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
obj.<span style="color: #660066;">register</span><span style="color: #009900;">&#40;</span>foo.<span style="color: #660066;">bar</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Does the wrong thing!</span></pre></div></div>

<p>The problem here is our call into each of the callback functions which still execute in the scope of the wrong object. This means that legacy code still does what it always did, but that&#8217;s just as broken as it was. We&#8217;d still need new syntax to make things safe. Ugg.</pre>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2010/09/javascript-uxo-removal/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>A Bit of Closure</title>
		<link>http://infrequently.org/2009/11/a-bit-of-closure/</link>
		<comments>http://infrequently.org/2009/11/a-bit-of-closure/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 19:03:36 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/2009/11/a-bit-of-closure/</guid>
		<description><![CDATA[So from time to time I&#8217;d wondered what all the brilliant DHTML hackers that Google had hired were up to. Obviously, building products. Sure. But I knew these guys. They do infrastructure, not just kludges and one-off&#8217;s. You don&#8217;t build a product like Gmail and have no significant UI infrastructure to show for it. Today [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://2.bp.blogspot.com/_EuCTzLdp3vE/SvIwmKttuCI/AAAAAAAAC1U/h9AdUMdkEO4/s200/closure.png" align="right"/></p>
<p>So from time to time I&#8217;d wondered what all the brilliant DHTML hackers that Google had hired were up to. Obviously, building products. Sure. But I knew these guys. They do infrastructure, not just kludges and one-off&#8217;s. You don&#8217;t build a product like Gmail and have no significant UI infrastructure to show for it.</p>
<p>Today they <a href="http://code.google.com/closure/library/">flung the doors open on Closure</a> and it&#8217;s supporting <a href="http://code.google.com/closure/compiler/">compiler</a>. These tools evolved together, and it shows. Closure code eschews many of the space-saving shortcuts that Dojo code employs because the compiler is so sophisticated that it can shorten nearly all variables, eliminate dead code, and even do type inference (based on JSDoc comments and static analysis).</p>
<p>There&#8217;s a <em>ton</em> of great code in Closure, so go give the <a href="http://closure-library.googlecode.com/svn/trunk/closure/goog/docs/index.html">docs a look</a> and, if you&#8217;re into that kind of thing, read the <a href="http://googlecode.blogspot.com/2009/11/introducing-closure-tools.html">official blog post</a> for a sense of what makes Closure so awesome.</p>
<p>It&#8217;s interesting to me how much it feels like a more advanced version of Dojo in many ways. There&#8217;s a familiar package system, the widgets are significantly more mature, and <a href="http://www.google.com/profiles/jparent">Julie</a> and <a href="http://www.google.com/profiles/ojan.vafai">Ojan</a>&#8216;s Editor component rocks. The APIs will feel familiar (if verbose) to Dojo users, the class hierarchies seem natural, and Closure even uses <a href="http://alex.dojotoolkit.org/2009/03/dojo-13b3-is-out/">Acme</a>, the Dojo CSS selector engine. It&#8217;s impressive work and congrats are in order for <a href="http://erik.eae.net/">Arv</a>, <a href="http://pupius.co.uk/blog/">Dan</a>, <a href="http://me.eae.net/">Emil</a>, <a href="http://thebode.blogspot.com/">Attila</a>, <a href="http://www.nick-santos.com/">Nick</a>, <a href="http://www.google.com/profiles/jparent">Julie</a>, <a href="http://www.google.com/profiles/ojan.vafai">Ojan</a>, and everyone else who worked so hard to build such an impressive system and fight to get it Open Source&#8217;d.</p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2009/11/a-bit-of-closure/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>JS 1.8 Function Expressions: The Opposite of &#8220;Good&#8221;</title>
		<link>http://infrequently.org/2009/08/js-1-8-function-expressions-the-opposite-of-good/</link>
		<comments>http://infrequently.org/2009/08/js-1-8-function-expressions-the-opposite-of-good/#comments</comments>
		<pubDate>Thu, 06 Aug 2009 21:02:51 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[1.8]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[mozilla]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/?p=1053</guid>
		<description><![CDATA[JavaScript is crying out for a way to write functions more tersely. I&#8217;ve added my suggestion to the debate, and was vaguely aware that Mozilla had implemented &#8220;function expressions&#8221;. It wasn&#8217;t until I saw their use in the (excellent) ProtoVis examples that I realized how stomach-churningly bad the syntax for them is. Instead of dropping [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript is crying out for a way to write functions more tersely. I&#8217;ve <a href="http://alex.dojotoolkit.org/2009/05/on-js-lambdas/">added my suggestion to the debate</a>, and was vaguely aware that Mozilla had implemented <a href="https://developer.mozilla.org/en/New_in_JavaScript_1.8">&#8220;function expressions&#8221;</a>. It wasn&#8217;t until I saw their use in the (excellent) <a href="http://vis.stanford.edu/protovis/ex/">ProtoVis examples</a> that I realized how stomach-churningly bad the syntax for them is. Instead of dropping the word <code>function</code> from the declaration of lambdas, they kill the <code>{ }</code> charachters, leaving the big visual turd at the front of the lambda while simultaneously omitting the symmetrical visual aid that allows programmers to more easily spot missing terminators.</p>
<p>Oof.</p>
<p>It&#8217;d be one thing of the feature saved enough effort (typing) to justify the confusion, but it doesn&#8217;t. Bitter syntactic sugar in a language that&#8217;s already complicated by whitespace issues should be avoided.</p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2009/08/js-1-8-function-expressions-the-opposite-of-good/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>On JS &#8220;Lambdas&#8221;</title>
		<link>http://infrequently.org/2009/05/on-js-lambdas/</link>
		<comments>http://infrequently.org/2009/05/on-js-lambdas/#comments</comments>
		<pubDate>Wed, 20 May 2009 17:01:42 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[lambda]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/?p=976</guid>
		<description><![CDATA[The ES working group is hard at work on &#8220;Harmony&#8221;, the goals of which are significantly more sane than previous attempts to build a new language from JavaScript. Namely, they&#8217;re being careful to be able to express things in new syntax based on old syntax. This is referred to as &#8220;de-sugaring&#8221;. Many new bits of [...]]]></description>
			<content:encoded><![CDATA[<p>The ES working group is hard at work on &#8220;Harmony&#8221;, the goals of which are significantly more sane than previous attempts to build a new language from JavaScript. Namely, they&#8217;re being careful to be able to express things in new syntax based on old syntax. This is referred to as &#8220;de-sugaring&#8221;. Many new bits of syntax will be expressed in old syntax in a resulting spec, and so it will be with any new lambda syntax.</p>
<p>One of the things I find most persistently annoying about the language as we have it is the verbose and wordy way of saying &#8220;build me a new invokable thing&#8221;. Since JS builds dynamic scopes based on on functions, this is particularly annoying when writing event handlers, callbacks, and arguments to <code>forEach</code> and friends. Needless to say, these are the things we do <em>all the time</em> in JavaScript. If there was anything that deserved syntactic sugar, this is it.</p>
<p>This brings us to &#8220;lambdas&#8221;. Languages like Ruby have a great syntax for expressing &#8220;a thing you can invoke&#8221; in a terse way. Python has something along these lines based on the keyword <code>lambda</code>, but Guido insists on keeping them <a href="http://docs.python.org/tutorial/controlflow.html#lambda-forms">neutered</a> based on (AFAICT from in-person discussions) a dislike of functional programming. Java is <a href="http://www.javac.info/">dangerously close to getting useful closures</a> but it&#8217;s so syntactically handicapped that it&#8217;ll probably be <em>another</em> decade before the masses rebel and demand a terse way to say what they mean. Or maybe they&#8217;ll all have just defected to <a href="http://jruby.codehaus.org/">JRuby</a>. For most of these languages, being able to say &#8220;here&#8217;s a new invokable thing&#8221; tersely is a Nice To Have feature. No real-world program will be slowed down by the size of a keyword. Indeed, Java makes you do so much work for the compiler that it&#8217;s practically taken as a badge of honor that you have to type it all out over and over and over.</p>
<p>JavaScript is different. Unlike nearly every other language, the terseness of JavaScript code is a key determinant in the performance of a web application. The bigger your scripts, the slower your app loads. It&#8217;s as simple as that.</p>
<p>It&#8217;s a crime, then, that we still have to type:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">node.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>evt<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// or</span>
&nbsp;
<span style="color: #009900;">&#91;</span><span style="color: #006600; font-style: italic;">/* ... */</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #009966; font-style: italic;">/* ... */</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The syntax for &#8220;an anonymous invokable thing&#8221; should be a lot shorter given how much exercise it gets in the real world. What real-world script authors don&#8217;t need is something other than &#8220;a shorter way to say <code>function</code>&#8220;. Anything different than this isn&#8217;t strictly useful. Anything that requires you to use a more wordy syntax to name arguments is a failure, and anything that uses a name longer than the shortest possible thing just re-introduces that bug that needs fixing in the first place. There have been proposals at the ES working group to date regarding &#8220;lambdas&#8221; and most assume that they will be something that serves to make implementer&#8217;s roles easier when it comes to de-sugaring but fail in one of these ways. Folks who write actual scripts should start speaking up and telling the Harmony working group what needs to be clearly and un-ambiguiously said:</p>
<h4>Don&#8217;t fuck with the semantics of <code>function</code>, just let me stop typing it so often</h4>
<p>What might a better syntax for <code>function</code> look like?</p>
<p>The best that <a href="http://erik.eae.net/">Erik</a> and I could come up with that is relatively unambiguious for parsers, doesn&#8217;t introduce hard-to-type characters on certain keyboard layouts, and doesn&#8217;t mess with the semantics of <code>function(){}</code> is this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="" style="font-family:monospace;">// empty function body, zero arguments
var lambda = #<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span> /*... */ <span class="br0">&#125;</span>;
&nbsp;
// empty function body, arguments list elided away
var lambda = #<span class="br0">&#123;</span> /* ... */ <span class="br0">&#125;</span>;
&nbsp;
// we can re-write the previous examples as:
node.addEventListener<span class="br0">&#40;</span>&quot;click&quot;, #<span class="br0">&#40;</span>evt<span class="br0">&#41;</span><span class="br0">&#123;</span> /* ... */ <span class="br0">&#125;</span>, false<span class="br0">&#41;</span>;
&nbsp;
// and
&nbsp;
<span class="br0">&#91;</span>/* ... */<span class="br0">&#93;</span>.forEach<span class="br0">&#40;</span>#<span class="br0">&#40;</span>i<span class="br0">&#41;</span><span class="br0">&#123;</span> /* ... */ <span class="br0">&#125;</span><span class="br0">&#41;</span>;</pre></td></tr></table></div>

<p>The long history of the word &#8220;lambda&#8221; coupled with the different interpretations that various languages place on them make using the word &#8220;lambda&#8221; to say &#8220;a short name for something you can invoke&#8221; particularly loaded. Perhaps instead we should just call this new syntax that crams a lot of meaning into a short &#8220;keyword&#8221; something else entirely.</p>
<p>&#8220;Cramdas&#8221;, anyone?</p>
<p><b>Update:</b> &#8230;and James Padolsey <a href="http://james.padolsey.com/javascript/custom-javascript-with-parsescripts/">makes it so!</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2009/05/on-js-lambdas/feed/</wfw:commentRss>
		<slash:comments>28</slash:comments>
		</item>
		<item>
		<title>OSCON 2009 Call For Papers Is Open!</title>
		<link>http://infrequently.org/2009/01/oscon-2009-call-for-papers-is-open/</link>
		<comments>http://infrequently.org/2009/01/oscon-2009-call-for-papers-is-open/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 21:35:20 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[opensource]]></category>
		<category><![CDATA[openweb]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[oscon]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/?p=865</guid>
		<description><![CDATA[I&#8217;m a bit tardy on this, but the OSCON 2009 Call For Papers is now open. In the past couple of years the shift from desktop-centric to a more web-centric OSCON has continued to make the conference useful and engaging, and great work on topics like JavaScript/Ajax performance, Dojo, Comet, and many of the emerging [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a bit tardy on this, but the OSCON 2009 Call For Papers <a href="http://en.oreilly.com/oscon2009/public/cfp/57">is now open</a>.</p>
<p>In the past couple of years the shift from desktop-centric to a more web-centric OSCON has continued to make the conference useful and engaging, and great work on topics like JavaScript/Ajax performance, Dojo, Comet, and many of the emerging back-end bits of infrastructure that make it all go have made my yearly trip to Portland worthwhile.</p>
<p>Great talks have gotten lost from the JavaScript/web track in years past because they&#8217;ve missed the submission deadline, so if you&#8217;re hacking on something fascinating, now&#8217;s the time to get that proposal in.  Make sure you flag it with the right track when you submit (javascript, ajax, web, or whatever they&#8217;re calling it this year), and don&#8217;t hesitate to ping me if you&#8217;re unsure about whether or not your talk got slotted correctly for the review process.</p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2009/01/oscon-2009-call-for-papers-is-open/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>delegate(), delegate(), delegate()</title>
		<link>http://infrequently.org/2008/10/delegate-delegate-delegate/</link>
		<comments>http://infrequently.org/2008/10/delegate-delegate-delegate/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 23:50:01 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[dhtml]]></category>
		<category><![CDATA[openweb]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[webdev]]></category>
		<category><![CDATA[boodman]]></category>
		<category><![CDATA[crockford]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[dojo]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/?p=786</guid>
		<description><![CDATA[My MBP batteries keep dying after about a year (each). I usually have 2 that I tote around with me, and each tends to be good for 1.5-2hrs of actual work. This means that I tend not to be able to work through a cross-country flight, and particularly not if I need a VM for [...]]]></description>
			<content:encoded><![CDATA[<p>My MBP batteries keep dying after about a year (each). I usually have 2 that I tote around with me, and each tends to be good for 1.5-2hrs of actual work. This means that I tend not to be able to work through a cross-country flight, and particularly not if I need a VM for anything (which is most of the time). I think that if Apple <em>does</em> <a href="http://www.macrumors.com/2008/10/09/apple-invites-media-to-notebook-event-october-14th/">rev the MBP&#8217;s on the 14th</a>, the things I&#8217;d pay for boil down to &#8220;more memory and much longer battery life&#8221;. The 5+ hour flight to <a href="http://ajaxexperience.techtarget.com/html/index.html">TAE</a> then provided a short window to do work in before I retreated to watching episodes of <a href="http://www.colbertnation.com/">The Colbert Report</a> on my phone. Knowing that i wouldn&#8217;t be able to work the whole time, I brought a copy of <a href="http://www.iam.unibe.ch/~scg/Archive/Papers/Duca06bTOPLASTraits.pdf">a great paper on Traits</a>. The paper got me thinking a lot about <code><a href="http://api.dojotoolkit.org/jsdoc/dojo/1.2/dojo.declare">dojo.declare()</a></code> and <code><a href="http://api.dojotoolkit.org/jsdoc/dojo/1.2/dojo.delegate">dojo.delegate()</a></code>.</p>
<p>Today, Dojo&#8217;s <code>delegate()</code> function is a straightforward implementation of the <a href="http://blog.youngpup.net/">Boodman</a>/<a href="http://www.crockford.com/">Crockford</a> delegation pattern which Doug calls &#8220;beget&#8221; and which ES 3.1 will refer to as <code>Object.create</code>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">dojo.<span style="color: #660066;">delegate</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// boodman/crockford delegation w/ cornford optimization</span>
    <span style="color: #003366; font-weight: bold;">function</span> TMP<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> props<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        TMP.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> tmp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TMP<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>props<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            dojo._mixin<span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> props<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> tmp<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Object</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This function returns a new object which looks to the old object for things it does not itself have. Imagine an object <code>foo</code> which contains pithy truisms:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
  science<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;rocks!&quot;</span><span style="color: #339933;">,</span>
  learning<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;is how you know you're alive&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We now want to promigulate our opinions, so we can delegate the responsibility of forming them:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> bar <span style="color: #339933;">=</span> dojo.<span style="color: #660066;">delegate</span><span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
  testify<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;science &quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">science</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;and learning&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">learning</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, our <code>bar</code> object can change its mind independently of <code>foo</code>, but until it does, it&#8217;ll behave as though <code>foo</code>&#8216;s views are its own:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">bar.<span style="color: #660066;">testify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// outputs: &quot;science rocks and learning is how you know you're alive&quot;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// bar refines its opinion</span>
bar.<span style="color: #660066;">science</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;is a process&quot;</span><span style="color: #339933;">;</span> 
bar.<span style="color: #660066;">learning</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;requires humility&quot;</span><span style="color: #339933;">;</span>
foo.<span style="color: #660066;">science</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;rocks!&quot;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// still true</span>
&nbsp;
bar.<span style="color: #660066;">testify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// outputs: &quot;science is a process and learning requires humility&quot;</span></pre></div></div>

<p>But what about when the chain gets deeper? The fact that <code>bar</code> can&#8217;t &#8220;see&#8221; <code>foo</code>&#8216;s values via <code>this</code> isn&#8217;t much of a problem when the hierarchy isn&#8217;t very long, but if you&#8217;re specializing a behavior or complex interaction, making it possible to get at the parent&#8217;s values for properties and methods becomes more pressing.</p>
<p>Neil has previously <a href="http://www.sitepen.com/blog/2008/03/16/are-you-sure-you-should-be-subclassing-that/">written about lightweight subclassing</a>, but for as good as it its, it doesn&#8217;t get us all the way there either. In regular OO-style languages, the inheritance system gives you an out via a &#8220;super&#8221; keyword or convention. This type of property shadowing-with-exceptions is a huge boon to composition in class-based languages, but it&#8217;s not the whole story. Indeed, the Traits paper was all about the shortcomings of this special-purpose mechanism. What we want for both long delegation chains and long inheritance hierarchies is a more general system; in essence a way to say &#8220;I want to control how things are shadowed and which ones an item points at in each level of the hierarchy&#8221;.</p>
<p>What if we could make <code>delegate()</code> savvy of this type of indirection? Here&#8217;s my quick prototype:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">delegate <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> tobj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> TMP <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> props<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        TMP.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> tmp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TMP<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>props<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> remaps <span style="color: #339933;">=</span> props<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;-&gt;&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>remaps<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">delete</span> props<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;-&gt;&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #000066; font-weight: bold;">in</span> remaps<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>tobj<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> undefined <span style="color: #339933;">||</span> tobj<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> remaps<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>remaps<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #006600; font-style: italic;">// support hiding via null assignment</span>
                            tmp<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #006600; font-style: italic;">// alias the local version away</span>
                            tmp<span style="color: #009900;">&#91;</span>remaps<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            dojo.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> props<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">return</span> tmp<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Object</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This new version of <code>delegate()</code> accepts a specially named &#8220;->&#8221; property in the list of items to add to the destination object. Items in this list can either &#8220;shadow null&#8221; (hide entirely) the parent&#8217;s property or can provide a new name for it, assuming of course that the new object will also have a property of that name. Here&#8217;s a quick example of &#8220;->&#8221; at work with our previous example. This time, <code>foo</code> also has a &#8220;testify&#8221; method that we&#8217;d like <code>bar</code> to be able to control without having to copy the implementation:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    science<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;rocks!&quot;</span><span style="color: #339933;">,</span>
    learning<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;is how you know you're alive&quot;</span><span style="color: #339933;">,</span>
    testify<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;science &quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">science</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;and learning&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">learning</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> bar <span style="color: #339933;">=</span> delegate<span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;-&gt;&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;testify&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;grampsSays&quot;</span> <span style="color: #006600; font-style: italic;">// maps foo's &quot;testify&quot; to bar's &quot;grampsSays&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    testify<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">science</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">learning</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">grampsSays</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// call the re-named &quot;testify&quot;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
            console.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;this object is strikingly ignorant&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
bar.<span style="color: #660066;">testify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// outputs: &quot;science rocks and learning is how you know you're alive&quot;</span>
bar.<span style="color: #660066;">science</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
bar.<span style="color: #660066;">testify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// outputs: &quot;this object is strikingly ignorant&quot;</span></pre></div></div>

<h4>That New Object Smell</h4>
<p>The last missing piece of the hierarchy pie here is that there&#8217;s no initializer for the objects which come from a delegation. A simple addition of some property detection code to look for an initializer can easily handle that:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">delegate <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> tobj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> TMP <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> props<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// boodman/crockford delegation w/ cornford optimization. </span>
&nbsp;
        TMP.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> obj<span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> tmp <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> TMP<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>props<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> remaps <span style="color: #339933;">=</span> props<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;-&gt;&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>remaps<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000066; font-weight: bold;">delete</span> props<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;-&gt;&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #006600; font-style: italic;">// like dojo.mixin(), except w/o key/key mapping</span>
                <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #000066; font-weight: bold;">in</span> remaps<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                    <span style="color: #006600; font-style: italic;">// &quot;safe&quot; copy properties</span>
                    <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>tobj<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">===</span> undefined <span style="color: #339933;">||</span> tobj<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!=</span> remaps<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>remaps<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #006600; font-style: italic;">// support hiding via null assignment</span>
                            tmp<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #000066; font-weight: bold;">else</span><span style="color: #009900;">&#123;</span>
                            <span style="color: #006600; font-style: italic;">// alias the local version away</span>
                            tmp<span style="color: #009900;">&#91;</span>remaps<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> obj<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
            dojo.<span style="color: #660066;">mixin</span><span style="color: #009900;">&#40;</span>tmp<span style="color: #339933;">,</span> props<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #006600; font-style: italic;">// support for &quot;constructor&quot; functions. The name &quot;init&quot; is arbitrary.</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> tmp<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;init&quot;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;function&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            tmp.<span style="color: #660066;">init</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>tmp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> tmp<span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// Object</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And there we have it. A style of delegation that easily supports both Trait-like name aliasing (and null shadowing) as well as internal initializers. Since our upgraded <code>delegate</code> can handle nulling out a parent&#8217;s value for a property, we also have a straightforward way to prevent parent initializers from being called (or being called/chained &#8211; at our discretion &#8211; by a new name):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> foo <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    science<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;rocks!&quot;</span><span style="color: #339933;">,</span>
    learning<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;is how you know you're alive&quot;</span><span style="color: #339933;">,</span>
    testify<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;science &quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">science</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;and learning&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">learning</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> bar <span style="color: #339933;">=</span> delegate<span style="color: #009900;">&#40;</span>foo<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">testify</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// outputs: &quot;science rocks and learning is how you know you're alive&quot;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> baz <span style="color: #339933;">=</span> delegate<span style="color: #009900;">&#40;</span>bar<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// map away the parent's constructor</span>
    <span style="color: #3366CC;">&quot;-&gt;&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #3366CC;">&quot;init&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;superInit&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #006600; font-style: italic;">// provide our own constructor</span>
    init<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;howdy!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">superInit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// call the super-object ctor</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// outputs: &quot;howdy&quot;, &quot;science rocks and learning is how you know you're alive&quot;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> thud <span style="color: #339933;">=</span> delegate<span style="color: #009900;">&#40;</span>baz<span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;-&gt;&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span> <span style="color: #3366CC;">&quot;init&quot;</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// hide the parent ctor</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// outputs: nothing</span></pre></div></div>

<p>This form of <code>delegate</code> is likely to appear in Dojo 1.3 along with similar improvements to <code>dojo.declare()</code> to help alleviate the composition problems associated with using complex sets of mixins.</p>
<p><b>Update</b>: corrected the null-out branch and updated the text with Doug&#8217;s note that beget/delegate will be called Object.create() in 3.1.</p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2008/10/delegate-delegate-delegate/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>&#8220;Action Oriented Programming&#8221;</title>
		<link>http://infrequently.org/2008/10/action-oriented-programming/</link>
		<comments>http://infrequently.org/2008/10/action-oriented-programming/#comments</comments>
		<pubDate>Thu, 09 Oct 2008 16:40:13 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[webdev]]></category>
		<category><![CDATA[closures]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/?p=783</guid>
		<description><![CDATA[It&#8217;s good to be back in SF after a pretty hectic week in Boston for Dojo Developer Days and The Ajax Experience. There&#8217;s a lot to say about them, which hopefully I&#8217;ll get to in a longer post. Our first DDD event under Pete&#8216;s excellent leadership was a success and Dojo and SitePen very well [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s good to be back in SF after a pretty hectic week in Boston for Dojo Developer Days and The Ajax Experience. There&#8217;s a lot to say about them, which hopefully I&#8217;ll get to in a longer post. Our first DDD event under <a href="http://higginsforpresident.net/">Pete</a>&#8216;s excellent leadership was a success and Dojo and SitePen very well represented at the conference.</p>
<p>While in Boston, <a href="http://blog.xdraw.org/">Gavin</a> and <a href="http://www.eyelevelpasadena.com/">Jill</a>  joined a gaggle of Dojo hackers at a dinner ostensibly to mourn my birthday (thanks to Dylan and Pete for organizing!) and in the course of conversation Jill asked something along the lines of:</p>
<blockquote><p>
So why do people get so excited about closures?
</p></blockquote>
<p>Which prompted a several of us to flail and flop gasping the salt flats of analogy like fish out of polite-conversation water. After about 10 minutes of this, Jill succinctly summed it all up in the form of a question:</p>
<blockquote><p>
Oh, so it&#8217;s like &#8220;action-oriented programming&#8221;?
</p></blockquote>
<p>This is perhaps the most insightful and succinct description I have ever heard of what JavaScript is all about.</p>
<p><b>Update:</b> <a href="http://tharpo.com">Jennifer</a> just played <a href="http://blogs.wnyc.org/radiolab/2008/07/29/tell-me-a-story/">this for me</a> and it gets right to the heart of this post: the important part of doing what we do with computers, and more importantly, with the web, is to give the power of Computer Science to real people&#8230;and it starts with insights like Jill&#8217;s that build a shared way of thinking and talking about the world. It makes me sad that many programmers miss that, but when non-programmers can share in the beauty and power of code, it does a lot to make it all seem worthwhile.</p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2008/10/action-oriented-programming/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>The Importance Of Chrome</title>
		<link>http://infrequently.org/2008/09/the-importance-of-chrome/</link>
		<comments>http://infrequently.org/2008/09/the-importance-of-chrome/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 23:28:44 +0000</pubDate>
		<dc:creator>alex</dc:creator>
				<category><![CDATA[dhtml]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[openweb]]></category>
		<category><![CDATA[chrome]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[strategy]]></category>
		<category><![CDATA[webkit]]></category>

		<guid isPermaLink="false">http://alex.dojotoolkit.org/?p=747</guid>
		<description><![CDATA[The rumors seem to have been true&#8230;the gBrowser is real. And it looks like it will simply be awesome. To my friends who have been toiling on it in deep secrecy for so very long, congratulations. Yes, yes, more to do, blah blah&#8230;screw that. You shipped! Huzzah! So what does Chrome mean for those of [...]]]></description>
			<content:encoded><![CDATA[<p>The rumors seem to have been true&#8230;<a href="http://googleblog.blogspot.com/2008/09/fresh-take-on-browser.html">the gBrowser is real</a>. And it looks like it will simply be awesome. To my friends who have been toiling on it in deep secrecy for so very long, congratulations. Yes, yes, more to do, blah blah&#8230;screw that. You shipped! Huzzah!</p>
<p>So what does Chrome mean for those of us who aren&#8217;t breaking out the champagne? Well, first, it&#8217;s the second sign (after <a href="http://gears.google.com/">Gears</a> and YBP (<a href="http://alex.dojotoolkit.org/2008/06/gears-de-brands-2/">har!</a>)) that the content authors are taking back the web from the &#8220;browser guys&#8221;. I&#8217;ve been fascinated for the last 6 months or so by the strategic mis-alignment which results when both the browsing and authoring experience in the hands of organizations only care about one but not the other. Mozilla gets paid by search-box revenue and users download it because it&#8217;s better for browsing, therefore Mozilla is incented to <a href="http://www.adaptivepath.com/aurora/">build new ways to browse</a>, but their investments in content are somewhat mis-aligned (and, frankly, <a href="http://labs.mozilla.com/">it shows</a>). Google and Yahoo, on the other hand, are critically dependent on the content getting better, so they produce plugins to augment HTML in un-intrusive ways. Chrome crosses over into the browser business <em>from the perspective of content</em>, and it also shows, albeit in a good-ish way. I guess we&#8217;ll need to wait and see how browsing-oriented Chrome gets (e.g., will it sprout an extensions platform &ndash; ala Firefox &ndash; or will the propsect of an ad-blocking plugin for the Google browser make that proposal D.O.A.?).</p>
<p>Regardless of how Chrome evolves as a product, the important question now is: how will it be distributed? The obviously non-evil thing to do is to say &#8220;look, it&#8217;s great, it&#8217;s free&#8221; and hope that the world discovers it on its own thanks to word-of-mouth and/or leverage of the Google brand. Given that Chrome delivers new awesome things which are end-user-visible (some &#8220;end-user-awesome&#8221;, if you will), there&#8217;s some real chance that Chrome can get to roughly Firefox level market-share without breaking too much of a sweat. Not that Firefox&#8217;s market share is anything to really covet, given that MoFo/MoCo have been toiling at it for a decade now. To get real, honest-to-god leverage out of this process, Chrome is going to need something like 60+% market share, and that means changing ingrained user habits. I put the probability of that happening without distribution channel love at roughly bupkis.</p>
<p>Microsoft killed Netscape by bundling the browser with the OS. Apple is <a href="http://marketshare.hitslink.com/report.aspx?sample=15&#038;qprid=22&#038;qpdt=1&#038;qpct=5&#038;qptimeframe=M&#038;qpsp=101&#038;qpnp=12&#038;qprid2=3&#038;qpcustom2=Firefox+3.0">making inroads by bundling</a>. Firefox is even <a href="http://marketshare.hitslink.com/report.aspx?sample=15&#038;qprid=22&#038;qpdt=1&#038;qpct=5&#038;qptimeframe=M&#038;qpsp=101&#038;qpnp=12&#038;qprid2=3&#038;qpcustom2=Firefox+3.0#">getting aggressive</a>. So where does this leave &#8220;don&#8217;t be evil&#8221;? Given the <a href="http://weblogs.java.net/blog/kgh/archive/2005/10/java_se_and_the.html">toolbar promotional deals</a> which Google has cut in the past, I think there&#8217;s some organizational capacity inside the Goog to use the distribution channels they&#8217;ve already created as a way of getting to critical mass. What I don&#8217;t see, though, is a view of how to bring the mission of Gears into alignment with Chrome (or vice versa). They&#8217;re both important, but Chrome is a long-term bet while Gears is the near-future solution. They are not in opposition, but their strategies for gaining leverage over the problems facing content authors are very different.</p>
<p>We need what Gears can offer to every browser <em>right now</em> while Chrome dukes it out for market share on the browsing experience merits. Hopefully, if nothing else, the Chrome installer will add Gears to other browsers on the system that users install Chrome to. Even if they don&#8217;t pick the googly experience for browsing day-to-day, perhaps Chrome can still serve to give new tools to the content-author side of the house. Other browser vendors won&#8217;t do such a thing since they win or loose on an exclusive &#8220;I must replace the other guy&#8221; basis. Here, Google (and by &#8220;Google&#8221;, I mean &#8220;the open web&#8221;) wins either way. Hopefully Google&#8217;s interest in making the content experience better trumps the &#8220;we&#8217;re all browser guys now&#8221; instinct in this case.</p>
<p>We&#8217;ll find out tomorrow, I guess. Here&#8217;s to hoping.</p>
]]></content:encoded>
			<wfw:commentRss>http://infrequently.org/2008/09/the-importance-of-chrome/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

