当前位置: 首页 > Web前端 > HTML

ES6新语法(一)

时间:2023-04-02 12:22:57 HTML

ES6(ECMAScript6的缩写),是2015年6月正式发布的JavaScript语言标准let/const//varvara=1;vara=5;if(a>4){varb=10;}console.log(b);//10//让a=1;让a=5;//错误:标识符'a'已经被声明leta=5;if(a>4){letb=10;}console.log(b);//错误:b未定义//constconsta=1;a=5;//错误:给常量变量赋值.var可以重复定义,没有块级作用域,let不能重复定义块级作用域,const定义常量赋值letlist=[1,2,3]let[a,b,c]=listconsole.log(a,b,c)//123letdict={name:'jim',age:21}let{name,age}=dictconsole.log(name,age)//jim21letdict={name:'jim',age:21}let[name,age]=dict//报错:dict在解构赋值的过程中不可迭代,左右结构必须相同箭头函数(python中的lambda)#常用函数functionfn(arg1,arg2){returnarg1+arg2}#arrowFunction(arg1,arg2)=>arg1+arg2varfn=(arg1,arg2)=>arg1+arg2//很方便将来多次调用箭头函数。只有一个参数时,括号可以省略。没有参数时,必须写一个括号,当函数体只有一句话时,可以省略花括号,语句默认为返回值。你不需要在#js中编写returnfor。in只能用来遍历下表,这是一个很大的痛点。所以我真的很喜欢ofletiterator=[0,2,4,6,8,16,32,64]foriiniterator:console.log(i)的出现//0,1,2,3,4,5,6,7foriofiterator:console.log(i)//0,2,4,6,8,16,32,64默认参数//before函数fn(arg1,arg2){让x=arg1||5;让y=arg2||6;returnx+y}console.log(fn)//11//es6functionfn(arg1=5,arg2=6){returnarg1+arg2}console.log(fn)//11letlist=[1,2,3,4,5,6]functionfn(arg1,arg2,...args){console.log(arg1,arg2,args)}fn(...list)//12数组(4)数组(4)表示为[3,4,5,6]这种写法比较直观清晰,介绍了内部逻辑代码,和python很像。Arrayletlist=[1,2,3,4,5,6]//mapletnew_list=list.map((arg)=>{//每个元素都会作为参数传入if(arg>4){returntrue}else{returnfalse}})console.log(new_list)//[false,false,false,false,true,true]//reduceletlist=[1,2,3,4,5,6]letnew_list=list.reduce((sum,arg)=>{//第一次传入前两个参数,然后将结果作为第一个参数,其他list元素传入returnsum+arg})console.log(new_list)//21//filterletnew_list=list.filter((arg)=>{if(arg>4){returntrue//根据返回的布尔值判断是否添加新列表}})console.log(new_list)//[5,6]//forEachletnew_list=list.map((arg)=>{//每个元素都会作为参数传递返回arg})console.log(new_list)//[1,2,3,4,5]这是与python中的map、reduce、filter高阶函数功能几乎相同,只是在js中只是作为数组方法使用,python中没有forEach方法。原因可能是map可以达到效果。个人觉得大概是这样的,如果有什么不同,请告诉我,哈哈objectletname='jim'letage=21letobj={name:name,age:age,show:function(){console.log(name,age)}}obj.show()//jim21console.log(obj)//{name:"jim",age:21,show:?}//上面可以简化为同名keyletobj={name,age,show(){console.log(name,age)}}我们可以省略,取值只写一个。对于函数,我们可以省略函数stringletstr="http://www.jim.com/index.html"//不存在,盲写//startsWith判断是否使用Astringstartsif(str.startsWith('http://')){console.log("Thisisanhttpaddress")//Thisisanhttpaddress}//endsWith判断是否以某个后缀结尾if(str.endsWith('.html')){console.log("Thisisanhtmlfile")//Thisisanhtmlfile}很多语言的字符串都有这种判断方法,现在javascript也在不断改进,666面向对象//之前的函数Person(name,age){this.name=name;this.age=age;}Person.prototype.show=function(){//当然也可以在Person函数内部定义,如果使用箭头函数,函数就失去作用,this指向窗口objectthis.show=function(){functionbody}console.log(this.name,this.age)}letp=newPerson('jim','21');p.show()//jim21functionChil(名字,年龄,角色){Person.call(this)this.name=namethis.age=age//调用函数中直接写参数就可以this.role=role}Chil.prototype.show=Person.prototype.show不写就用不到show函数当然,这只是一种方式letc=newChil('han','23','student')c.show()//es6classPerson{constructor(name,age){this.name=namethis.age=age}show(){console.log(this.name,this.age)}}letp=newPerson('jim',23)p.show()//jim23classChilextendsPerson{constructor(name,age,role){super(name,age)this.role=role}}letc=newChil('han','23','student')c.show()han23使用class定义类,constructor定义了构造函数super来实现对基类构造的调用,而extends的继承借鉴了java等语言,使得js语法更易读易写