JavaScript-ES6-function-partII

JavaScript-ES6-function-partII

這篇簡介JavaScript-ES6-function-partII。

ObjectDestructuration Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function fetch(url, { body = '', method = 'GET', headers = {} }) {
console.log(method);
}

fetch('http://example.com', {})
// "GET"

fetch('http://example.com')
// error

此例第二個參數是object,可以為三個屬性設置default value
但這種用法不能省略第二個參數
---------
欲省略第二個參數的作法如下:
function fetch(url, { body = '', method = 'GET', headers = {} } = {}) {
console.log(method);
}

fetch('http://example.com')
// "GET"