Blogger Information
Blog 37
fans 0
comment 1
visits 28505
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
JS循环,函数返回值与解构赋值
kong
Original
644 people have browsed it

JS基本语法 - 4

循环

  1. /** 循环三要素
  2. * 1. 初始化: 入口
  3. * 2. 条件: true开始,false终止
  4. * 3. 更新条件: 避免死循环
  5. **/
  6. // 1. while
  7. let arr = [10 ,'admin' ,true]
  8. // (1) 初始化
  9. let index = 0;
  10. // (2) 判断条件
  11. while(index < arr.length){
  12. console.log(arr[index]) // 10 ,admin ,true
  13. // (3) 更新条件
  14. index++
  15. }
  16. // 2. for
  17. // while的语法糖
  18. let arr = [10 ,'admin' ,true]
  19. // for(初始化;条件;条件更新;)
  20. for(let index = 0; index<arr.length;index++){
  21. console.log(arr[index])
  22. }
  23. // 3. for-of
  24. //遍历数组
  25. let arr = [10 ,'admin' ,true]
  26. // 数组循环 只关注值 忽略引索
  27. for(let vale of arr){
  28. // vale循环遍量用于每一次遍历的接收值
  29. console.log(vale)
  30. }
  31. // 4. for-in
  32. //遍历对象
  33. let obj = {
  34. item: '1',
  35. id: 2,
  36. name: 'admin'
  37. }
  38. for(let item in obj){
  39. console.table(obj[item])
  40. }
  41. // 5. forEach / map
  42. //遍历数组
  43. //这是认定在数组对象上的接口
  44. // 接口:函数或者方法
  45. // forEach,map:参数相同,仅仅是返回值不同
  46. // forEach无返回值,map有返回值
  47. let arr = [1,2,3,4,5,6]
  48. arr.forEach(function(值,索引,数组)){}
  49. arr.forEach(function(item,index,arr){
  50. console.log(item)
  51. })
  52. //简写
  53. arr.forEach(item => console.log(item))
  54. // map()有返回值
  55. let res = arr.map(item => item)
  56. console.log(res)

数组与对象的解构与赋值

  1. //语法
  2. // 左边: 模板,数组用 [...] 对象用 {...}
  3. // 右边: 值(数组, 对象)
  4. // 1. 数组
  5. // 创建
  6. let [uname, email] = ['php中文网','php@qq.com']
  7. console.log(uname,email);
  8. // 更新
  9. [uname, email] = ['墨子', 'mz@qq.com']
  10. console.log(uname, email);
  11. // 变量大于值: 默认值
  12. [uname, email ,gender = '女'] = ['php中文网','php@qq.com']
  13. console.log(uname, email, gender)
  14. // 变量小于值: 剩余参数 ...rest
  15. ;[uname, email, ...arr] = ['墨子','mz@qq.com','男',60]
  16. let [sex,age] = arr
  17. console.log(uname, email, sex, age)
  18. // 2. 对象解构
  19. // 默认变量名和属性相同
  20. // tips: 变量与当前作用域中的变量命名冲突时,用别名访问
  21. let {id ,uname:username} = {id: 1, uname: 'php'}
  22. console.log(id ,username)
  23. // 应用场景1: 克隆对象
  24. let user = {uname: 'admin',email:'php@qq.com'}
  25. console.log(user)
  26. let {...obj} = user
  27. console.log(obj);
  28. // 2. 应用场景2: 解构传参
  29. let show = function(user){
  30. return `${user.uname}:(${user.email})`
  31. }
  32. user = {uname:'墨子', email: 'php@qq.com'}
  33. console.log(show(user))
  34. //使用对象解构简化传参
  35. show = function({uname,email}){
  36. return `${uname}:(${email})`
  37. }
  38. console.log(show(user))

总结

循环

  1. while()
  2. for()
  3. for-of
  4. for-in
  5. forEach() / map()

解构赋值

语法
左边: 模板,数组用 […] 对象用 {…}
右边: 值(数组, 对象)

Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post