-
Notifications
You must be signed in to change notification settings - Fork 6
Scopes and Variables
Shane Brinkman-Davis Delamore edited this page Mar 16, 2018
·
13 revisions
Variables are automatically declared in the top-most scope where they are assigned.
Example:
# CaffeineScript
# global.b == 2
a = -> b = 3
a()
Math.max 1, b # 2
# JavaScript:
let a = function() {
let b;
return (b = 3);
};
a();
Math.max(1, global.b);
versus:
# CaffeineScript
b = 2
a = -> b = 3
a()
Math.max 1, b # 3
# JavaScript:
let b = 2;
let a = function() {
return (b = 3);
};
a();
Math.max(1, global.b);
Each of these constructs define a scope:
- Function Definition
- Class Definition
-
Comprehensions and Iteration (
when
andwith/do
blocks define scopes,from/in
andinto/returning
are evaluated before and outside the comprehension and thus do not) -
Control Structures (
while
anduntil
loops bodies define scopes,if
andunless
do not)
CaffeineScript's auto-lets are much the same as CoffeeScript's, with one big difference: the body of the with/do
clause of a comprehension also defines a scope.
Is there an interest in manual let
and const
? If so, let me know. They wouldn't be hard to add.
- Home
- Get Started
- Benefits
- Highlights
- Productivity by Design
- CaffeineScript Design
- What is CaffeineScript Good For?
- Get the most out of JavaScript
- Language Comparison
- CHANGELOG
- Blocks Instead of Brackets
- Binary Line-Starts
- Everything Returns a Value
- Streamlined Modules
- Scopes and Variables
- Optional Commas
- Semantics
- Ambiguities