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


下面逐一為大家詳解常用的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 操作符:

function foo(...args) {
  console.log(args);
}
foo( 1, 2, 3, 4, 5); // [1, 2, 3, 4, 5]
class MyArray extends Array {
    constructor(...args) { super(...args); }//構造函數的是rest,super的是spread
}

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 letters = ['a', 'b', 'c'];
letters.size = 3;
for (let letter of letters) {
  console.log(letter);
}
// 結果: a, b, c

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

 let stus = ["Sam", "22", "男"];
 for (let stu in stus) {
   console.log(stus[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!"
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://www.jianshu.com/p/ac1787f6c50f


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM