标签:参数 提升 tle log ext ret doctype doc es6
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script src="js/browser.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> { let a = 10; var b = 1; } //console.log(a); //报错 console.log(b); //1 for(let i = 0; i < 10 ; i++){} //报错console.log(i); //报错 var a = []; for(var i = 0 ; i < 10 ; i++){ a[i] = function(){ console.log(i); } } a[6](); //10 //let 版本 for(let i = 0 ; i < 10; i++){ a[i] = function(){ console.log(i); } } a[6](); //6 //由于i是let生命的,当前的i只在本路循环有效。所以每一次循环的i其实都是一个新的值,所以输出6; //不存在变量提升 //console.log(foo); //referunceError; //typeof foo; //typof 不在安全 let foo = 2; //暂时性死区 //只要块级作用域存在let命令,他声明的变量就绑定这个区域,不受外界影响 var tmp = 123; if(true){ //tmp = ‘abc‘; //referernceError let tmp; } //暂时性死区 if(true){ //死区开始 //tmp2 = ‘abc‘; //ReferenceError //console.log(tmp2); //ref let tmp2; //死区结束 console.log(tmp2); tmp2 = 123; console.log(tmp2); } function bar(x = y , y = 2){ return [x , y]; } //由于x = y,而y没有声明,属于死区 //bar(); //y is not defined function bar2(x = 2 , y = x){ return [x , y]; } console.log(bar2()); //[2,2] //不允许重复声明 if(true){ let a = 10; //var a = 1; //has oready been declared } if(true){ let a = 10; //let a = 10; //baocuo } //不许再函数内部重新声明参数 var func = function(args){ let args; //报错 } func(1) var func2 = function(args){ { let args; //不报错 } } func2(1); </script> </body> </html>
标签:参数 提升 tle log ext ret doctype doc es6
原文地址:http://www.cnblogs.com/xudy/p/6257717.html