1.const声明常量不可改变,不属于顶层对象varstr=1window.strconststrwindow.str//undefined不存在变量提升console.log(str)varstr='es6'不报错undefinedconsole.log(str)conststr='es6'error//临时死区if(){console.log(str)varstr='es6'//大括号错误}//块级scope大括号是块级作用域if(){conststr='es6'}console.log(str)//错误报告的基本数据类型将存在于栈内存(stack)中参考:堆内存(heap)在栈内存中存放的是引用地址,所以可以改变地址而不改变值Object.freeze(引用类型)不能改变,只能冻结第一层。如果每一层都被冻结,递归函数myFreeze(obj){Object.freeze(obj);Object.keys(obj).forEach(function(key)){if(typeofobj[key]==='object'){myFreeze(obj[key])}}}2箭头函数constsum3=(x,y)=>{returnx+y;}constsum3=(x,y)=>x+y(只有一个参数括号也可以被遗漏)箭头函数中没有this,指向上层作用域,没有窗口,箭头函数不能获取参数,箭头函数不能作为构造函数3Templatecharactersstring``backticksconsole.log(我想学${this.name})变量使用${}得到4个解构赋值constcourse={name:'es6',price:'500'}const{name,price}=courseconsole.log(name,price)const[a,b,c]=courseArr重名问题:const{name:cname,//aliasprice,teacher:{name,age}}console.log(cname,name)Usageconstsum=([a,b,c])=>{console.log(a+b+c)}sum([1,2,3])constfoo=({name,age,sc='111'})=>{}解构设置默认值根据传入的值传入函数的返回值也可以结构化returnconst{name,age}=foo();JSONstringJSON.parse(a)5ES6不能被所有浏览器识别做什么(安装babel)npminstall--save-devbabel-preset-envbabel-cli-D=--saveconfigfile:.babelrc6spreadoperatorletbar={a:1,b:2};让baz={...bar};//{a:1,b:2}上面的方法其实等价于:letbar={a:1,b:2};让baz=Object.assign({},bar);//{a:1,b:2}letobj1={a:1,b:2};letobj2={...obj1,b:'2-edited'};console.log(obj1);//{a:1,b:2}console.log(obj2);//{a:1,b:"2-edited"}functionadd(x,y){returnx+y;}constnumbers=[4,38];add(...numbers)//42[...'你好']//["h","e","l","l","o"]http://es.xiecheng.live/
