Sonntag, 24. November 2013

Quotes from Coffeescript Application Development book

Above I posted some extractions from the book Coffeescript Application Development I found the most interesting.

Classes
Most of the time, you will define your methods in the main body of the class definition. This is the easiest and most logical place. However, there are a few occasions when you may wish to attach a method to an object prototype from somewhere else in the code. This may be for stylistic or metaprogramming reasons, or most likely, to attach methods to an object that you did not define. CoffeeScript provides the :: operator for easy access to an object's prototype.
 Variable arguments list
Appending ... to an argument name in the function definition tells CoffeeScript to accept any number of arguments. That variable will be assigned an array containing all the relevant values that were passed.
Check on existence
CoffeeScript has a helpful existential operator to deal with these situations correctly and cleanly. It is a simple ? symbol.
Destructuring assignments
We can even use destructuring assignment to perform in-place variable swaps:
[first, second] = ["cow", "milk"]
# Wait, or is it the other way around?
[first, second] = [second, first]
console.log "Don't put the #{second} before the #{first}."
Destructuring assignment can be used with more complicated structures too:
[drink, [alcohol, mixer]] = ["Screwdriver", ["vodka", "orange juice"]]
console.log "A #{drink} consists of #{alcohol} and #{mixer}."
Loops return array of results
One extremely useful feature of CoffeeScript loops is that they return a value—an array of the results of the inner statement for every item in the loop. If you've ever used Array.prototype.map to transform an array, you'll be happy to see that it's easy to achieve the same functionality in CoffeeScript.
Link on book in store

Keine Kommentare:

Kommentar veröffentlichen