From an outside look of it, I think it’s like a mixture of Python and Ruby. —Nango
1 2
| greet = (message) -> alert message
|
Advantages
- Least amount of code to solve problems
- Readable and Understandable
- Easy to Maintain
1 2
| message = "Ready for some Coffee?" alert(message)
|
=>
1 2 3
| var message; message = "Ready for some Coffee?"; alert(message);
|
All the code is wrapped up in a closure to prevent variable pollution
Use a default value of ‘Stranger’ for the name parameter
1
| greet = (name='Stranger') -> alert name
|
=>
1 2 3 4 5 6 7
| var greet; greet = function(name) { if (name == null) { name = 'Stranger'; } return "Hello, " + name; };
|
Kind of Like Ruby
ha, But In this field, it is called CoffeeScript-style string interpolation.
1 2
| greet = (name='Stranger') -> "Hello, #{name}"
|
One thing I like it is, you can be 任性 like this:
1 2 3 4 5
| coffee() coffee("Yo") coffee "Yo" coffee("Yo", 2) coffee "Yo", 2
|
Array
1 2 3 4 5 6 7 8 9
| storeLocations = ['Orlando', 'Winter Park', 'Sanford'] Can use new lines instead of commas storeLocations = [ 'Orlando' 'Winter Park' 'Sanford' ]
|
Loop
1 2 3 4 5 6
| storeLocations.forEach (location, index) -> alert "Location: #{location} alert "Location: for location in storeLocations alert "Location: #{location}"
|
Indent
There are things I don’t like like this.
1 2 3
| coffee = name: 'French' strength: 1
|
=>
1 2 3 4 5 6
| coffee = { name: 'French' }; ({ strength: 1 })
|
一行流 (one line combo)
If u do python, you familiar with this.
1 2 3
| filteredFlights = (flight for flight in currentFlights when stops is '2+' or flight.routing is 0)
|
OOP
the great extend
keyword
My friend Teddy
has a great post about this.
1 2 3
| class Animal class Snake extends Animal
|
= >
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
| (function() { var Animal, Snake, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; Animal = (function() { function Animal() {} return Animal; })(); Snake = (function(_super) { __extends(Snake, _super); function Snake() { return Snake.__super__.constructor.apply(this, arguments); } return Snake; })(Animal); }).call(this);
|
TODO
Others
With the character ->
, it can lower the possibility of missing braces or brackets.
There’re lots of other stuff, like automatically hoisting
for u to check out.
Install
1 2 3
| 1. Install Node.js http://nodejs.org/ 2. Install npm http://npmjs.org/ 3. $ npm install -g coffee-script
|
1 2 3 4 5 6 7
| coffee -c test.coffee Creates test.js coffee -cw test.coffee Every time test.coffee is updated re-compile. coffee -c src -o js Compile all .coffee files in the src dir into the js dir. coffee -wc src -o js Every time a file is updated re-compile
|
Link:
http://teddy1004.github.io/2014/12/04/class-inheritance-in-JavaScript/
http://coffeescript.codeschool.com/levels/1/challenges/1
http://courseware.codeschool.com/coffeescript_slides.pdf