ES6中經常使用的10個新特性講解

ECMAScript 6(ES6) 目前基本成爲業界標準,它的普及速度比 ES5 要快不少,主要緣由是現代瀏覽器對 ES6 的支持至關迅速,尤爲是 Chrome 和 Firefox 瀏覽器,已經支持 ES6 中絕大多數的特性。html

 

image

 

下面逐一爲你們詳解經常使用的ES6新特性:數組

1.不同的變量聲明:const和let

ES6推薦使用let聲明局部變量,相比以前的var(不管聲明在何處,都會被視爲聲明在函數的最頂部) let和var聲明的區別:瀏覽器

var x = '全局變量';
{
  let x = '局部變量';
  console.log(x); // 局部變量
}
console.log(x); // 全局變量

let表示聲明變量,而const表示聲明常量,二者都爲塊級做用域;const 聲明的變量都會被認爲是常量,意思就是它的值被設置完成後就不能再修改了:函數

const a = 1
a = 0 //報錯

若是const的是一個對象,對象所包含的值是能夠被修改的。抽象一點兒說,就是對象所指向的地址沒有變就行:post

const student = { name: 'cc' }

student.name = 'yy';// 不報錯
student  = { name: 'yy' };// 報錯

有幾個點須要注意:this

  • let 關鍵詞聲明的變量不具有變量提高(hoisting)特性
  • let 和 const 聲明只在最靠近的一個塊中(花括號內)有效
  • 當使用常量 const 聲明時,請使用大寫變量,如:CAPITAL_CASING
  • const 在聲明時必須被賦值

2.模板字符串

在ES6以前,咱們每每這麼處理模板字符串: 經過「\」和「+」來構建模板spa

$("body").html("This demonstrates the output of HTML \
content to the page, including student's\
" + name + ", " + seatNumber + ", " + sex + " and so on.");

而對ES6來講code

  1. 基本的字符串格式化。將表達式嵌入字符串中進行拼接。用${}來界定;
  2. ES6反引號(``)直接搞定;
$("body").html(`This demonstrates the output of HTML content to the page, 
including student's ${name}, ${seatNumber}, ${sex} and so on.`);

3.箭頭函數(Arrow Functions)

ES6 中,箭頭函數就是函數的一種簡寫形式,使用括號包裹參數,跟隨一個 =>,緊接着是函數體;htm

箭頭函數最直觀的三個特色。對象

  • 不須要 function 關鍵字來建立函數
  • 省略 return 關鍵字
  • 繼承當前上下文的 this 關鍵字
// ES5
var add = function (a, b) {
    return a + b;
};
// 使用箭頭函數
var add = (a, b) => a + b;

// ES5
[1,2,3].map((function(x){
    return x + 1;
}).bind(this));
    
// 使用箭頭函數
[1,2,3].map(x => x + 1);

細節:當你的函數有且僅有一個參數的時候,是能夠省略掉括號的。當你函數返回有且僅有一個表達式的時候能夠省略{} 和 return;

4. 函數的參數默認值

在ES6以前,咱們每每這樣定義參數的默認值:

// ES6以前,當未傳入參數時,text = 'default';
function printText(text) {
    text = text || 'default';
    console.log(text);
}

// ES6;
function printText(text = 'default') {
    console.log(text);
}

printText('hello'); // hello
printText();// default

5.Spread / Rest 操做符

Spread / Rest 操做符指的是 ...,具體是 Spread 仍是 Rest 須要看上下文語境。

當被用於迭代器中時,它是一個 Spread 操做符:

function foo(x,y,z) {
  console.log(x,y,z);
}
 
let arr = [1,2,3];
foo(...arr); // 1 2 3

當被用於函數傳參時,是一個 Rest 操做符:當被用於函數傳參時,是一個 Rest 操做符:

function foo(...args) {
  console.log(args);
}
foo( 1, 2, 3, 4, 5); // [1, 2, 3, 4, 5]

6.二進制和八進制字面量

ES6 支持二進制和八進制的字面量,經過在數字前面添加 0o 或者0O 便可將其轉換爲八進制值:

let oValue = 0o10;
console.log(oValue); // 8
 
let bValue = 0b10; // 二進制使用 `0b` 或者 `0B`
console.log(bValue); // 2

7.對象和數組解構

// 對象
const student = {
    name: 'Sam',
    age: 22,
    sex: '男'
}
// 數組
// const student = ['Sam', 22, '男'];

// ES5;
const name = student.name;
const age = student.age;
const sex = student.sex;
console.log(name + ' --- ' + age + ' --- ' + sex);

// ES6
const { name, age, sex } = student;
console.log(name + ' --- ' + age + ' --- ' + sex);

8.對象超類

ES6 容許在對象中使用 super 方法:

var parent = {
  foo() {
    console.log("Hello from the Parent");
  }
}
 
var child = {
  foo() {
    super.foo();
    console.log("Hello from the Child");
  }
}
 
Object.setPrototypeOf(child, parent);
child.foo(); // Hello from the Parent
             // Hello from the Child

9.for...of 和 for...in

for...of 用於遍歷一個迭代器,如數組:

let letter = ['a', 'b', 'c'];
letter.size = 3;
for (let letter of letters) {
  console.log(letter);
}
// 結果: a, b, c

for...in 用來遍歷對象中的屬性:

let stu = ['Sam', '22', '男'];
stu.size = 3;
for (let stu in stus) {
  console.log(stu);
}
// 結果: Sam, 22, 男

10.ES6中的類

ES6 中支持 class 語法,不過,ES6的class不是新的對象繼承模型,它只是原型鏈的語法糖表現形式。

函數中使用 static 關鍵詞定義構造函數的的方法和屬性:

class Student {
  constructor() {
    console.log("I'm a student.");
  }
 
  study() {
    console.log('study!');
  }
 
  static read() {
    console.log("Reading Now.");
  }
}
 
console.log(typeof Student); // function
let stu = new Student(); // "I'm a student."
stu.study(); // "study!"
stu.read(); // "Reading Now."

類中的繼承和超集:

class Phone {
  constructor() {
    console.log("I'm a phone.");
  }
}
 
class MI extends Phone {
  constructor() {
    super();
    console.log("I'm a phone designed by xiaomi");
  }
}
 
let mi8 = new MI();

extends 容許一個子類繼承父類,須要注意的是,子類的constructor 函數中須要執行 super() 函數。 固然,你也能夠在子類方法中調用父類的方法,如super.parentMethodName()。 在 這裏 閱讀更多關於類的介紹。

有幾點值得注意的是:

  • 類的聲明不會提高(hoisting),若是你要使用某個 Class,那你必須在使用以前定義它,不然會拋出一個 ReferenceError 的錯誤
  • 在類中定義函數不須要使用 function 關鍵詞

做者:木亦Sam 連接:https://juejin.im/post/5b1d1fd6f265da6e410e137c 來源:掘金 著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。

相關文章
相關標籤/搜索