Skip to content

Instantly share code, notes, and snippets.

@seoyoochan
Created April 30, 2016 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seoyoochan/cad70ef5b58719f1b01d69a11b5968b2 to your computer and use it in GitHub Desktop.
Save seoyoochan/cad70ef5b58719f1b01d69a11b5968b2 to your computer and use it in GitHub Desktop.
ES6 TDZ(Temporary Dead Zone)
The w in the w + 1 default value expression looks for w in the formal parameters' scope,
but does not find it, so the outer scope's w is used.
Next, The x in the x + 1 default value expression finds x in the formal parameters' scope,
and luckily x has already been initialized, so the assignment to y works fine.
However, the z in z + 1 finds z as a not-yet-initialized-at-that-moment parameter variable,
so it never tries to find the z from the outer scope.
As we mentioned in the "let Declarations" section earlier in this chapter,
ES6 has a TDZ, which prevents a variable from being accessed in its uninitialized state.
As such, the z + 1 default value expression throws a TDZ ReferenceError error.
var w = 1, z = 2;
function foo( x = w + 1, y = x + 1, z = z + 1 ) {
console.log( x, y, z );
}
foo(); // ReferenceError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment