-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ES6-let const #19
Labels
Comments
let块作用域{
let a = 10;
var b = 1;
}
a; // ReferenceError: a is not defined.
b; babel 之后 "use strict";
{
var _a = 10;
var b = 1;
}
a;
b; |
不存在变量名提升var 和 let、const 的不同之处在于后两者是在编译时才初始化,所以会存在死区。 暂时性死区(temporal dead zone)var tmp = 123;
if (true) {
tmp = 1; // referenceError
let tmp;
}
不允许重复声明为什么需要块级作用域var tmp = new Date();
function f() {
console.log(tmp); // undefined
if (false) {
var tmp = 'hello world';
}
}
f(); 块级作用域内声明函数函数声明类似于var,即会提升到全局作用域或函数作用域的头部。 // 浏览器的 ES6 环境
function f() { console.log('I am outside!'); }
(function () {
if (false) {
// 重复声明一次函数f
function f() { console.log('I am inside!'); }
}
f();
}());
// Uncaught TypeError: f is not a function // 浏览器的 ES6 环境
function f() { console.log('I am outside!'); }
(function () {
var f = undefined;
if (false) {
function f() { console.log('I am inside!'); }
}
f();
}());
// Uncaught TypeError: f is not a function |
constconst A = 1;
A = 2;
// after babel
"use strict";
function _readOnlyError(name) { throw new Error("\"" + name + "\" is read-only"); }
var A = 1;
A = (_readOnlyError("A"), 2); |
顶层对象浏览器里是window,Node里是global。ES5中顶层对象和全局变量是等价的。 var a = 1;
// 如果在 Node 的 REPL 环境,可以写成 global.a
// 或者采用通用方法,写成 this.a
window.a // 1
let b = 1;
window.b // undefined
let b = 1;
console.log(window.b);
// after babel
"use strict";
var b = 1;
console.log(window.b); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
No description provided.
The text was updated successfully, but these errors were encountered: