mirror of
https://github.com/jashkenas/coffeescript.git
synced 2026-05-03 03:00:14 -04:00
* Move analytics initialization into docs.coffee * Bump Bootstrap and CodeMirror versions * Update output * Merge CodeMirror styles * Update CodeMirror styles * Better flexbox layout for docs, including flex auto-sized sidebar and main column * Minor styling fixes for Edge * Fix scrollspy for new layout * Update output * Clicking on the CoffeeScript logo in the navbar should scroll to top; fix main column width on mobile * Update output * 2.3.0 changelog * Update operators section for ** * Update docs for ES2018 object rest/spread * Remove references to Node's experimental modules support * Move the README coffee cup to the right side, so our name isn't cut off on the NPM page; update README and CONTRIBUTING text * Update dependencies * Add compatibility note for async generators * Bump version to 2.3.0 and update output * Have CI test in Node 10 instead of 9 * Somehow this gets generated slightly differently in Node 10 * Fix #4042: Update broken link
14 lines
1.3 KiB
Markdown
14 lines
1.3 KiB
Markdown
## Bound (Fat Arrow) Functions
|
||
|
||
In JavaScript, the `this` keyword is dynamically scoped to mean the object that the current function is attached to. If you pass a function as a callback or attach it to a different object, the original value of `this` will be lost. If you’re not familiar with this behavior, [this Digital Web article](https://web.archive.org/web/20150316122013/http://www.digital-web.com/articles/scope_in_javascript) gives a good overview of the quirks.
|
||
|
||
The fat arrow `=>` can be used to both define a function, and to bind it to the current value of `this`, right on the spot. This is helpful when using callback-based libraries like Prototype or jQuery, for creating iterator functions to pass to `each`, or event-handler functions to use with `on`. Functions created with the fat arrow are able to access properties of the `this` where they’re defined.
|
||
|
||
```
|
||
codeFor('fat_arrow')
|
||
```
|
||
|
||
If we had used `->` in the callback above, `@customer` would have referred to the undefined “customer” property of the DOM element, and trying to call `purchase()` on it would have raised an exception.
|
||
|
||
The fat arrow was one of the most popular features of CoffeeScript, and ES2015 [adopted it](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions); so CoffeeScript 2 compiles `=>` to ES `=>`.
|