该文章是初次涉猎ES6作的笔记,刚开始简单的东西我就pass掉
代码运行环境(运行脚手架只须要在src/index.js写完代码输入npm run build就能够运行)
let a=[1,2,3] b=[...a] b.push(6) console(b) ==>1,2,3,6
function a(a,...arg)==>a(1,2,3),arg=[2,3]
let a="china" let b=`i am from ${a},<br>and you?`
let searchWorld="am" let a="i am you" //是否包含,开头是否包含,结尾是否包含 a.include(searchWorld) a.startsWith(searchWorld) a.endsWith(searchWorld) //字符串自身复制,参数是次数,小数就取整 console.log(a.repeat(3))
//0B开头是二进制标志 let a = 0B0011 //打印3 console.log(a) //0o开头是八进制标志 let a = 0o0011 console.log(a)
let a = 10/3 //判断是否是数字 console.log(Number.isFinite(a))//true //判断是否是NaN console.log(Number.isNaN(NaN))//true //转换整数小数 console.log(Number.parseInt(a))//取整 console.log(Number.parseFloat(a)) //判断是否为整数 console.log(Number.isInteger(a))//false //es6的最大安全值 console.log(Math.pow(2,53)-1) //最大,最小常量数,至关于const定义变量 console.log(Number.MIN_SAFE_INTEGER) console.log(Number.MAX_SAFE_INTEGER) //判断是否安全数字 console.log(Number.isSafeInteger(a))//true
//将字符串转为数字 console.log(Math.trunc("123"))//123 console.log(Math.trunc("12abc"))//NaN console.log(Math.trunc("abc"))//NaN
//判断正负数 console.log(Math.sign(-123))//-1 console.log(Math.sign(123))//1 console.log(Math.sign(0))//0
//计算立方根 console.log(Math.cbrt(8))
//计算32位二进制 console.log(Math.clz32(8))
//计算乘法 console.log(Math.imul(8))//0 console.log(Math.imul(8,2))//16
//计算乘法 console.log(Math.hypot(1,1,1,1))//return (1^2+1^2+1^2+1^2)的平方根==>2
在github传了官方pdf,更多方法就在那里查阅
let jsonData={ "0":0, "1":1, "2":2, "length":3 } let arr=Array.from(jsonData) console.log(arr)//[0,1,2]
let txt="1,2,3,4,5" let data=Array.of(txt) console.log(data)
let arr=[0,1,2,3,4,5,6,7,8] console.log(arr.copyWithin(1,3,8))//[0,3,4,5,6,7,6,7,8]
let arrDemo=[0,1,2,3,4,5,6,1,8] console.log(arrDemo.find((value,index,arr)=>{ return value >5 }))//6
let arrDemo=[0,1,2,3,4,5,6,1,8] console.log(arrDemo.fill("x",2,5)) //[0, 1, "x", "x", "x", 5, 6, 1, 8]
let arrDemo=[0,1,2,3,4,5,6,1,8] for (let i of arrDemo) { console.log(i)//依次打印每一个元素 }
let arrDemo=[0,1,2,3,4,5,6,1,8] for (let i in arrDemo) { console.log(i) }
<span style="color:red;font-weight:bold">在有些场景in很差用,例如</span>git
let arrDemo=["1","2","3"] for (let [index,val] of arrDemo.entries()) { console.log(index,val) }//能输出key和value
let arrDemo=["1","2","3"] for (let [index,val] in arrDemo.entries()) { console.log(index,val) }//不能输出
let arrDemo=["a","b","c"] let flag=arrDemo.entries() console.log(flag.next().value) console.log(flag.next().value) console.log(flag.next().value) console.log(flag.next().value) //输出[0,"a"],[1,"b"],[2,"c"]
function addDemoOne(a,b){ return a+b } function addDemoTwo(a,b=3){ return a+b } console.log(addDemoOne(2,3))//5 console.log(addDemoTwo(2))//5
function addDemoTwo(a,b=3){ return a+b } console.log(addDemoTwo(1,2))//3
function addDemoTwo(a,b=3){ if(a === 1){ throw new Error("值错误") } return a+b } console.log(addDemoTwo(1))//Uncaught Error: 值错误
在这里有个坑,请看demo2,3
function addDemoOne(a,b,c){ return a+b } console.log(addDemoOne.length)//3 function addDemoTwo(a,b,c=2){ return a+b } console.log(addDemoTwo.length)//2 function addDemoThree(a,b=2,c){ return a+b } console.log(addDemoThree.length)//1
function addDemoOne(a,b,c){ return a+b } console.log(addDemoOne.name)//addDemoOne let addDemoTwo = function addDemoThree(){} console.log(addDemoTwo.name)//addDemoThree
let demo = (a,b) => a+b//省略 return,若是加return则必须加{} console.log(demo(2,3))//5
let jsonData={ a:100, b:200 } function addDemoTwo({a,b=3}){ return a+b } console.log(addDemoTwo(jsonData))//300
判断json存在某属性
let jsonData={ a:1, b:2 } console.log("a" in jsonData)//true console.log("c" in jsonData)//false
判断数组是否为空es6
let arrOne=[,,,,] let arrTwo=[1,2,3] console.log(0 in arrOne)//false console.log(0 in arrTwo)//true
将对象合并github
var a={a:1} var b={b:2} var c=Object.assign(a,b)//能够合并多个 console.log(c)//{a: 1, b: 2}
let arr =[1,2,3,4] function addDemoTwo(a,b,c,d){ console.log(a,b,c,d) } addDemoTwo(...arr)//1,2,3,4
let testData=Symbol('hello') console.log(testData)//Symbol(hello) console.log(typeof testData)//symbol
let demoOne=Symbol('hello') let demoTwo=Symbol('word') let jsonData={ a:'aaa', b:'bbb' } jsonData[demoOne]='ccc' jsonData[demoTwo]='ddd' for(let i in jsonData){ console.log(jsonData[i])//遍历不出symbol属性 } console.log(jsonData);//能够遍历出
let demoData=new Set([1,2,3,4,5,5])//不容许重复元素 console.log(demoData); demoData.add(6) console.log(demoData); demoData.delete(1) console.log(demoData); console.log(demoData.has(1)); console.log(demoData.has(2)); for (let i of demoData){ console.log(i); } demoData.clear() console.log(demoData);
去除数组中重复的元素能够这样
let arr=[1,2,3,3,4] let setOne=new Set(arr) console.log(setOne);
let demo={a:1,b:2} let setData=new WeakSet() setData.add(demo) console.log(setData);
let json={name:"fan",age:18} let m = new Map() m.set(json,'me') m.set('name','fan') console.log(m.get('name'));//fan console.log(m.get(json));//me,根据对象搜索
Proxy本质是一个对象,当对对象的属性进行操做能够触发一系列操做
let data2=new Proxy({ add:function(val){ return val+1 }, name:'tom' },{ get:function(target,key,property){ console.log('get key--->',key)//name console.log('get target--->',target);//obj console.log('get property--->',property);//proxy obj return target[key] }, set:function(target,key,value,receiver){ console.log('set key--->',key);//name console.log('set value--->',value);//3333 console.log('set target--->',target);//obj console.log('set reeiver--->',receiver);//proxy obj return target[key]=value } }) data2.name=3333//执行set console.log(data2.name);//执行get
let target = ()=>{ return 'hello world' } let handler = { apply(target,ctx,args){ //不要加钩子 console.log('6666'); return Reflect.apply(...arguments)//Reflect代码demo自己 } } let demo=new Proxy(target,handler) demo()//6666
promise就是前一步完成了动做就执行下一步操做
let flag=200 function one(resolve,reject){ console.log("one"); if(flag==200){ resolve("step1 finish") }else{ reject("step1 erro") } } function two(resolve,reject){ console.log("two"); if(flag==200){ resolve("step2 finish") }else{ reject("step2 erro") } } function three(resolve,reject){ console.log("three"); if(flag==200){ resolve("step3 finish") }else{ reject("step3 erro") } } new Promise(one).then(function(val){ console.log(val); return new Promise(two) }).then(function(val){ console.log(val); return new Promise(three) }).then(function(val){ console.log(val); return val })
class people{ name(val){ // console.log(val); return val } fullname(val){ console.log('Mr'+this.name(val)) } add(){ return this.x+this.y } constructor(x,y){ this.x = x this.y = y } } let p = new people(1,2) console.log(p.add()); class student extends people{ hello(){ console.log("hello"); } } let s= new student(10,20) console.log(s.add());