vue之ES6基本語法(下)

 

1.數組得解構賦值

1)“模式匹配”爲變量賦值;var [a, b, c] = [1, 2, 3];

2) 不完全解構,即等號左邊的模式,只匹配一部分的等號右邊的數組。

1

2

3

4

let [a, [b], d] = [1, [2, 3], 4];

// 1

// 2

// 4

3)解構賦值不僅適用於var命令,也適用於let和const命令,對於Set結構,也可以使用數組的解構賦值。只要某種數據結構具有Iterator接口,都可以採用數組形式的解構賦值。

2.對象的解構賦值

1)對象的解構與數組有一個重要的不同。數組的元素是按次序排列的,變量的取值由它的位置決定;而對象的屬性沒有次序,變量必須與屬性同名,才能取到正確的值。

1

2

3

4

5

6

var { bar, foo } = { foo: "aaa", bar: "bbb" };

foo // "aaa"

bar // "bbb"

 

var { baz } = { foo: "aaa", bar: "bbb" };

baz // undefined

2)變量名與屬性名不一致,必須寫成下面這樣。這實際上說明,對象的解構賦值是下面形式的簡寫。也就是說,對象的解構賦值的內部機制,是先找到同名屬性,然後再賦給對應的變量。真正被賦值的是後者,而不是前者。

1

2

3

4

5

6

7

8

9

//對象的解構賦值的內部機制,是先找到同名屬性,然後再賦給對應的變量。<br>var { foo: foo, bar: bar } = { foo: "aaa", bar: "bbb" };

 

var { foo: baz } = { foo: 'aaa', bar: 'bbb' };

baz // "aaa"

 

let obj = { first: 'hello', last: 'world' };

let { first: f, last: l } = obj;

// 'hello'

// 'world'

3)和數組一樣,解構也可以用於嵌套結構的對象。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

var obj = {

  p: [

    'Hello',

    { y: 'World' }

  ]

};

 

var { p: [x, { y }] } = obj;

// "Hello"

// "World"

//這時p是模式,不是變量,因此不會被賦值

var node = {

  loc: {

    start: {

      line: 1,

      column: 5

    }

  }

};

 

var { loc: { start: { line }} } = node;

line // 1

loc  // error: loc is undefined

start // error: start is undefined

 

 

let obj = {};

let arr = [];

 

({ foo: obj.prop, bar: arr[0] } = { foo: 123, bar: true });

 

obj // {prop:123}

arr // [true]

4)數組本質是特殊的對象,因此可以對數組進行對象屬性的解構

1

2

3

4

var arr = [1, 2, 3];

var {0 : first, [arr.length - 1] : last} = arr;

first // 1

last // 3

3.字符串的解構賦值

1)字符串也可以解構賦值。這是因爲此時,字符串被轉換成了一個類似數組的對象。

2)類似數組的對象都有一個length屬性,因此還可以對這個屬性解構賦值。

1

2

3

4

5

6

7

8

9

const [a, b, c, d, e] = 'hello';

// "h"

// "e"

// "l"

// "l"

// "o"

 

let {length : len} = 'hello';

len // 5

4.數值和布爾值的解構賦值

解構賦值的規則是,只要等號右邊的值不是對象,就先將其轉爲對象。由於undefinednull無法轉爲對象,所以對它們進行解構賦值,都會報錯。

1

2

3

4

5

6

7

8

let {toString: s} = 123;

s === Number.prototype.toString // true

 

let {toString: s} = true;

s === Boolean.prototype.toString // true

 

let { prop: x } = undefined; // TypeError

let { prop: y } = null// TypeError

5.變量得解構賦值用途

1)交換變量的值。 

1

[x,y]=[y,x];

2)從函數返回多個值

3)遍歷map結構

任何部署了Iterator接口的對象,都可以用for...of循環遍歷。Map結構原生支持Iterator接口,配合變量的解構賦值,獲取鍵名和鍵值就非常方便。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

var map = new Map();

map.set('first''hello');

map.set('second''world');

 

for (let [key, value] of map) {

  console.log(key + " is " + value);

}

// first is hello

// second is world

 

// 獲取鍵名

for (let [key] of map) {

  // ...

}

 

// 獲取鍵值

for (let [,value] of map) {

  // ...

}

4)輸入模塊的指定方法

加載模塊時,往往需要指定輸入那些方法。解構賦值使得輸入語句非常清晰。

1

const { SourceMapConsumer, SourceNode } = require("source-map");

最常用的ES6特性

let,

const,

上一篇已經講過了

class,

extends,

super,

這三個特性涉及了ES5中最令人頭疼的的幾個部分:原型、構造函數,繼承…你還在爲它們複雜難懂的語法而煩惱嗎?你還在爲指針到底指向哪裏而糾結萬分嗎?

有了ES6我們不再煩惱!

ES6提供了更接近傳統語言的寫法,引入了Class(類)這個概念。新的class寫法讓對象原型的寫法更加清晰、更像面向對象編程的語法,也更加通俗易懂。

class Animal {

   constructor(){

       this.type = 'animal'

   }

   says(say){

       console.log(this.type + ' says ' + say)

   }

}

 

let animal = new Animal()

animal.says('hello') //animal says hello

 

class Cat extends Animal {

   constructor(){

       super()

       this.type = 'cat'

   }

}

 

let cat = new Cat()

cat.says('hello') //cat says hello

上面代碼首先用class定義了一個“類”,可以看到裏面有一個constructor方法,這就是構造方法,而this關鍵字則代表實例對象。簡單地說,constructor內定義的方法和屬性是實例對象自己的,而constructor外定義的方法和屬性則是所有實例對象可以共享的。

Class之間可以通過extends關鍵字實現繼承,這比ES5的通過修改原型鏈實現繼承,要清晰和方便很多。上面定義了一個Cat類,該類通過extends關鍵字,繼承了Animal類的所有屬性和方法。

super關鍵字,它指代父類的實例(即父類的this對象)。子類必須在constructor方法中調用super方法,否則新建實例時會報錯。這是因爲子類沒有自己的this對象,而是繼承父類的this對象,然後對其進行加工。如果不調用super方法,子類就得不到this對象。

ES6的繼承機制,實質是先創造父類的實例對象this(所以必須先調用super方法),然後再用子類的構造函數修改this。

P.S 如果你寫react的話,就會發現以上三個東西在最新版React中出現得很多。創建的每個component都是一個繼承React.Component的類。詳見react文檔

 

arrow functions,箭頭函數 上一篇也講過了

template string, 用反引號``拼接字符串上一篇也講過了

destructuring, 解構賦值 

default,

rest arguments 

 

default很簡單,意思就是默認值。大家可以看下面的例子,調用animal()方法時忘了傳參數,傳統的做法就是加上這一句type = type || ‘cat’ 來指定默認值。

function animal(type){

   type = type || 'cat'  

   console.log(type)

}

animal()

如果用ES6我們而已直接這麼寫:

function animal(type = 'cat'){

   console.log(type)

}

animal()

最後一個rest語法也很簡單,直接看例子:

function animals(...types){

   console.log(types)

}

animals('cat', 'dog', 'fish') //["cat", "dog", "fish"]

而如果不用ES6的話,我們則得使用ES5的arguments。

修改變量名

此時我們不喜歡type這個變量名,因爲它有可能重名,所以我們需要修改一下它的變量名。在es6中可以用as實現一鍵換名。

//index.js

 

import animal, { say, type as animalType } from './content'  

let says = say()

console.log(`The ${animalType} says ${says} to ${animal}`)  

//The dog says Hello to A cat

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章