該文章是初次涉獵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());