1
0
Fork 0
mirror of https://github.com/jashkenas/coffeescript.git synced 2022-11-09 12:23:24 -05:00
jashkenas--coffeescript/documentation/sections/expressions.md
Geoffrey Booth b28e398396 [CS2] Docs updates (#4536)
* Docs: named functions and function declarations

* No more prototypal `extends`; update docs and example

* More comprehensive documentation of the existential operator; closes #1631

* Better document operators, including `from`

* No fat arrow class methods anymore

* Destructuring shouldn’t say that default values are applied in case of undefined or null

* Spinoff generator and async functions into their own sections; reorder things so that the sections on functions come just before classes, and destructuring goes next to the operators (which discuss assignment)

* Rewrite “CoffeeScript 2” section, making it less practical and more explanatory; move practical info into “Usage”

* Update “Variable Scoping and Lexical Safety” section to remove incorrect reference to Ruby (fixes #2360), add missing details about the safety wrapper, add note about `let`/`const`.

* Updated browser compiler

* Updated docs

* Rewrite Literate CoffeeScript breaking changes

* Split apart the “Breaking Changes” and “Unsupported Features” sections into separate sidebar items and files

* Add example of `not in`, closes #3281

* Fix words in bold that should be in backticks

* Consolidate some breaking changes sections

* Add Node API documentation; closes #3551

* Move the chaining documentation out of the changelog into its own section
2017-05-05 19:44:11 -07:00

1.5 KiB
Raw Blame History

Everything is an Expression (at least, as much as possible)

You might have noticed how even though we dont add return statements to CoffeeScript functions, they nonetheless return their final value. The CoffeeScript compiler tries to make sure that all statements in the language can be used as expressions. Watch how the return gets pushed down into each possible branch of execution in the function below.

codeFor('expressions', 'eldest')

Even though functions will always return their final value, its both possible and encouraged to return early from a function body writing out the explicit return (return value), when you know that youre done.

Because variable declarations occur at the top of scope, assignment can be used within expressions, even for variables that havent been seen before:

codeFor('expressions_assignment', 'six')

Things that would otherwise be statements in JavaScript, when used as part of an expression in CoffeeScript, are converted into expressions by wrapping them in a closure. This lets you do useful things, like assign the result of a comprehension to a variable:

codeFor('expressions_comprehension', 'globals')

As well as silly things, like passing a try/catch statement directly into a function call:

codeFor('expressions_try', true)

There are a handful of statements in JavaScript that cant be meaningfully converted into expressions, namely break, continue, and return. If you make use of them within a block of code, CoffeeScript wont try to perform the conversion.