Home > coffeescript, Programming > Learning CoffeeScript

Learning CoffeeScript

Practice 1: Define a Student class, make some new instances and classify them into two arrays based on the student scores

class Student
    constructor:(@gender, @score)->

s1=new Student('M', 45)
s2=new Student('F', 78)
s3=new Student('F', 59)
s4=new Student('M', 99)

(if s.score>59 then passed else failed).push s for s in [s1, s2, s3, s4]
console.log passed
console.log failed

Practice 2: Array filtering

pr=(items, lbl)->
    console.log "==#{lbl}=="
    for item, i in items
        console.log "Item #{i}: #{item}"


odd=(item for item in a when item%2)
even=(item for item in a when item%2==0)
double=(item*2 for item in a)

pr odd, "odd"
pr even, "even"
pr double, "double"

Practice 3: more about class

class Candidate

class Election

e=new Election
console.log e.elect( new Candidate(0.34) )
console.log e.elect( new Candidate(0.59) )

Practice 4: serial_maker (based on
JavaScript: The Good Parts by Douglas Crockford
Chap 4.12)

class SerialMaker
    constructor:(@prefix='Q', @start=0)->

sm=new SerialMaker('SQ')
console.log sm.gen_seq()
console.log sm.gen_seq()
console.log sm.gen_seq()

Practice 5: (added 7/12/2012) Self-invoking constructor

class Person
    constructor:(name='no name')->
        return new Person(name) if !(@ instanceof Person)

        return @name


# or the above line can be written as 
# p1=new Person('Freedom')
# the result is exactly the same and the
# benefit is user doesn't need to worry about forgetting using the "new" keyword
console.log p1.id() + ','+ p1.isrich

Reference: http://arcturo.github.com/library/coffeescript/index.html

Categories: coffeescript, Programming
  1. June 26, 2012 at 10:20 pm

    Hi Rico,

    This is Chris Smith, author of the Backbone + CoffeeScript interactive book (http://www.scriptbooks.com). Awesome to come across your blog here!

  2. ricoch3n
    June 27, 2012 at 2:30 am

    Thanks Chris. I love every moment learning backbone.js through your (by far the best) one of a kind book covering the technologies that I enjoy learning. On one the week nights I literally went through the whole chapter 1 with php as the backend (just a convenient choice). Everything works great except with eco v1.1.0-pre I can’t use eco.compile, the workaround I found is to use the following fix (for instance):

        template: '''
                <%= @model.get('subject') %>
                <%= @model.get('message') %>
            <a href="#" class="btn">
        render: ->
            @$el.html eco.render(@template, {model:@model})
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: