Created
April 30, 2016 19:44
-
-
Save seoyoochan/cad70ef5b58719f1b01d69a11b5968b2 to your computer and use it in GitHub Desktop.
ES6 TDZ(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
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. |
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
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