Skip to content

Instantly share code, notes, and snippets.

@polotek
Created March 13, 2012 23:08
Show Gist options
  • Save polotek/2032494 to your computer and use it in GitHub Desktop.
Save polotek/2032494 to your computer and use it in GitHub Desktop.
Variable scope proposal for candor language
// All variables MUST be initially declared using a keyword.
// Let's use "var"
var g = "global"
h = "bad" // ReferenceError, cannot find undeclared variable "h"
foo() {
var b;
console.log(a) // ReferenceError, cannot find undeclared variable "a"
console.log(b) // "nil", undeclared variables default to nil (since candor doesn't do undefined right?)
}
foo();
bar(c, d) {
console.log(c) // "one", function arguments are implicitly declared in the scope of the function
var e = "two"
var baz = () {
console.log(d); // "two", lexical closure scoping
console.log(e); // "three", lexical closure scoping
console.log(g); // "global", lexical closure scoping works up to the global level
}
baz()
foo() // function statements implicitly declare variable in the appropriate scope
foo = "foo" // function vars can be overwritten just like normal
}
bar("one", "two")
testGlobal() {
g = "test" // works on declared global variable referenced through closure scope
console.log(g) // "test", changed value
}
testGlobal()
console.log(g) // "test", changed global value in testGlobal function
var g2 = "global"
testGlobal2() {
var g2 = "test global" // local variable shadows global
console.log(g2) // "test"
}
testGlobal2()
console.log(g2) // "global", unchanged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment