name = "Jay" // CoffeeScript doesn't need a var prefix greet = "Hello, #{name}" // String interpolation // Result: Hello, Jay grade = 88.43 withHonors = 100 <= grade > 85 // chained comparisons // Result: true songNotes = ["do", "re", "mi", "fa", "so"] // just an array but CoffeeScript knows how to deal with new lines
Also, CoffeeScript takes care to make sure that all of your variables are properly declared within lexical scope — you never need to write var yourself.
There are a lot more cool/useless stuff baked into CoffeeScript variables but to really use them you need functions. In JavaScript, functions are needed for almost anything. Its the same in CoffeeScript. CoffeeScript just declares functions differently - more lisp/scala like.
// Here is a function without a paramter foo -> alert('foobar') // foo is the function name // the part after the "->" is the body of the function // Here is a function with a parameter square = (x) -> x * x // Here is a function with default value on a parameter fill = (container, liquid = "coffee") -> "Filling the #{container} with #{liquid}..."
No comments:
Post a Comment