Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Pattern to Declare Global Variables #34

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ The details in this guide have been very heavily inspired by several existing st
- [PEP-8][pep8]: Style Guide for Python Code
- Bozhidar Batsov's [Ruby Style Guide][ruby-style-guide]
- [Google's JavaScript Style Guide][google-js-styleguide]
- [Variables and Scope][coffeescript-variables-scope]
- [Common CoffeeScript Idioms][common-coffeescript-idioms]
- Thomas Reynolds' [CoffeeScript-specific Style Guide][coffeescript-specific-style-guide]
- Jeremy Ashkenas' [code review][spine-js-code-review] of [Spine][spine-js]
- Alex McCaw and Jeremy Ashkenas' [The Little Book On CoffeeScript][little-book-coffeescript]
- The [CoffeeScript FAQ][coffeescript-faq]


## Table of Contents

* [The CoffeeScript Style Guide](#guide)
Expand All @@ -34,6 +37,7 @@ The details in this guide have been very heavily inspired by several existing st
* [Block Comments](#block_comments)
* [Inline Comments](#inline_comments)
* [Naming Conventions](#naming_conventions)
* [Variables and Scope](#variables_and_scope)
* [Functions](#functions)
* [Strings](#strings)
* [Conditionals](#conditionals)
Expand Down Expand Up @@ -241,7 +245,21 @@ Methods and variables that are intended to be "private" should begin with a lead
```coffeescript
_privateMethod: ->
```
<a name="variables_and_scope"/>
## Variables and Scope

Variable assignment is always local to the scope of the function in which it is declared. To explicitly declare a global variable, either directly setting it as a property on the global object:

```coffeescript
# in browsers
window.foo = "bar"
```
or with the following pattern:

```coffeescript
exports = this
exports.foo = "bar"
```
<a name="functions"/>
## Functions

Expand Down Expand Up @@ -488,7 +506,8 @@ console.log args... # Yes
[pep8]: http://www.python.org/dev/peps/pep-0008/
[ruby-style-guide]: https://github.com/bbatsov/ruby-style-guide
[google-js-styleguide]: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
[coffeescript-variables-scope]: http://arcturo.github.com/library/coffeescript/02_syntax.html
[common-coffeescript-idioms]: http://arcturo.github.com/library/coffeescript/04_idioms.html
[coffeescript-specific-style-guide]: http://awardwinningfjords.com/2011/05/13/coffeescript-specific-style-guide.html
[coffeescript-faq]: https://github.com/jashkenas/coffee-script/wiki/FAQ
[camel-case-variations]: http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms
[camel-case-variations]: http://en.wikipedia.org/wiki/CamelCase#Variations_and_synonyms