Estos son los apuntes de Coffescript de Arnoldo Rodriguez, uno de los profesores de DesafioLatam, en Monterrey, México, están muy buenos, especialmente si quieres recorrer de forma rápida todas las virtudes que tiene coffeescript.
Comments in CoffeeScript
# single line comment
###
multiline comment, perhaps a LICENSE.
###
Variables and Scope
# All variables are by default scoped locally
my_var = "Hello world"
pi = 3.1416
# Attach a variable to the global or window object to make it global
window.my_var = "Hello world"
Functions
# use the slim arrow to declare a function
func = -> "bar"
sumXtoTen = (x) ->
10 + x
# arguments are specified in parenthesis before the arrow
times = (a, b) -> a * b
# you can specify default arguments
times = (a = 10, b = 15) -> a * b
# use splats to receive N arguments via an array
sum = (nums...) ->
result = 0
nums.reduce (total, num) -> total + num
# you can avoid parethesis in function calls with at least 1 argument
alert "hello world"
console.log "this is the end"
# on the contrary you cannot call a function without parenthesis and zero
# arguments
# this fails
alert
# this is O.K.
alert()
# use the fat arrow to bound a function call to the local context
# mostly used in callbacks
# avoid using the conventional self = this is not very coffeescriptish
this.clickHandler = -> alert "clicked"
element.addEventListener "click", (e) => this.clickHandler(e)
Objects Literals and Array Definition
# Object literals
# regular Javascript syntaxis
object1 = {one: 1, two: 2}
# without braces
object2 = one: 1, two: 2
# using new lines instead of commands
object3 =
one: 1
two: 2
# Arrays
# avoid the trailing comma
array1 = [1, 2, 3]
# using new lines
array2 = [
1
2
3
]
Director de DesafíoLatam. Ingeniero Civil Informático de la Universidad Federico Santa María. Emprendedor lean, dedicado al desarrollo de una mejor web con ruby on rails. Fanático de los números y las métricas, la música y la fotografía.