Skip to content

Scopes and Variables

Shane Brinkman-Davis Delamore edited this page Mar 16, 2018 · 13 revisions

Auto Variable Declaration (Auto-Lets)

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);

The Scopes

Each of these constructs define a scope:

Compared with CoffeeScript

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.

Manual Lets and Const

Is there an interest in manual let and const? If so, let me know. They wouldn't be hard to add.

Clone this wiki locally