let与var比的特点
var tmp = new Date();
function f() {
  console.log(tmp);
  if (false) {
    var tmp = 'hello world';
  }
}
f(); // undefined上述代码输出undefined的原因是函数f内的tmp,发生了变量提升,导致输出undefined
let其实是为ES6新增的块级作用域,用let声明的变量只在用{}围住的范围内起作用
function f1() {
  let n = 5;
  if (true) {
    let n = 10;
  }
  console.log(n); // 5
}let块级作用域的出现使得匿名立即执行函数不在有必要
// IIFE 写法
(function () {
  var tmp = ...;
  ...
}());
// 块级作用域写法
{
  let tmp = ...;
  ...
}原文:https://www.cnblogs.com/ailingstar/p/12341731.html