admin管理员组文章数量:1026989
ES5 typeof
is considered safe, as it will not throw ReferenceError
when checked agains a non-declared value. such as
console.log(typeof undeclaredVar); // undefined
however, when checking for typeof undeclaredLetConst
in es6 it will throw an error only if the value was later on declared with a let
or const
. if it was declared with var it will work normally.
console.log(typeof undeclaredLetConst);
let undeclaredLetConst = "hello"; // ReferenceError
whats happening there?
ES5 typeof
is considered safe, as it will not throw ReferenceError
when checked agains a non-declared value. such as
console.log(typeof undeclaredVar); // undefined
however, when checking for typeof undeclaredLetConst
in es6 it will throw an error only if the value was later on declared with a let
or const
. if it was declared with var it will work normally.
console.log(typeof undeclaredLetConst);
let undeclaredLetConst = "hello"; // ReferenceError
whats happening there?
Share Improve this question edited Sep 10, 2016 at 23:02 Bamieh asked Sep 10, 2016 at 15:38 BamiehBamieh 10.9k5 gold badges33 silver badges52 bronze badges1 Answer
Reset to default 12Why it works with var
declarations
When a JavaScript engine looks through a lexical scope block and finds a variable declaration with var
, it hoists the declaration to the top of the function (or global scope if no "use strict"
is present).
Hence the typeof
will never fail as the variable its checking upon will be hoisted beforehand.
The Temporal Dead Zone (TDZ)
The TDZ is never named explicitly in the ECMAScript specification, but the term is used to describe why let and const declarations are not accessible before their declaration.
Why it fails with const
and let
When a JavaScript engine looks through a lexical scope block and finds a variable declaration with let
or const
, it places the declaration in the TDZ. Any attempt to access a variable in the TDZ results in a runtime error.
The declaration is removed from the TDZ during runtime once the flow reaches the declaration itself.
console.log(typeof undeclaredLetConst); // "undefined"
if (1) {
let undeclaredLetConst = "no errors!";
}
undeclaredLetConst
isn’t in the TDZ when typeof
operation executes because it occurs outside of the block in which undeclaredLetConst
is declared. That means there is no value binding, and typeof simply returns "undefined".
Source: An awesome book by Nicholas C. Zakas, Understanding ECMAScript 6.
ES5 typeof
is considered safe, as it will not throw ReferenceError
when checked agains a non-declared value. such as
console.log(typeof undeclaredVar); // undefined
however, when checking for typeof undeclaredLetConst
in es6 it will throw an error only if the value was later on declared with a let
or const
. if it was declared with var it will work normally.
console.log(typeof undeclaredLetConst);
let undeclaredLetConst = "hello"; // ReferenceError
whats happening there?
ES5 typeof
is considered safe, as it will not throw ReferenceError
when checked agains a non-declared value. such as
console.log(typeof undeclaredVar); // undefined
however, when checking for typeof undeclaredLetConst
in es6 it will throw an error only if the value was later on declared with a let
or const
. if it was declared with var it will work normally.
console.log(typeof undeclaredLetConst);
let undeclaredLetConst = "hello"; // ReferenceError
whats happening there?
Share Improve this question edited Sep 10, 2016 at 23:02 Bamieh asked Sep 10, 2016 at 15:38 BamiehBamieh 10.9k5 gold badges33 silver badges52 bronze badges1 Answer
Reset to default 12Why it works with var
declarations
When a JavaScript engine looks through a lexical scope block and finds a variable declaration with var
, it hoists the declaration to the top of the function (or global scope if no "use strict"
is present).
Hence the typeof
will never fail as the variable its checking upon will be hoisted beforehand.
The Temporal Dead Zone (TDZ)
The TDZ is never named explicitly in the ECMAScript specification, but the term is used to describe why let and const declarations are not accessible before their declaration.
Why it fails with const
and let
When a JavaScript engine looks through a lexical scope block and finds a variable declaration with let
or const
, it places the declaration in the TDZ. Any attempt to access a variable in the TDZ results in a runtime error.
The declaration is removed from the TDZ during runtime once the flow reaches the declaration itself.
console.log(typeof undeclaredLetConst); // "undefined"
if (1) {
let undeclaredLetConst = "no errors!";
}
undeclaredLetConst
isn’t in the TDZ when typeof
operation executes because it occurs outside of the block in which undeclaredLetConst
is declared. That means there is no value binding, and typeof simply returns "undefined".
Source: An awesome book by Nicholas C. Zakas, Understanding ECMAScript 6.
本文标签: javascriptES6 typeof throws an errorStack Overflow
版权声明:本文标题:javascript - ES6 typeof throws an error - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745660093a2161837.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论