ES6部分新特性總結

let 變量聲明

1.不存在變量提高數組

//使用var聲明
console.log(a);//undefined
var a = 1;
//使用let聲明
console.log(b);
let b = 2; //報錯

2.只在當前做用域有效數據結構

for(var i = 0;i<4;i++){}
i;//4
for(let n=0;n<4;n++){}
n;//報錯

3.暫時性死區
若是同一個做用域內已經聲明過變量了,
再聲明會報錯,let不容許重複聲明,並不會像var同樣覆蓋原來的。函數

const 聲明只讀變量

//不能夠重複聲明
const n = 1;
n = 2;//報錯
//const聲明的常量是不可修改的
let array = [1,2];
const static = array;
array.push(3);
static ;//1,2,3
//const聲明的若是是一個對象,對象自己能夠改變,但指向的地址不可改變。
即static指向array內存的指針不能夠修改,但指針指向的內存array能夠修改

對象的解構複製

數組解構ui

clipboard.png

對象解構this

let {name,age} = {name:'chan',age:23}
name //"chan"
age //23
//一樣屬性名賦值 xx = 'xj'
let {school:xx} = {school:'xj'}
xx //"xj" 這種方式是給xx對象school的屬性值

默認賦值es5

let {name='chan'} ={age:1}
//name若是沒有傳值的時候,等於chan
//若是傳值了let {name='chan'} ={name:'kc'} name=kc 
name
"chan"

//傳值的狀況下
let {age=30} = {age:20}
age
20

includes

查看某個字符or元素存不存在
字符串調用spa

let string = 'abcdefg'
// 判斷h存不存在
string.includes('h') //false
// 判斷a存不存在
string.includes('a') //true

數組調用
判斷某一個對象是否存在數組中prototype

let a = {name:'chan',age:10}
let b = {name:'louis',age:20}
let objArr = [a,b]
objArr.includes(a) //true
objArr.includes({name:'chan',ageL:10}) //false

Set數據結構

Set結構相似於數組,但全部成員的值都是惟一的。指針

const unique = new Set([1,2,3,4,4])
unique;//1,2,3,4
//生成set數據結構的對象時內部帶有===的檢驗方法
const unique = new Set([1,2,'2'])
unique;// 1,2,'2'
//添加成員
unique.add(3);
//獲取長度
unique.size;//4
//Array.from能夠將set結構轉換爲數組
let arr = Array.from(unique)
arr;//[1,2,'2',4]
//獲取鍵值,set結構沒有鍵,只有鍵值 keys(),values()用法相同
unique.keys()//返回value
//entries()返回鍵值和鍵名,鍵名和鍵值相同

class

class定義類和es5的構造函數相同,class Point 等同於 Point.prototype = {}
Point 必須由new 操做符來調用 不然會報錯
也能夠經過let fun = class Point{}來賦值,可是經過這樣的方法並不能在外部調用到Point
不存在變量提高 與es5 function functionName 有所不一樣code

class Point {
        //等同於es5的構造函數,this指向實例
        constructor(x,y){
          this.x = x;
          this.y = y;
        }
        //方法之間不須要用逗號隔開
        fn(){
          console.log('x is'+this.x+',y is '+ this.y)
        }
      }
      // typeof Point 'function'
      // Point === Point.prototype.constructor //true
      let dot = new Point(100,0);
      dot.fn()// x is 100,y is 0
相關文章
相關標籤/搜索