又进行了一遍es6的练习,各种找资料,对比,补充,能更好地理解。
es6添加了对类的支持 引入了class
class Animal { constructor(name){ this.name=name; } sayName(){ console.log(`My name is ${this.name}`);//字符串模板 }}
类的继承
class Programmer extends Animal{ constructor(name){ super(name); } program(){ console.log(`I'm coding...`)//字符串模板 }}
测试类
const animal=new Animal('cat'), kittly=new Programmer('kittly'); animal.sayName(); kittly.sayName(); kittly.program();
解构
const [x,y]=getVal(),//函数返回值的解构 [name,,age]=['kittly','male','eighteen'];//数组解构function getVal(){ return [2,3]} console.log(`x:${x},y:${y}`)console.log(`name:${name},age:${age}`);
函数 1 参数默认值 ,2不定参数,3拓展参数
1 参数默认kittly
const sayHello=(name='kittly')=> console.log(`hello ${name}`);//箭头函数 sayHello(); sayHello('cat');
2 …x代表了所有传入add函数的参数
const add = (...x) =>{ return x.reduce((m,n)=>m+n)//函数返回参数的总和};console.log(add(1,2,3))console.log(add(1,2,3,5,6,7,0))
3 将一个数组以拓展参数的形式传递,它能很好地映射到每个单独的参数
const people = ['someone','theOne','one'];const sayHi=(people1,people2,people3)=>{ console.log(` Hi ${people1},${people2},${people3}`);}sayHi(...people);
旧方法
sayHi.apply(null,people)
for of 值遍历
let someArr=['a','b','c'];for(val of someArr){ console.log(val)//a,b,c}
Math
Math.acosh(3);//反双曲余弦(有啥用尼)1.76274....Math.hypot(3,4);//直角三角形斜边长 5,哈哈哈Math.imul(2,4);//8 32位带符号乘法8 Math.imul(-2,4);//-8
includes包含
console.log("abcdefg".includes('h'));//falseconsole.log("abcdefg".includes('e'));//true
repeat 重复
console.log("abcdefg".repeat(2));//abcdefgabcdefg
Array.from() 从类数组或可迭代对象产生一个数组
console.log(Array.from(document.querySelectorAll('*')));
//结果:返回所有标签:[html, head, meta, title, body, div#body, h1, p, script]
arr.fill(value,start,end)
用value填充数组,start(从0开始)到end(end-1)(可省略)console.log([1,2,3,4,5].fill(5))//用5填充数组5,5,5,5,5console.log([1,2,3,4,5].fill(7,1))//用7从1开始至结尾填充数组1,7,7,7,7console.log([1,2,3,4,5].fill(6,2,4))//用6填充2至4(不包括4)1,2,6,6,5