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

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

ECMAScript 6(ES6) 目前基本成爲業界標準,它的普及速度比 ES5 要快很多,主要原因是現代瀏覽器對 ES6 的支持相當迅速,尤其是 Chrome 和 Firefox 瀏覽器,已經支持 ES6 中絕大多數的特性。

 

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的是一個對象,對象所包含的值是可以被修改的。抽象一點兒說,就是對象所指向的地址沒有變就行:

const student = { name: 'cc' }

student.name = 'yy';// 不報錯
student  = { name: 'yy' };// 報錯
複製代碼

有幾個點需要注意:

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

2.模板字符串

在ES6之前,我們往往這麼處理模板字符串: 通過“\”和“+”來構建模板

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

而對ES6來說

  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 中,箭頭函數就是函數的一種簡寫形式,使用括號包裹參數,跟隨一個 =>,緊接着是函數體;

箭頭函數最直觀的三個特點。

  • 不需要 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...of 用於遍歷一個迭代器,如數組:

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

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!"
Student.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 關鍵詞

 轉載:https://juejin.im/post/5b1d1fd6f265da6e410e137c

記錄便於查找。

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