3.解构赋值.html 951 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // 1.数组
  11. var a = [1,2,3];
  12. // 解构赋值
  13. let [w,e,r] = a;
  14. console.log(w,e,r)
  15. console.log(w)
  16. // 2.对象
  17. var obj = {
  18. a: 1,
  19. b: 2,
  20. c: function() {
  21. console.log("111")
  22. }
  23. }
  24. obj.c();
  25. var {a,b,c} = obj;
  26. console.log(a,b,c)
  27. // 3.字符串
  28. let str = 'hello';
  29. let [z,x,aa,v,bbb] = str;
  30. console.log(z,x,aa,v,bbb)
  31. // 4.函数
  32. function fn1() {
  33. return {
  34. name: '孙悟空',
  35. age: 18
  36. }
  37. }
  38. let {name,age} = fn1();
  39. console.log(name,age)
  40. </script>
  41. </body>
  42. </html>