解构表达式可以很方便赋值
1.数组结构
<script> let arr = [1, 2, 3, 4 ,5]; //采用之前的赋值方式, 我们可以a = arr[1], b = arr[2]..... 有了结构表达式就可以如下 let [a, b, c, d, e] = arr; console.log(a,b,c,d,e); </script>
2.对象结构
<script> const person = { name: 'zhanshen', age: 22, lanuage: ['python', 'java', 'go'] } const {name, age, lanuage} = person; console.log(name,age,lanuage); </script>
要是换名字, 可以用冒号
<script> const person = { name: 'zhanshen', age: 22, lanuage: ['python', 'java', 'go'] } const {name: a, age: b, lanuage: c} = person; console.log(a, b, c); </script>
3. 字符串模板
用反引号来表示长字符串
<script> let longStr = ` <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> ` console.log(longStr); </script>
用${}表示插值
<script> var name = '张三'; var age = 14; function say() { return "我是个坏学生!" } console.log(`我叫${name}, 我今年${age}岁了, 明年我${age + 1}岁, 我想说${say()}`) </script>
4. 其他方法
<script> let str = 'hello world'; console.log(str.startsWith('hello')); console.log(str.endsWith("world")); console.log(str.includes("h")); console.log(str.includes("ll")); </script>