Skip to content

Instantly share code, notes, and snippets.

@zeusdeux
Forked from rwaldron/tdz-1.js
Created October 13, 2015 18:42
Show Gist options
  • Save zeusdeux/5d115640087fc126222c to your computer and use it in GitHub Desktop.
Save zeusdeux/5d115640087fc126222c to your computer and use it in GitHub Desktop.
Temporal Dead Zone
{
// The block has begun, we're in a new block scope. The TDZ for the "a" binding has begun
var f = function() {
// 2. Because f() is evaluated before `a` is actually declared,
// an exception will be thrown indicating to the author that
// `a` is not yet defined.
console.log(a);
};
f(); // 1. f is called and results evaluated before the next line...
let a = 1;
// The `a` binding is now safe, but we never got here because f() killed us :(
// that's ok, because we can fix this bug and celebrate the win!
}
{
// The block has begun, we're in a new block scope. The TDZ for the "a" binding has begun
// Declare `a` before use.
let a = 1;
var f = function() {
// 2. Because f() is evaluated after `a` is declared,
// the value of `a` will be logged.
console.log(a);
};
f(); // 1. f is called and results evaluated
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment