I was playing around new ECMASCRIPT-6 const key word. I did not understand one particular behaviour of the keyword.
Lets say I have two functions
First case
(function(){
console.log(_t);
const _t=10;
})();
and Second case
function t(){
console.log(_y);
const _y=11;
}
t();
For the first case the output is (didn't understand why)
ReferenceError: can't access lexical declaration `_t' before initialization
For the second case the output is (fine)
undefined
The second case output is as expected but I'm not getting any idea why the first case result throws error. It can be inferred from the error that the variable is not hoisted. But why? I found here that const
uses block scope. Has it anything to do with this scoping?
I'm using Firefox Developer Version console to run tests.
I was playing around new ECMASCRIPT-6 const key word. I did not understand one particular behaviour of the keyword.
Lets say I have two functions
First case
(function(){
console.log(_t);
const _t=10;
})();
and Second case
function t(){
console.log(_y);
const _y=11;
}
t();
For the first case the output is (didn't understand why)
ReferenceError: can't access lexical declaration `_t' before initialization
For the second case the output is (fine)
undefined
The second case output is as expected but I'm not getting any idea why the first case result throws error. It can be inferred from the error that the variable is not hoisted. But why? I found here that const
uses block scope. Has it anything to do with this scoping?
I'm using Firefox Developer Version console to run tests.
Share Improve this question asked May 27, 2015 at 9:34 years_of_no_lightyears_of_no_light 9581 gold badge10 silver badges25 bronze badges 3- 2 you have encountered the temporal dead zone, which is not yet pletely implemented across browsers. – the8472 Commented May 27, 2015 at 15:18
- See Are variables declared with let or const not hoisted in ES6? for how it should work. I cannot explain the second behaviour though - looks like a bug to me. – Bergi Commented Sep 18, 2015 at 15:56
-
Did you define any
_y
in an outer scope (in other code that ran), does the same thing happen when you run only these two snippets in a clean environment? – Bergi Commented Sep 18, 2015 at 16:01
2 Answers
Reset to default 4This is Firefox related issue as mentioned in here
Firefox-specific notes
The const declaration has been implemented in Firefox long before const appeared in the ECMAScript 6 specification. For const ES6 pliance see bug 950547 and bug 611388.
Starting with Gecko 36 (Firefox 36 / Thunderbird 36 / SeaMonkey 2.33):
{const a=1};a now throws a ReferenceError and does not return 1 anymore due to block-scoping. const a; now throws a SyntaxError ("missing = in const declaration"): An initializer is required. const a = 1; a = 2; now also throws a SyntaxError ("invalid assignment to const a").
Also I found something here as well
I think Firefox engine is very strict on const hoisting.
I think this makes sense.
When I try it in Firefox (38.0.1), I get the same error message for both; that it can't access it before initialization. That makes sense, as the only difference is that there is a function expression and a function declaration.
The constant identifiers are actually hoisted, that's why you get the error that it can't be accessed before it's initialized.
In this case block scope and function scope is the same, as the function code blocks are the only blocks that you have.
If you add a code block so that the constant is out of scope when you use it, you will get the error message "ReferenceError: _y is not defined" instead:
function t(){
{
const _y = 11;
}
console.log(_y); // _y is out of scope
}
t();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745389410a4625591.html
评论列表(0条)