12124 . [ D4: React Native 函数的绑定 (2016-8-23)] ( #d4react-native-函数的绑定-2016-8-23 )
13135 . [ D5: React Native setNativeProps使用 (2016-8-24)] ( #d5react-native-setnativeprops使用2016-8-24 )
14146 . [ D6: ref 属性不只是string(2016-8-25)] ( #d6ref属性不只是string2016-8-25 )
15+ 7 . [ D7:解构赋值(Destructuring assignment)(2016-8-26)] ( #d7解构赋值destructuring-assignment2016-8-26 )
1516
1617```
1718模板:
@@ -25,6 +26,95 @@ D1:标题 (日期)
2526另外:记得在列表中添加链接
2627```
2728
29+ D7:解构赋值([ Destructuring assignment] [ 0 ] )(2016-8-26)
30+ ------
31+ 解构赋值语法是JavaScript的一种表达式,可以方便的从数组或者对象中快速提取值赋给定义的变量。
32+
33+ ### 获取数组中的值
34+ 从数组中获取值并赋值到变量中,变量的顺序与数组中对象顺序对应。
35+
36+ ```
37+ var foo = ["one", "two", "three", "four"];
38+
39+ var [one, two, three] = foo;
40+ console.log(one); // "one"
41+ console.log(two); // "two"
42+ console.log(three); // "three"
43+
44+ //如果你要忽略某些值,你可以按照下面的写法获取你想要的值
45+ var [first, , , last] = foo;
46+ console.log(first); // "one"
47+ console.log(last); // "four"
48+
49+ //你也可以这样写
50+ var a, b; //先声明变量
51+
52+ [a, b] = [1, 2];
53+ console.log(a); // 1
54+ console.log(b); // 2
55+ ```
56+
57+ 如果没有从数组中的获取到值,你可以为变量设置一个默认值。
58+
59+ ```
60+ var a, b;
61+
62+ [a=5, b=7] = [1];
63+ console.log(a); // 1
64+ console.log(b); // 7
65+ ```
66+
67+ 通过解构赋值可以方便的交换两个变量的值。
68+
69+ ```
70+ var a = 1;
71+ var b = 3;
72+
73+ [a, b] = [b, a];
74+ console.log(a); // 3
75+ console.log(b); // 1
76+
77+ ```
78+
79+ ### 获取对象的值
80+ 从对象中获取对象属性的值,在声明变量的时候要与对象的属性名保持一致。
81+
82+ ```
83+ var o = {p: 42, q: true};
84+ var {p, q} = o;
85+
86+ console.log(p); // 42
87+ console.log(q); // true
88+
89+ //你也可以这样写
90+ var a, b;
91+
92+ ({a, b} = {a:1, b:2});
93+
94+ console.log(a); // 1
95+ console.log(b); // 2
96+ ```
97+
98+ 可以从一个对象中获取对象属性的值并赋值给与对象属性名不同的变量。
99+
100+ ```
101+ var o = {p: 42, q: true};
102+ var {p: foo, q: bar} = o;
103+
104+ console.log(foo); // 42
105+ console.log(bar); // true
106+ ```
107+ 和获取数组中的值一样,从对象中获取属性的值也可以设置一个默认值。
108+
109+ ```
110+ var {a=10, b=5} = {a: 3};
111+
112+ console.log(a); // 3
113+ console.log(b); // 5
114+ ```
115+
116+
117+
28118D6: ref 属性不只是string(2016-8-25)
29119----
30120
@@ -232,3 +322,8 @@ import langsData from '../../../res/data/langs.json'
232322@[ How to fetch data from local JSON file on react native?] ( http://stackoverflow.com/questions/29452822/how-to-fetch-data-from-local-json-file-on-react-native )
233323
234324
325+
326+
327+
328+ [ 0 ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment
329+
0 commit comments