之前學 ES6 時整理的筆記😀
在開始之前
有位網友 Giana 寄信推薦我一款網頁工具:JS & CSS Unminifier
This tool does the job: it unminifies, reformats and reforms JavaScript (and CSS) code, making it readable again. (It’s very user-friendly and free! )
之所以推薦我這個,是因為她在找 unminify 工具的過程當中找到了我這篇文章,她在信中很樂於分享地說:
I’m sure your readers will appreciate it, especially if they’ve struggled like me.
所以說,怎能不分享個呢😎(謝謝 Giana!)
參考自:[ES6] Javascript 開發者必須知道的 10 個新功能
Default Parameters
MDN:
Default function parameters allow named parameters to be initialized with default values if no value or undefined
is passed.
1 | let person = function(height = 173, weight = 66, college = 'NCTU') { ... }; |
Template Literals
MDN:
Template literals are string literals allowing embedded expressions. You can use multi-line strings and string interpolation features with them.
Multi-line Strings
1 | // ES5 |
String Interpolation
1 | // ES5 |
Destructuring Assignment
MDN:
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
1 | let me = { name: 'Tim', college: 'NCTU' }; |
Array destructuring
1 | const foo = ['one', 'two', 'three']; |
Object destructuring
1 | const me = { name: 'Tim', college: 'NCTU' }; |
Object Literals
[筆記] JavaScript ES6 中的物件的擴展(object literal extension)
1 | function makeMachine(name, year, price) { |
Arrow Functions
MDN:
An arrow function expression is a compact alternative to a traditional function expression, but is limited and can’t be used in all situations.
1 | // 多個參數 |
Promises
Promise 學習筆記 | DEVLOG of andyyou
MDN:
The Promise
object represents the eventual completion (or failure) of an asynchronous operation and its resulting value.
建⽴ Promise
把 Promise
想成是⼀個 object!
1 | hasEnoughMoney = false; |
使⽤ Promise
1 | let askJoeman = function() { |
簡化寫法
若某個 function 沒呼叫 reject
1 | const showoff = function(something) { |
可簡寫成
1 | const showoff = function(something) { |
ES7 async/await ⾮同步寫法
原本的寫法
1 | let askJoeman = function() { |
新寫法
1 | async function askJoeman() { |
cleaner and more readable!
Let & Const, Block-Scoped
let
僅在當前的 block 內有效,重複定義時會 throw error。const
同樣只在當前的 block 內有效,定義時必須 initialize,且不能更改。
Classes
MDN:
Classes are a template for creating objects. They encapsulate data with code to work on that data.
Class Declaration 類別宣告
1 | class People { |
Class Expression 類別敘述
1 | // unnamed |
使用方式
1 | let PG = new People('Curry', 32); |
Prototype Methods 原型⽅法
可包 getter、setter、method 等等的進到 class 定義裡⾯,詳⾒ MDN。
Static methods 靜態⽅法
不需要實體化就可以被呼叫,也無法被已實體化的類別物件呼叫。
經常被⽤來建⽴給應⽤程式使⽤的⼯具函數。
1 | class People { |
⽤ extends 建⽴⼦類別 & ⽤ super 呼叫⽗類別
1 | class Player { |
Module
MDN:
providing mechanisms for splitting JavaScript programs up into separate modules that can be imported when needed.
Export
MDN:
The export
statement is used when creating JavaScript modules to export live bindings to functions, objects, or primitive values from the module so they can be used by other programs with the import
statement.
1 | export password = 'hello'; |
Import
MDN:
The static import
statement is used to import read only live bindings which are exported by another module.
1 | import { password, getStatus } from 'module-name'; |
Export Default
可以讓要使⽤ module 的 client,在不知道 module export 了哪些名稱的情況下,能⾃訂名稱取⽤ module 裡⾯定義的東西。
1 | export default { |
希望讀完這篇文章的您能夠有所收穫,我們下篇文見啦 😃