一、let块做用域html
if(true){ var a=1; let b=2; } console.log("a:"+a);//a:1 console.log("b:"+b);//Uncaught ReferenceError: b is not defined
二、let经常使用于for循环es6
var a=[]; for (let i = 0; i < 10; i++) { a[i] = function () { console.log(i); //6 //变量i是let声明的,当前的i只在本轮循环有效。因此每一次循环的i其实都是一个新的变量,因而最后输出的是6 }; } a[6]();
三、typeof再也不是一个百分之百安全的操做安全
console.log(typeof x);//undefined console.log(typeof y);//Uncaught ReferenceError: y is not defined var x=2; let y=3;
typeof运行时y还没有声明,因此报错。babel
四、隐蔽的暂时性死区ide
//正确 function bar(x=2, y=x) { return [x, y]; } console.log(bar());//[2,2]
//报错 function bar(x=y, y=2) { return [x, y]; } console.log(bar());//Uncaught ReferenceError: y is not defined
五、不容许重复声明函数
不能在函数内重复声明参数学习
function bar(arg) { let arg;//Uncaught SyntaxError: Identifier 'arg' has already been declared }
一、const声明的变量也是块做用域,可是不能被屡次修改es5
let a=10; const b=20; console.log(a); console.log(b); a=100; b=200;//Uncaught TypeError: Assignment to constant variable.
二、const 声明变量时必须赋值spa
const a;//Uncaught SyntaxError: Missing initializer in const declaration
三、const能够指定为对象指针
const常亮指向对象的指针不能变,对象自己是能够改变的
let user={name:"starof",age:25}; const LOVE_YOU=user; console.log(user); /*LOVE_YOU=1;//报错*/ user.age=18; console.log(user);
下面代码在es5中输出inside,在es6中输出outside。
若是肯定是在es5中仍是在es6中运行?能够经过babel使其在es6中执行。
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <script src="js/browser.js"></script> </head> <body> <script type="text/babel"> function f() { console.log("outside"); } (function () { if (true) { //重复 声明f function f() { console.log("inside"); //babel解析为es6因此输出outside } } f(); }()) </script>
es6中规定:
var和function声明的全局变量,依旧是全局对象的属性。
let,const声明的全局变量不属于全局对象的属性。
如下代码输出什么结果?
let b=1; console.log(window.b++);//NaN
本文做者starof,因知识自己在变化,做者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:http://www.cnblogs.com/starof/p/6919624.html有问题欢迎与我讨论,共同进步。