ES6(ES2015)是如何演變併為現代 JavaScript 帶來了新功能的?


在本文中,您將瞭解 ES6(ES2015)是如何演變併為現代 JavaScript 帶來了新功能的。

ES6 代表 ECMAScript 6。它是 ECMAScript 的第 6 個版本,旨在標準化 JavaScript。ES6 的十大功能包括:let 和 const 關鍵字、箭頭函式、多行字串、預設引數、模板字面量、解構賦值、增強物件字面量、Promise。

示例 1

在這個示例中,讓我們演示箭頭函式(=>)−

console.log("An Arrow function Square has been defined")

square = (x) => { return x * x; }
 
let inputValue = 6
console.log("The input value is defined as :", inputValue)

let result= square(inputValue)
console.log("
The square of the given value is :", result)

解釋

  • 步驟 1 − 定義一個名為 'square' 的箭頭函式,它接受一個數字作為引數並返回該數字的平方。

  • 步驟 2 − 呼叫函式並將函式呼叫賦值給一個值:'result';

  • 步驟 3 − 顯示結果。

示例 2

console.log("A class Rectangle is defined that uses constructor ",)
class Rectangle { 
   constructor(x, y) {
      this.x = x;
      this.y = y;
   }
}
let object = new Rectangle(10, 20);
console.log("A sides of the rectangle are defined as ")
console.log(object.x, " and ",object.y)

解釋

  • 步驟 1 − 定義一個名為 'Rectangle' 的類,它接收兩個數字作為引數。該類使用建構函式來賦值兩個值。

  • 步驟 2 − 使用物件呼叫類。

  • 步驟 3 − 顯示值。

更新於: 2023年2月16日

96 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告