-
-
Save rwaldron/3932594 to your computer and use it in GitHub Desktop.
temporary dead zone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// --------------------------------------------------------------------- | |
// 1. UNCONTROVERSIAL AMONGST TC39 | |
// --------------------------------------------------------------------- | |
// read before write throws | |
{ | |
console.log(x); // throws | |
let x = 12; | |
console.log(x); | |
} | |
// functions are initialized on entry to block | |
{ | |
console.log(f); // prints function | |
function f() { } | |
} | |
// read before write throws even if it syntactically looks like the | |
// assignment happens before any read | |
{ | |
f(); // throws | |
let x = 12; | |
function f() { return x } // tries to read local x | |
} | |
// read before write throws even if there's all sorts of crazy control | |
// flow that makes it totally unclear syntactically whether that's going | |
// to happen; hence the term "temporal" -- the variable is dead for the | |
// time period between when it's in scope and when it's first initialized | |
{ | |
// might or might not call f | |
function helper(callback) { | |
if (Math.random() < 0.5) { | |
return callback(); | |
} | |
return "didn't feel like calling the callback"; | |
} | |
{ | |
helper(f); // throws iff helper calls f | |
let x = 12; | |
function f() { return x; } // tries to read local x | |
} | |
} | |
// ---------------------------------------------------------------------- | |
// 2. CONTROVERSIAL, i.e., DAVE HATES IT :) | |
// ---------------------------------------------------------------------- | |
// *only* allowed to be initialized with the syntactic initializer | |
{ | |
x = "initialized"; // throws | |
let x = "foobar"; | |
} | |
// legal: | |
{ | |
let x = foo() ? bar() : baz(); | |
} | |
// illegal: | |
{ | |
let x; | |
if (foo()) { | |
x = bar(); // throws | |
} else { | |
x = baz(); // throws | |
} | |
} |
{
let x;
if (foo()) {
x = bar(); // throws
} else {
x = baz(); // throws
}
}
throws????
I know this throws
{
if (foo()) {
let x = bar(); // throws
} else {
let x = baz(); // throws
}
console.log(x)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
throws????
I know this throws