Definition
Coffee Script is an attempt to expose the good parts of JavaScript in a simple way. The golden rule of Coffee Script is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime.
² Overview Of Coffee Script
l Coffee Script is a small language that compiles into JavaScript.
l Coffee Script does not have the semi-colons and curly braces, similar syntax to the likes js.
l This means you can write less code and do things faster. It also makes it easier to read and maintain.
v Variables
In js
var message;
message = "Ready for some Coffee?";
alert(message);
In Coffee script
message = "Ready for some Coffee?"
alert(message)
l No variable declarations
l No semicolons
v Function
coffee = ->
confirm "Ready for some Coffee?"
l Always has a return value
Return String
coffee = ->
answer = confirm "Ready for some Coffee?"
"Your answer is " + answer
v Function Parameters
coffee = (message) ->
answer = confirm message
"Your answer is #{answer}"
v Calling Functions
coffee = -> == coffee()
coffee = (message) -> == 1. coffee("Yo")
2. coffee "Yo"
parenthesis optional
coffee = (message, other) -> == 1. coffee("Yo", 2)
2. coffee "Yo", 2
If we want a default message
coffee = (message = "Ready for some Coffee?") ->
answer = confirm message
"Your answer is #{answer}"
v If Statement
In js
if (age < 18) {
alert('Under age');
}
In Coffee script
1. if age < 18 alert 'Under age'
2. alert 'Under age' if age < 18
3. if age < 18 then alert 'Under age'
v Operators
Coffee Script
|
JavaScript
|
== or is
|
===
|
!= or isnt
|
!==
|
not
|
!
|
and
|
&&
|
or
|
||
|
true yes on
|
true
|
false no off
|
false
|
v Switch Statements
message = switch cupsOfCoffee
when 0 then 'Asleep'
when 1 then 'Eyes Open'
when 2 then 'Buzzed'
else 'Dangerous
v Arrays
Ranges
l In js
1. var range = [1, 2, 3, 4];
2. var range = [1, 2, 3];
l In Coffee script
1. range = [1..4] (Two Dots include end)
2. range = [1...4] (Three Dots excludes end)
v Objects
1. coffee = { name: 'French', strength: 1 }
l curly brackets optional
2. coffee = name: 'French', strength: 1
l commas optional
3. coffee =
name: 'French'
strength: 1
v Advantages
· Coffee Script has much nicer, more Ruby-like syntax than JavaScript.
· JavaScript does some very silly things, while Coffee Script has several safeguards to prevent unwanted behavior. (For example, if you forget to use the "var" keyword to define new variables in JavaScript, they will suddenly get global scope with no warnings! Coffee Script protects you from this behavior).
· Coffee Script should work nicely with jQuery
· Python style white spacing
· Ruby styled lightweight syntax
· Concise function declarations
· Class based inheritance
v Disadvantages
· Difficult to learn.
· Switch from JavaScript to coffee script not easy.
· Coffee script was designed in the vein of Python and Ruby, so if you hate those languages then you will probably hate coffee script.
· Its compile every time.
· You'll need a basic knowledge of JS for debugging purposes. You can't directly start here, naturally.
Reasons to use it:
· variable safety - no need to declare variable using var keyword like JS.
· loops and comprehensions : Comprehensions replace for loops in CoffeeScript, but they simply compile into the traditional javascript equivalent for-loop.There are other things you can do with compressions. For example, you won't always want to execute some code (like console.log) on each item.
console.log hero for hero, index in heroes when index % 2 == 0
· easy safe loops using has Own Property: for own x of obj
· string interpolation: "My name is #{name}". Coffee Script interpolates strings in similar fashion to ruby. Most expressions are valid inside the #{...} interpolation syntax.
· easy closures (do (x) -> ...)
· ranges: for i in [0..10] : Array range can be define as range = [1..4] (Two Dots include end) and range = [1..4] (Two Dots excludes end).
· range slicing: Easily slice data by using myArray[2..5] its return only data index starting from 2 to 5.
· existential operator: if x? then use(x), are?.you?.there?
Coffee Script's existential operator ? returns true unless a variable is null or undefined, which makes it analogous
· readable regular expressions
· De-structuring assignment: [a,b] = [x,y]
coffee> x = 1
= 1
coffee> y = 2
= 2
coffee> [x, y] = [y, x]
[ 2, 1 ]
coffee> x
=2
coffee> y
=1
In our assignment statement, we matched our value pattern to the structure of our value, swapping x and y.
· Number of lines of code has been shown to be one of the few good indicators for predicting defects. Coffee Script significantly reduces the number of lines of code in a project and therefore the number of defects.
· Coffee Script improvements: indenting two spaces and using -> plus no closing parents and curly braces makes nested callbacks much easier to read and write
· When discussing Coffee Script, you need to keep in mind that it is not a replacement for JavaScript. Coffee Script is a "JavaScript generator." Your browser doesn't need any new plug-ins or add-ons to utilize Coffee Script.
· This also means you can use all your favorite JavaScript libraries (jQuery, Knockout.js, Sammy.js and so on) with Coffee Script (and vice versa).
· With the proper use of white space you can make your script more readable as well as maintainable.
Why I Don't Use Coffee Script
· I have to learn another Ruby-like language.
· meta-programming become a poor experience.
· only a few resources available for this language.
No comments:
Post a Comment