<?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>The next 10,000 hours &#187; javascript coffeescript</title>
	<atom:link href="http://www.trouble.net.au/blog/korny/tag/javascript-coffeescript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.trouble.net.au/blog/korny</link>
	<description>Korny&#039;s tech blog</description>
	<lastBuildDate>Fri, 09 Apr 2010 23:02:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Playing with CoffeeScript</title>
		<link>http://www.trouble.net.au/blog/korny/2010/02/25/playing-with-coffeescript/</link>
		<comments>http://www.trouble.net.au/blog/korny/2010/02/25/playing-with-coffeescript/#comments</comments>
		<pubDate>Thu, 25 Feb 2010 03:03:35 +0000</pubDate>
		<dc:creator>korny</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[javascript coffeescript]]></category>

		<guid isPermaLink="false">http://www.trouble.net.au/blog/korny/?p=148</guid>
		<description><![CDATA[I&#8217;ve been fiddling with Coffeescript, a nifty language for writing Javascript &#8211; well, really it&#8217;s more like a pre-compiler than a language; it compiles directly to readable javascript (including comments!) and generally one Coffeescript statement produces one javascript statement.
So why would you use it?  Because it bypasses some of the horribleness of Javascript syntax, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been fiddling with <a href="http://jashkenas.github.com/coffee-script">Coffeescript</a>, a nifty language for writing Javascript &#8211; well, really it&#8217;s more like a pre-compiler than a language; it compiles directly to readable javascript (including comments!) and generally one Coffeescript statement produces one javascript statement.</p>
<p>So why would you use it?  Because it bypasses some of the horribleness of Javascript syntax, and implements a few handy patterns, like anonymous function creation, in much clearer code.<br />
<span id="more-148"></span><br />
For example, the following Coffeescript:</p>
<pre class="brush: text">
square: (x) -&gt;
alert &#039;squaring: &#039; + x
x * x
</pre>
<p>converts to the following javascript:</p>
<pre class="brush: javascript">
square: function square(x) {
  alert(&#039;squaring:&#039; + x);
  return x * x;
}
</pre>
<p>There are many more examples at the <a href="http://jashkenas.github.com/coffee-script">Coffeescript</a> home page.</p>
<p>I&#8217;m not 100% sold on this, as I&#8217;m still learning (proper) Javascript, and this amplifies some of my ignorance.  For example, I really struggle with &#8216;this&#8217; and functions/objects &#8211; and while Coffeescript automatically enforces some good Javascript behaviour, when I do something wrong it&#8217;s a bigger than usual struggle to work out why.<br />
A longer example: I&#8217;ve been playing with <a href="http://raphaeljs.com/">Raphael</a> (and JQuery) and decided to do some fiddling in Coffeescript:</p>
<pre class="brush: text">
app: {
  init: -&gt;
    app.paper: Raphael(&#039;playspace&#039;,320,200)
    app.draw_circle()
    app.draw_square()

  # simple circle with event-based on-click event
  draw_circle: -&gt;
    circle: app.paper.circle(50,40,30)
    circle.attr(&quot;fill&quot;,&quot;#f00&quot;)
    circle.attr(&quot;stroke&quot;,&quot;#fff&quot;)
    $(circle.node).click((e) -&gt; app.kick(e))

  # square with object-based on-click event
  draw_square: -&gt;
    square: app.paper.rect(150,40,50,30)
    square.attr(&quot;fill&quot;,&quot;#0f0&quot;)
    square.attr(&quot;stroke&quot;,&quot;#fff&quot;)
    $(square.node).click((e) -&gt; app.punt(square,e))

  kick: (e) -&gt;
    app.paper.text(e.pageX + 10, e.pageY + 10, &quot;Raphaël\nkicks\nbutt!&quot;)

  punt: (obj,e) -&gt;
    obj.translate(10,10)
}

# make it globally visible - not really needed, handy for debugging
if window?
  window.APP: app

$(app.init)
</pre>
<p>The javascript version is:</p>
<pre class="brush: javascript">
(function(){
  var app;
  app = {
    init: function init() {
      app.paper = Raphael(&#039;playspace&#039;, 320, 200);
      app.draw_circle();
      return app.draw_square();
    },
    // simple circle with event-based on-click event
    draw_circle: function draw_circle() {
      var circle;
      circle = app.paper.circle(50, 40, 30);
      circle.attr(&quot;fill&quot;, &quot;#f00&quot;);
      circle.attr(&quot;stroke&quot;, &quot;#fff&quot;);
      return $(circle.node).click(function(e) {
        return app.kick(e);
      });
    },
    // square with object-based on-click event
    draw_square: function draw_square() {
      var square;
      square = app.paper.rect(150, 40, 50, 30);
      square.attr(&quot;fill&quot;, &quot;#0f0&quot;);
      square.attr(&quot;stroke&quot;, &quot;#fff&quot;);
      return $(square.node).click(function(e) {
        return app.punt(square, e);
      });
    },
    kick: function kick(e) {
      return app.paper.text(e.pageX + 10, e.pageY + 10, &quot;Raphaël\nkicks\nbutt!&quot;);
    },
    punt: function punt(obj, e) {
      return obj.translate(10, 10);
    }
  };
  // make it globally visible - not really needed, handy for debugging
  (typeof window !== &quot;undefined&quot; &amp;&amp; window !== null) ? (window.APP = app) : null;
  $(app.init);
})();
</pre>
<p>Now, this works, it&#8217;s a lot more readable than the Javascript version, and it&#8217;s nifty how Coffeescript wrapped everything in an anonymous function so as to not pollute the global namespace (except where I did so deliberately).</p>
<p>But this was about my 8th try.  I had endless problems getting the &#8216;click&#8217; handlers to hook into the &#8216;kick&#8217; and &#8216;punt&#8217; methods.  I tried (initially) using &#8216;this.kick&#8217; but it failed as when the click handler is called, this is bound elsewhere.  I tried storing &#8216;this&#8217; in a variable, but had pain with the fact that somewhere in the mess of anonymous function wrappers, &#8216;this&#8217; never seemed to mean the right thing.</p>
<p>In the end, I hit on storing the whole thing as an object &#8216;app&#8217; &#8211; I was already going to make it part of &#8216;window.APP&#8217; but didn&#8217;t want to prefix all calls with &#8216;APP&#8217;&#8230; but in the end, that&#8217;s kind-of what I did, only the global APP variable isn&#8217;t actually needed, just the local &#8216;app&#8217; variable.</p>
<p>Anyway, I&#8217;m sure there is a better way to do this &#8211; but I haven&#8217;t found it, and this one works.  Now I have to decide if I want to keep playing with Coffeescript, having worked all this out, or go back to ugly old Javascript, where at least there are hundreds of net resources to help me when I get stuck!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.trouble.net.au/blog/korny/2010/02/25/playing-with-coffeescript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
