CoffeeScript
During the holidays i had the chance to try out this "converter" beast and i must say that i really enjoyed it.
For those who don't know what coffeescript is here is a short summary of it:
CoffeeScript is a little language that compiles into JavaScript. Underneath all those awkward braces and semicolons, JavaScript has always had a gorgeous object model at its heart. CoffeeScript is an attempt to expose the good parts of JavaScript in a simple way.
The golden rule of CoffeeScript is: "It's just JavaScript". The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime. You can use any existing JavaScript library seamlessly from CoffeeScript (and vice-versa). The compiled output is readable and pretty-printed, passes through JavaScript Lint without warnings, will work in every JavaScript implementation, and tends to run as fast or faster than the equivalent handwritten JavaScript.
Neat eh? Well yes it is - basically it converts your coffescript to javascript(and even passes the Lint test). Many people(the pessimists) are complaining that its hard to debug, but thats not true if you know javascript, because the compiled javascript is definitely readable, so don't be lazy learn javascript first before diving into coffee or wait till there are better debugging tools available(this should happen soon actually).
My experience with coffee was smooth like butter:
* Code writing felt like python(because of the INDENT logic).
* Even though coffee ads Classes, but underneath it, its all javascript objects.
* Most of the logic operators were borrowed/are similar from/to ruby (i.e: is - == , isnt - !== , true yes on - true, ...).
* Features like array/object comprehension, destructive assignment, splats,... - python has them all.
* Some other cool features like the "Existential Operator" - "?" that checks if variable is defined and not null.
Note: For me coffee felt like javascript, python and ruby combined, and i know those languages quite extensively(well except ruby) thus i felt like being in cosy, warm place - home, but you may not!, as there some people who can't get over the "curly brace syndrome" i had that illness once myself..
When i started meddling with coffescript i began using classes right away, but i quickly ran into some "problems" or to say better, i had questions about how the classes work, as the documentation was not that clear.
Questions:
1. Does coffee support/expose private variables?
2. What about static variables?
Answer:
Well, i actually knew that coffee does support them, because after all coffee is just javascript, so what can javascript do, coffee can too, it just that i wasn't sure how, but i figured it out quite simple - by compiling coffee file :)
Heres is an example of how the variable scoping works in classes:
class Animal
#static variable // note here how the '@' is used
@name3 = 'Anna'
#private variable
name2 = 'john'
constructor: ->
#instance variable
@name = 'rex'
move: (meters): ->
console.log "#{@name} moved #{meters} meters"
and in Javascript this would look like:
var Animal;
Animal = (function(){
function Animal(){
//instance variable
this.name = 'rex'
}
//private variable
var name2 = 'john';
//static variable
// if in coffeescript '@' is used inside the class, but outside a method it refers to the class rather then "this"
Animal.name3 = 'Anna'
Animal.prototype.move = function(meters){
console.log(this.name + " moved " + meters + " meters");
}
return Animal
);
So what i actually tried to show you here is that "@" does not always mean "this".
So.. Like i said i really enjoy drinking coffee, you should try it too, but be careful coffee is quite addictive, once you try it, it is hard to stop, because you can buy it anywhere - be it browsers, server side(nodejs, rhino), cross platfrom development(titanium appcelerator, phonegap)