码迷,mamicode.com
首页 > 其他好文 > 详细

[ES6] 04. The let keyword -- 2 Fiald case

时间:2014-11-19 23:56:04      阅读:321      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   on   div   

Fiald case 1: let can work in it‘s block

{
    let a = 10;
    var b = 1;
}

a // ReferenceError: a is not defined.
b //1

 

Case 2: Let has no "Hosting" Problem

function do_something() {
  console.log(foo); // ReferenceError
  let foo = 2;
}
function do_something() {
  console.log(foo); //undefined
  var foo = 2;
}

//hosting:
function do_something() {
 var foo = undefined;   
  console.log(foo); 
  foo = 2;
}

 

Case 3: let不允许在相同作用域内,重复声明同一个变量: In a block, you can only define one variable one time.

// 报错
{
    let a = 10;
    var a = 1;
}

// 报错
{
    let a = 10;
    let a = 1;
}

 

But if you do something like:

function f1() {
  let n = 5;
  if (true) {
      let n = 10;
  }
  console.log(n); // 5
}

There is no problem! becuase 5 and 10 are in different block scope.

 

[ES6] 04. The let keyword -- 2 Fiald case

标签:style   blog   io   ar   color   os   sp   on   div   

原文地址:http://www.cnblogs.com/Answer1215/p/4109489.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!