Skip to content

Commit

Permalink
apply Jeremys errata
Browse files Browse the repository at this point in the history
  • Loading branch information
maccman committed Jun 8, 2011
1 parent c93b8d2 commit 774cb9e
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 33 deletions.
8 changes: 5 additions & 3 deletions coffeescript/00_introduction.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ <h1>What is CoffeeScript?</h1>

<p><a href="http://coffeescript.org">CoffeeScript</a> is a little language that compiles down to JavaScript. The syntax is inspired by Ruby and Python, and implements many features from those two languages. This book is designed to help you learn CoffeeScript, understand best practices and start building awesome client side applications. The book is little, only five chapters, but that's rather apt as CoffeeScript is a little language too.</p>

<p>This book is completely open source, and was written by <a href="http://alexmaccaw.co.uk">Alex MacCaw</a> (or <a href="http://twitter.com/maccman">@maccman</a>) - if you have any errata or suggestions, please don't hesitate to open a ticket on the book's <a href="https://github.com/arcturo/library">GitHub page</a>. Readers may also be interested in <a href="http://oreilly.com/catalog/9781449307530/">JavaScript Web Applications by O'Reilly</a>, a book that explores rich JavaScript applications and moving state to the client side.</p>
<p>This book is completely open source, and was written by <a href="http://alexmaccaw.co.uk">Alex MacCaw</a> (or <a href="http://twitter.com/maccman">@maccman</a>) with great contributions from <a href="https://github.com/dxgriffiths">David Griffiths</a>, <a href="http://github.com/satyr">Satoshi Murakami</a>, and <a href="https://github.com/jashkenas">Jeremy Ashkenas</a>.</p>

<p>If you have any errata or suggestions, please don't hesitate to open a ticket on the book's <a href="https://github.com/arcturo/library">GitHub page</a>. Readers may also be interested in <a href="http://oreilly.com/catalog/9781449307530/">JavaScript Web Applications by O'Reilly</a>, a book that explores rich JavaScript applications and moving state to the client side.</p>

<p>So let's dive right into it; why is CoffeeScript better than writing pure JavaScript? Well for a start, there's less code to write - CoffeeScript is very succinct, and takes white-space into account. In my experience this reduces code by a third to a half of the original pure JavaScript. In addition, CoffeeScript has some neat features, such as array comprehensions, prototype aliases and classes that further reduce the amount of typing you need to do.</p>

Expand All @@ -43,7 +45,7 @@ <h1>What is CoffeeScript?</h1>

<h2>Initial setup</h2>

<p>One of the easiest ways to initially play around with the library is to use it right inside the browser. Navigate to <a href="http://jashkenas.github.com/coffee-script">http://jashkenas.github.com/coffee-script</a> and click on the <em>Try CoffeeScript</em> tab. The site uses a browser version of the CoffeeScript compiler, converting any CoffeeScript typed inside the left panel, to JavaScript in the right panel.</p>
<p>One of the easiest ways to initially play around with the library is to use it right inside the browser. Navigate to <a href="http://coffeescript.org">http://coffeescript.org</a> and click on the <em>Try CoffeeScript</em> tab. The site uses a browser version of the CoffeeScript compiler, converting any CoffeeScript typed inside the left panel, to JavaScript in the right panel.</p>

<p>In fact you can use browser based compiler yourself, by including <a href="http://jashkenas.github.com/coffee-script/extras/coffee-script.js">this script</a> in a page, marking up any CoffeeScript script tags with the correct <code>type</code>.</p>

Expand All @@ -65,7 +67,7 @@ <h2>Initial setup</h2>
<pre><code>coffee --compile my-script.coffee
</code></pre>

<p>If <code>--output</code> is not specified, CoffeeScript will write to a JavaScript file with the same name, in this case <code>my-script.js</code>. This will overwrite any existing files, so be careful you're overwriting any JavaScript files unintentionally. For a full list of the command line options available, please see the <a href="http://coffeescript.org">docs</a>.</p>
<p>If <code>--output</code> is not specified, CoffeeScript will write to a JavaScript file with the same name, in this case <code>my-script.js</code>. This will overwrite any existing files, so be careful you're overwriting any JavaScript files unintentionally. For a full list of the command line options available, pass <code>--help</code>.</p>

<p>As you can see above, the default extension of CoffeeScript files is <code>.coffee</code>. Amongst other things, this will allow text editors like <a href="http://macromates.com/">TextMate</a> to work out which language the file contains, giving it the appropriate syntax highlighting. By default, TextMate doesn't include support for CoffeeScript, but you can easily install the <a href="https://github.com/jashkenas/coffee-script-tmbundle">bundle to do so</a>.</p>

Expand Down
10 changes: 5 additions & 5 deletions coffeescript/01_syntax.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ <h2>Functions</h2>

<h3>Function arguments</h3>

<p>How about specifying arguments? Well, CoffeeScript lets you do that by specifying arguments in a pair of rounded brackets before the arrow.</p>
<p>How about specifying arguments? Well, CoffeeScript lets you do that by specifying arguments in a pair of parenthesis before the arrow.</p>

<p><span class="csscript"></span></p>

Expand Down Expand Up @@ -131,7 +131,7 @@ <h3>Function arguments</h3>

<h3>Function invocation</h3>

<p>Functions can be invoked exactly as in JavaScript, with brackets <code>()</code>, <code>apply()</code> or <code>call()</code>. However, like Ruby, CoffeeScript will automatically call functions if they are invoked with at least one argument.</p>
<p>Functions can be invoked exactly as in JavaScript, with parens <code>()</code>, <code>apply()</code> or <code>call()</code>. However, like Ruby, CoffeeScript will automatically call functions if they are invoked with at least one argument.</p>

<p><span class="csscript"></span></p>

Expand All @@ -146,7 +146,7 @@ <h3>Function invocation</h3>
alert(inspect(a))
</code></pre>

<p>Although parenthesis is optional, I'd recommend using it if it's not immediately obvious what's being invoked, and with which arguments. In the last example, with <code>inspect</code>, I'd definitely recommend wrapping at least the <code>inspect</code> invocation in brackets</p>
<p>Although parenthesis is optional, I'd recommend using it if it's not immediately obvious what's being invoked, and with which arguments. In the last example, with <code>inspect</code>, I'd definitely recommend wrapping at least the <code>inspect</code> invocation in parens.</p>

<p><span class="csscript"></span></p>

Expand Down Expand Up @@ -180,7 +180,7 @@ <h2>Object literals &amp; array definition</h2>
<pre><code>object1 = {one: 1, two: 2}

# Without braces
object2 = one:1, two:2
object2 = one: 1, two: 2

# Using new lines instead of commas
object3 =
Expand Down Expand Up @@ -413,7 +413,7 @@ <h2>Aliases &amp; the Existential Operator</h2>
<pre><code>velocity = southern ? 40
</code></pre>

<p>If you're using a <code>null</code> check before accessing a property, you can skip that by placing the existential operator right before the opening brackets. This is similar to Ruby's <code>try</code> method.</p>
<p>If you're using a <code>null</code> check before accessing a property, you can skip that by placing the existential operator right before the opening parens. This is similar to Ruby's <code>try</code> method.</p>

<p><span class="csscript"></span></p>

Expand Down
2 changes: 1 addition & 1 deletion coffeescript/03_classes.html
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ <h2>Inheritance &amp; Super</h2>
alert("This parrot is no more") if parrot.rip()
</code></pre>

<p>It's worth pointing out though that static properties aren't inherited, only instance properties are. This is due to implementation details with JavaScript's prototypal architecture, and is a difficult problem to work around. The CoffeeScript team are actively looking at this problem though, so hopefully we'll see a solution to this soon.</p>
<p>It's worth pointing out though that static properties are copied to subclasses, rather than inherited using prototype as instance properties are. This is due to implementation details with JavaScript's prototypal architecture, and is a difficult problem to work around.</p>

<h2>Mixins</h2>

Expand Down
4 changes: 2 additions & 2 deletions coffeescript/04_idioms.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ <h2>Map</h2>
});
</code></pre>

<p>As we covered in the syntax chapter, CoffeeScript's comprehensions can be used to get the same behavior as <code>map()</code>. Notice we're surrounding the comprehension with brackets, which is <strong>absolutely critical</strong> in ensuring the comprehension returns what you'd expect, the mapped array.</p>
<p>As we covered in the syntax chapter, CoffeeScript's comprehensions can be used to get the same behavior as <code>map()</code>. Notice we're surrounding the comprehension with parens, which is <strong>absolutely critical</strong> in ensuring the comprehension returns what you'd expect, the mapped array.</p>

<p><span class="csscript"></span></p>

Expand Down Expand Up @@ -89,7 +89,7 @@ <h2>Select</h2>
<pre><code>result = (item for item in array when item.name is "test")
</code></pre>

<p>Don't forgot to include the brackets, as otherwise <code>result</code> will be the last item in the array.
<p>Don't forgot to include the parens, as otherwise <code>result</code> will be the last item in the array.
CoffeeScript's comprehensions are so flexible that they allow you to do powerful selections as in the following example:</p>

<p><span class="csscript"></span></p>
Expand Down
3 changes: 1 addition & 2 deletions coffeescript/05_applications.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ <h2>Stitch it up</h2>
&lt;!-- Require the main Stitch file --&gt;
&lt;script src="/application.js" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
&lt;script type="text/javascript" charset="utf-8"&gt;
var exports = this;
jQuery(function(){
var App = require("app");
exports.App = App.init({el: $("body")});
window.App = App.init({el: $("body")});
});
&lt;/script&gt;
&lt;/head&gt;
Expand Down
15 changes: 8 additions & 7 deletions coffeescript/README
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ A little book on CoffeeScript and building RIAs.

http://coffeescript.org

Alex MacCaw ([email protected])
TODO:

1. CoffeeScript introduction, pros cons, common fallacies
2. General syntax, objects, arrays, flow control, functions, switch, string interpolation
3. Classes, inheritance, super, context
4. Using CoffeeScript with Stich, Express. Loading in templates.
5. And maybe a chapter on CoffeeScript with Backbone/Spine/Underscore/jQuery
* When discussing multiline comments, it may be worthwhile to mention that multiline strings and regexes take the same form: """ and ///, respectively.

* "Semicolons were the cause of much debate in the JavaScript community, and behind some weird interpreter behavior." ... Could be more detailed here. Perhaps something like: "Newlines in JavaScript already cause semicolons to be automatically inserted in many cases, but not all of them, which leads to trouble. CoffeeScript simply allows newlines to end each statement."

* "As you can see above, if the if statement is on one line, you'll need to use the then keyword" ... the "then" keyword can actually be in any place where you'd otherwise put an indented block, throughout the language.

* The Max/Min trick is great, but will fail for really large arrays, which is worth mentioning. (Functions can only take so many arguments in certain browsers).

Jison
8 changes: 5 additions & 3 deletions coffeescript/chapters/00_introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

[CoffeeScript](http://coffeescript.org) is a little language that compiles down to JavaScript. The syntax is inspired by Ruby and Python, and implements many features from those two languages. This book is designed to help you learn CoffeeScript, understand best practices and start building awesome client side applications. The book is little, only five chapters, but that's rather apt as CoffeeScript is a little language too.

This book is completely open source, and was written by [Alex MacCaw](http://alexmaccaw.co.uk) (or [@maccman](http://twitter.com/maccman)) - if you have any errata or suggestions, please don't hesitate to open a ticket on the book's [GitHub page](https://github.com/arcturo/library). Readers may also be interested in [JavaScript Web Applications by O'Reilly](http://oreilly.com/catalog/9781449307530/), a book that explores rich JavaScript applications and moving state to the client side.
This book is completely open source, and was written by [Alex MacCaw](http://alexmaccaw.co.uk) (or [@maccman](http://twitter.com/maccman)) with great contributions from [David Griffiths](https://github.com/dxgriffiths), [Satoshi Murakami](http://github.com/satyr), and [Jeremy Ashkenas](https://github.com/jashkenas).

If you have any errata or suggestions, please don't hesitate to open a ticket on the book's [GitHub page](https://github.com/arcturo/library). Readers may also be interested in [JavaScript Web Applications by O'Reilly](http://oreilly.com/catalog/9781449307530/), a book that explores rich JavaScript applications and moving state to the client side.

So let's dive right into it; why is CoffeeScript better than writing pure JavaScript? Well for a start, there's less code to write - CoffeeScript is very succinct, and takes white-space into account. In my experience this reduces code by a third to a half of the original pure JavaScript. In addition, CoffeeScript has some neat features, such as array comprehensions, prototype aliases and classes that further reduce the amount of typing you need to do.

Expand All @@ -20,7 +22,7 @@ CoffeeScript is not limited to the browser, and can be used to great effect in s

##Initial setup

One of the easiest ways to initially play around with the library is to use it right inside the browser. Navigate to [http://jashkenas.github.com/coffee-script](http://jashkenas.github.com/coffee-script) and click on the *Try CoffeeScript* tab. The site uses a browser version of the CoffeeScript compiler, converting any CoffeeScript typed inside the left panel, to JavaScript in the right panel.
One of the easiest ways to initially play around with the library is to use it right inside the browser. Navigate to [http://coffeescript.org](http://coffeescript.org) and click on the *Try CoffeeScript* tab. The site uses a browser version of the CoffeeScript compiler, converting any CoffeeScript typed inside the left panel, to JavaScript in the right panel.

In fact you can use browser based compiler yourself, by including [this script](http://jashkenas.github.com/coffee-script/extras/coffee-script.js) in a page, marking up any CoffeeScript script tags with the correct `type`.

Expand All @@ -39,7 +41,7 @@ This will give you a `coffee` executable. If you execute it without any command

coffee --compile my-script.coffee

If `--output` is not specified, CoffeeScript will write to a JavaScript file with the same name, in this case `my-script.js`. This will overwrite any existing files, so be careful you're overwriting any JavaScript files unintentionally. For a full list of the command line options available, please see the [docs](http://coffeescript.org).
If `--output` is not specified, CoffeeScript will write to a JavaScript file with the same name, in this case `my-script.js`. This will overwrite any existing files, so be careful you're overwriting any JavaScript files unintentionally. For a full list of the command line options available, pass `--help`.

As you can see above, the default extension of CoffeeScript files is `.coffee`. Amongst other things, this will allow text editors like [TextMate](http://macromates.com/) to work out which language the file contains, giving it the appropriate syntax highlighting. By default, TextMate doesn't include support for CoffeeScript, but you can easily install the [bundle to do so](https://github.com/jashkenas/coffee-script-tmbundle).

Expand Down
10 changes: 5 additions & 5 deletions coffeescript/chapters/01_syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ As mentioned earlier, there's no reason why the we can't use multiple lines, as
###Function arguments

How about specifying arguments? Well, CoffeeScript lets you do that by specifying arguments in a pair of rounded brackets before the arrow.
How about specifying arguments? Well, CoffeeScript lets you do that by specifying arguments in a pair of parenthesis before the arrow.

<span class="csscript"></span>

Expand Down Expand Up @@ -98,7 +98,7 @@ In the example above, `nums` is an array of all the arguments passed to the func

###Function invocation

Functions can be invoked exactly as in JavaScript, with brackets `()`, `apply()` or `call()`. However, like Ruby, CoffeeScript will automatically call functions if they are invoked with at least one argument.
Functions can be invoked exactly as in JavaScript, with parens `()`, `apply()` or `call()`. However, like Ruby, CoffeeScript will automatically call functions if they are invoked with at least one argument.

<span class="csscript"></span>

Expand All @@ -112,7 +112,7 @@ Functions can be invoked exactly as in JavaScript, with brackets `()`, `apply()`
# Equivalent to:
alert(inspect(a))

Although parenthesis is optional, I'd recommend using it if it's not immediately obvious what's being invoked, and with which arguments. In the last example, with `inspect`, I'd definitely recommend wrapping at least the `inspect` invocation in brackets
Although parenthesis is optional, I'd recommend using it if it's not immediately obvious what's being invoked, and with which arguments. In the last example, with `inspect`, I'd definitely recommend wrapping at least the `inspect` invocation in parens.

<span class="csscript"></span>

Expand Down Expand Up @@ -144,7 +144,7 @@ Object literals can be specified exactly as in JavaScript, with a pair of braces
object1 = {one: 1, two: 2}

# Without braces
object2 = one:1, two:2
object2 = one: 1, two: 2

# Using new lines instead of commas
object3 =
Expand Down Expand Up @@ -352,7 +352,7 @@ You can also use it in place of the `||` operator:

velocity = southern ? 40

If you're using a `null` check before accessing a property, you can skip that by placing the existential operator right before the opening brackets. This is similar to Ruby's `try` method.
If you're using a `null` check before accessing a property, you can skip that by placing the existential operator right before the opening parens. This is similar to Ruby's `try` method.

<span class="csscript"></span>

Expand Down
2 changes: 1 addition & 1 deletion coffeescript/chapters/03_classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ CoffeeScript uses prototypal inheritance to automatically inherit all of a class
parrot = new Parrot("Macaw")
alert("This parrot is no more") if parrot.rip()

It's worth pointing out though that static properties aren't inherited, only instance properties are. This is due to implementation details with JavaScript's prototypal architecture, and is a difficult problem to work around. The CoffeeScript team are actively looking at this problem though, so hopefully we'll see a solution to this soon.
It's worth pointing out though that static properties are copied to subclasses, rather than inherited using prototype as instance properties are. This is due to implementation details with JavaScript's prototypal architecture, and is a difficult problem to work around.

##Mixins

Expand Down
4 changes: 2 additions & 2 deletions coffeescript/chapters/04_idioms.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ As with `forEach()`, ES5 also includes a native map function that has a much mor
return item.name;
});

As we covered in the syntax chapter, CoffeeScript's comprehensions can be used to get the same behavior as `map()`. Notice we're surrounding the comprehension with brackets, which is **absolutely critical** in ensuring the comprehension returns what you'd expect, the mapped array.
As we covered in the syntax chapter, CoffeeScript's comprehensions can be used to get the same behavior as `map()`. Notice we're surrounding the comprehension with parens, which is **absolutely critical** in ensuring the comprehension returns what you'd expect, the mapped array.

<span class="csscript"></span>

Expand All @@ -61,7 +61,7 @@ CoffeeScript's basic syntax uses the `when` keyword to filter items with a compa

result = (item for item in array when item.name is "test")

Don't forgot to include the brackets, as otherwise `result` will be the last item in the array.
Don't forgot to include the parens, as otherwise `result` will be the last item in the array.
CoffeeScript's comprehensions are so flexible that they allow you to do powerful selections as in the following example:

<span class="csscript"></span>
Expand Down
Loading

0 comments on commit 774cb9e

Please sign in to comment.