24.ES9 Rest 参数和 spread 扩展运算符

 

Rest 参数和 spread 扩展运算符

Rest 参数与 spread 扩展运算符在 ES6 中已经引入,不过 ES6 中只针对于数组,在 ES9 中为对象提供了像数组一样的 rest 参数和扩展运算符

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ES9 Rest 参数和 spread 扩展运算符</title>
</head>

<body>
<!--
Rest 参数与 spread 扩展运算符在 ES6 中已经引入,不过 ES6 中只针对于数组,
在 ES9 中为对象提供了像数组一样的 rest 参数和扩展运算符
-->
<script>
// rest 参数
function connectionConfig({ip, port, ...info}) {
console.log(ip);
// 127.0.0.1
console.log(port);
// 3306
console.log(info);
// {username: 'root', password: '123456', type: 'jdbc'}
console.log(info.type);
// jdbc
}

connectionConfig({
ip: "127.0.0.1",
port: 3306,
username: "root",
password: "123456",
type: "jdbc"
});

// spread 扩展运算符
const host = {
ip: "127.0.0.1",
port: 3306
};
const user = {
username: "root",
password: "123456"
};
const database = {
type: "jdbc"
};

const dbConnectionInfo = {...host, ...user, ...database};
console.log(dbConnectionInfo);
// {ip: '127.0.0.1', port: 3306, username: 'root', password: '123456', type: 'jdbc'}

</script>

</body>

</html>