153 lines
2.3 KiB
Markdown
153 lines
2.3 KiB
Markdown
|
# 解构赋值
|
||
|
|
||
|
## 选择
|
||
|
1. 下面代码执行后 a,b,c的值分别是
|
||
|
|
||
|
```
|
||
|
let [a, b, c] = [1, 2, 3];
|
||
|
|
||
|
```
|
||
|
A.1,2,3 B.2,3,1 C.未定义 D.undefined undefined undefined
|
||
|
|
||
|
答案 A
|
||
|
解析 在解构中,有下面两部分参与:
|
||
|
解构的源,解构赋值表达式的右边部分。
|
||
|
解构的目标,解构赋值表达式的左边部分。
|
||
|
|
||
|
2. 下面代码执行后 a,b,c的值分别是
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let [a, [[b], c]] = [4, [[5], 6]];
|
||
|
|
||
|
```
|
||
|
A.4 [5] 6 B.[4, [[5], 6]] undefined undefined C.4,5,6 D. 4,[[5], 6]
|
||
|
|
||
|
答案 C
|
||
|
|
||
|
3. 下面代码执行后 a,b的值分别是
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let [a, , b] = [1, 2, 3];
|
||
|
|
||
|
```
|
||
|
A.1,2 B.1,3 C.2,3 D.1,3
|
||
|
|
||
|
答案 D
|
||
|
|
||
|
4. 下面代码执行后 a,b的值分别是
|
||
|
CODE
|
||
|
|
||
|
```
|
||
|
let [a = 1, b] = []; // a = 1, b = undefined
|
||
|
```
|
||
|
|
||
|
A.1 undefined B. 1 报错 C.[] D. undefined undefined
|
||
|
|
||
|
答案 A
|
||
|
|
||
|
5. 下面代码执行后 a b的值分别是
|
||
|
code
|
||
|
```
|
||
|
let [a, ...b] = [1, 2, 3];
|
||
|
|
||
|
```
|
||
|
|
||
|
A. 1; 2,3 B. 1;[1,2,3] C.1,[2,3] D.1,[]
|
||
|
|
||
|
答案 C
|
||
|
|
||
|
6. 下面代码执行后 a,b,c,e,d的值分别是
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let [a, b, c, d, e] = 'hello';
|
||
|
|
||
|
```
|
||
|
A h,e,l,l,o B.h,e,l,o,l C. 所有的都是undefined D. 所有的都是空数组
|
||
|
|
||
|
答案 B
|
||
|
|
||
|
7. 下面代码执行后 a的值为
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let [a = 2] = [undefined]; // a = 2
|
||
|
|
||
|
```
|
||
|
A.2 B.undefined C.[] D.报错
|
||
|
|
||
|
答案 A
|
||
|
|
||
|
8. 下面代码执行后foo,bar 的值分别是
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let { foo, bar } = { foo: 'aaa', bar: 'bbb' };
|
||
|
|
||
|
```
|
||
|
A. foo bar B.aaa,bbb C.foo bbb D.aaa bar
|
||
|
|
||
|
答案 B
|
||
|
|
||
|
9. 下面代码执行后foo 的值为
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let { baz : foo } = { baz : 'ddd' };
|
||
|
|
||
|
```
|
||
|
A. baz B.foo C.ddd D.undefiend
|
||
|
|
||
|
答案 C
|
||
|
|
||
|
10. 下面代码执行后X 的值为
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let obj = {p: ['hello', {y: 'world'}] };
|
||
|
let {p: [x, { }] } = obj;
|
||
|
|
||
|
```
|
||
|
|
||
|
A.hello B.world C.p D.obj
|
||
|
|
||
|
答案 A
|
||
|
|
||
|
11. 下面代码执行后x,y 的值分别为
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let obj = {p: [{y: 'world'}] };
|
||
|
let {p: [{ y }, x ] } = obj
|
||
|
|
||
|
```
|
||
|
|
||
|
A.undefined undefined B. obj world C. 报错 obj D.undefined world
|
||
|
|
||
|
答案 D
|
||
|
|
||
|
|
||
|
12. 下面代码执行后a,b,rest 的值分别为
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let {a, b, ...rest} = {a: 10, b: 20, c: 30, d: 40};
|
||
|
|
||
|
```
|
||
|
|
||
|
A. 10 20 30,40 B.10 20 [30,40] C. 10 20 [] D.undefined undefined []
|
||
|
|
||
|
答案 B
|
||
|
|
||
|
|
||
|
13. 下面代码执行后aa,bb 的值分别为
|
||
|
code
|
||
|
|
||
|
```
|
||
|
let {a: aa = 10, b: bb = 5} = {a: 3};
|
||
|
|
||
|
```
|
||
|
|
||
|
A. 3 5 B.10 5 C. 3 undefined D. 报错
|