Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. ## Array destructuring
  2.  
  3. ### Basic variable assignment
  4.  
  5. ```js
  6. const foo = ["one", "two", "three"]
  7.  
  8. const [one, two, three] = foo
  9. console.log(one) // "one"
  10. console.log(three) // "three"
  11. ```
  12.  
  13. ### Assignment separate from declaraion
  14.  
  15. ```js
  16. let a, b
  17.  
  18. [a, b] = [1, 2]
  19. console.log(b) // 2
  20. ```
  21.  
  22. ### Default values
  23.  
  24. ```js
  25. let a, b
  26.  
  27. [a=5, b=7] = [1]
  28. console.log(a) // 1
  29. console.log(b) // 7
  30. ```
  31.  
  32. ### Swapping variables
  33.  
  34. ```js
  35. let a = 1
  36. let b = 3
  37.  
  38. [a, b] = [b, a]
  39. ```
  40.  
  41. ### Parsing an array returned by a function
  42.  
  43. ```js
  44. function f() {
  45. return [1, 2]
  46. }
  47.  
  48. let a, b
  49. [a, b] = f()
  50. console.log(a) // 1
  51. console.log(b) // 2
  52. ```
  53.  
  54. ### Ignoring some returned values
  55.  
  56. ```js
  57. function f() {
  58. return [1, 2, 3]
  59. }
  60.  
  61. const [a, , b] = f()
  62. console.log(a) // 1
  63. console.log(b) // 3
  64. ```
  65.  
  66. ### Assigning the rest of an array to a variable
  67.  
  68. ```js
  69. const [a, ...b] = [1, 2, 3]
  70. console.log(a) // 1
  71. console.log(b) // [2, 3]
  72. ```
  73.  
  74. ## Object destructuring
  75.  
  76. ### Basic assignment
  77.  
  78. ```js
  79. const o = { p: 42, q: true }
  80. const { p, q } = o
  81.  
  82. console.log(p) // 42
  83. console.log(q) // true
  84. ```
  85.  
  86. ### Assignment without declaration
  87.  
  88. ```js
  89. let a, b
  90.  
  91. ({ a, b } = { a: 1, b: 2 })
  92. ```
  93.  
  94. ### Assigning to new variable names
  95.  
  96. ```js
  97. const o = { p: 42, q: true }
  98. const { p: foo, q: bar } = o
  99.  
  100. console.log(foo) // 42
  101. console.log(bar) // true
  102. ```
  103.  
  104. ### Default values
  105.  
  106. ```js
  107. let { a=10, b=5 } = {a: 3}
  108.  
  109. console.log(a) // 3
  110. console.log(b) // 5
  111. ```
  112.  
  113. ### Setting a function paramenter's default value
  114.  
  115. ```js
  116. function drawES6Chart({ size = 'big', cords = { x: 0, y: 0 }, radius = 25 } = {}) {
  117. console.log(size, cords, radius)
  118. }
  119.  
  120. drawES6Chart({
  121. cords: { x: 18, y: 30 },
  122. radius: 30
  123. })
  124. ```
  125.  
  126. ### Nested object and array destructuring
  127.  
  128. ```js
  129. const metadata = {
  130. title: 'Scratchpad',
  131. translations: [{
  132. locale: 'de',
  133. localization_tags: [],
  134. last_edit: '2014-04-14T08:43:37',
  135. url: '/de/docs/Tools/Scratchpad',
  136. title: 'JavaScript-Umgebung'
  137. }],
  138. url: '/en-US/docs/Tools/Scratchpad'
  139. }
  140.  
  141. const { title: englishTitle, translations: [{ title: localeTitle }] } = metadata
  142.  
  143. console.log(englishTitle) // "Scratchpad"
  144. console.log(localeTitle) // "JavaScript-Umgebung"
  145. ```
  146.  
  147. ### For of iteration and destructuring
  148.  
  149. ```js
  150. const people = [{
  151. name: "Mike Smith",
  152. family: {
  153. mother: "Jane Smith",
  154. father: "Harry Smith",
  155. sister: "Samantha Smith"
  156. },
  157. age: 35
  158. }, {
  159. name: "Tom Jones",
  160. family: {
  161. mother: "Norah Jones",
  162. father: "Richard Jones",
  163. brother: "Howard Jones"
  164. },
  165. age: 25
  166. }]
  167.  
  168. for (const { name: n, family: { father: f } } of people) {
  169. console.log(`Name: ${n}, Father: ${f}`)
  170. }
  171.  
  172. // "Name: Mike Smith, Father: Harry Smith"
  173. // "Name: Tom Jones, Father: Richard Jones"
  174. ```
  175.  
  176. ### Pulling fields from objects passed as function parameter
  177.  
  178. ```js
  179. function userId({ id }) {
  180. return id
  181. }
  182.  
  183. function whois({ displayName, fullName: { firstName: name } }) {
  184. console.log(`${displayName} is ${name}`)
  185. }
  186.  
  187. const user = {
  188. id: 42,
  189. displayName: 'jdoe'
  190. fullName: {
  191. firstName: 'John',
  192. lastName: 'Doe'
  193. }
  194. }
  195.  
  196. console.log(`userId: ${userId(user)}`) // "userId: 42"
  197. whois(user) // "jdoe is John"
  198. ```
  199.  
  200. ### Computed object property names and destructuring
  201.  
  202. ```js
  203. let key = 'z'
  204. let { [key]: foo } = { z: 'bar' }
  205.  
  206. console.log(foo)
  207. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement