Skip to content

Commit

Permalink
stub out classes
Browse files Browse the repository at this point in the history
  • Loading branch information
maccman committed Jun 3, 2011
1 parent 17c3a38 commit 40b3fbe
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 8 deletions.
2 changes: 1 addition & 1 deletion coffeescript/00_introduction.html
Expand Up @@ -16,7 +16,7 @@
<body>
<div id="container">
<header>
<h1><a href="index.html">CoffeeScript</a></h1>
<h1><a href="index.html">The Little Book on CoffeeScript</a></h1>
</header>

<div id="content">
Expand Down
2 changes: 1 addition & 1 deletion coffeescript/01_syntax.html
Expand Up @@ -16,7 +16,7 @@
<body>
<div id="container">
<header>
<h1><a href="index.html">CoffeeScript</a></h1>
<h1><a href="index.html">The Little Book on CoffeeScript</a></h1>
</header>

<div id="content">
Expand Down
2 changes: 1 addition & 1 deletion coffeescript/03_classes.html
Expand Up @@ -16,7 +16,7 @@
<body>
<div id="container">
<header>
<h1><a href="index.html">CoffeeScript</a></h1>
<h1><a href="index.html">The Little Book on CoffeeScript</a></h1>
</header>

<div id="content">
Expand Down
2 changes: 1 addition & 1 deletion coffeescript/04_applications.html
Expand Up @@ -16,7 +16,7 @@
<body>
<div id="container">
<header>
<h1><a href="index.html">CoffeeScript</a></h1>
<h1><a href="index.html">The Little Book on CoffeeScript</a></h1>
</header>

<div id="content">
Expand Down
2 changes: 1 addition & 1 deletion coffeescript/04_idioms.html
Expand Up @@ -16,7 +16,7 @@
<body>
<div id="container">
<header>
<h1><a href="index.html">CoffeeScript</a></h1>
<h1><a href="index.html">The Little Book on CoffeeScript</a></h1>
</header>

<div id="content">
Expand Down
59 changes: 59 additions & 0 deletions coffeescript/chapters/03_classes.md
Expand Up @@ -2,3 +2,62 @@

#Classes

Classes in JavaScript seem to have the kind of effect that a clove of garlic has to Dracula for some purists; although let's be honest, if you're that way inclined, you're unlikely to be reading a book on CoffeeScript. However, it turns out that they're just as damn useful in JavaScript as they are in other languages and CoffeeScript provides a great class abstraction.

As they're not available natively in JavaScript, CoffeeScript emulates classes behind the scenes, handling things like instantiation and inheritance. All that's exposed to you, as a developer, is the `class` keyword.

class Animal

In the example above, `Animal` is the name of the class, and also the name of the resultant variable that you can use to create instances. Since, behind the scenes, CoffeeScript is using construction functions you can instantiate classes using the `new` keyword.

animal = new Animal

Defining constructors (functions that get invoked upon instantiation) is simple, just use a function named `constructor`. This is akin to using's Ruby's `initialize` or Python's `__init__`.

class Animal
constructor: (name) ->
@name = name

In fact, CoffeeScript provides a shorthand for the common pattern of setting instance properties. By prefixing argument's with `@`, CoffeeScript will automatically set the arguments as instance properties in the constructor. Indeed, this shorthand will also work for normal functions outside classes.

class Animal
constructor: (@name) ->

As you'd expect, any arguments passed on instantiation are proxied to the constructor.

animal = new Animal("Parrot")
alert("Animal is a #{animal.name}")

##Instance properties

binding this

##Static properties

Inside of a class definition, "this" refers to the class object. So, in brief:

Don't get inherited

##Inheritance

class Parrot
constructor: ->
super("Parrot")
alive: ->
false
dead: ->
not @alive()

##Super

##Mixins

extend = (obj, mixin) ->
for name, method of mixin
obj[name] = method

include = (klass, mixin) ->
extend klass.prototype, mixin
2 changes: 1 addition & 1 deletion coffeescript/index.html
Expand Up @@ -9,7 +9,7 @@
<body>
<div id="container">
<header>
<h1><a href="index.html">CoffeeScript</a></h1>
<h1><a href="index.html">The Little Book on CoffeeScript</a></h1>
</header>

<div id="content">
Expand Down
2 changes: 1 addition & 1 deletion coffeescript/site/index.ms
Expand Up @@ -9,7 +9,7 @@
<body>
<div id="container">
<header>
<h1><a href="index.html">CoffeeScript</a></h1>
<h1><a href="index.html">The Little Book on CoffeeScript</a></h1>
</header>

<div id="content">
Expand Down
2 changes: 1 addition & 1 deletion coffeescript/site/page.ms
Expand Up @@ -16,7 +16,7 @@
<body>
<div id="container">
<header>
<h1><a href="index.html">CoffeeScript</a></h1>
<h1><a href="index.html">The Little Book on CoffeeScript</a></h1>
</header>

<div id="content">
Expand Down

0 comments on commit 40b3fbe

Please sign in to comment.