Blogger Information
Blog 35
fans 0
comment 0
visits 16750
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
对象与数组的解构赋值,访问器属性
手机用户311660634
Original
281 people have browsed it
  1. // 解构赋值
  2. // 1.数组
  3. let [uname, uage, usex] = ['刘备', 50, '男']
  4. // 更新,在{}[]()这些括号前不能省略分号;
  5. ;[uname, uage, usex] = ['关羽', 45, '男']
  6. // 2.值数量<变量数量,使用默认值
  7. ;[uname, uage, usex='男'] = ['关羽', 45]
  8. // 值数量>变量数量,使用...
  9. // 更新,在{}[]()这些括号前不能省略分号;
  10. ;[uname, ...ziliao] = ['关羽', 45, '男']
  11. // 2对象
  12. // 1.变量名 === 属性,绝对等于
  13. let {name, sex} = {name:'孙悟空', sex:'男'}
  14. // 更新,大括号不能出现在等号左边,得加上括号()
  15. ;({name, sex} = {name:'白骨精', sex:'女'})
  16. // 命名冲突:使用别名
  17. let {mname, sex:man} = {mname:'lin', sex:'man'}
  18. // 访问器属性
  19. let sjk = {
  20. sj:{
  21. name:'柯南',
  22. age:5,
  23. },
  24. // 读取接口
  25. getage(){
  26. return{
  27. name:this.sj.name,
  28. age:this.sj.age
  29. }
  30. },
  31. // 设置接口
  32. setage(age){
  33. if( age >= 0 && age<=130 ){
  34. this.sj.age = age
  35. } else{
  36. console.log('年龄必须是1到130之间')
  37. }
  38. }
  39. }
  40. // 访问器属性,普通方法===>访问器属性
  41. sjk = {
  42. sj:{
  43. name:'柯南',
  44. age:5,
  45. },
  46. // 使用访问器属性,get必须有,set可选
  47. get age(){
  48. return{
  49. name:this.sj.name,
  50. age:this.sj.age
  51. }
  52. },
  53. // 将一个方法伪装成属性
  54. set age(age){
  55. if( age >= 0 && age<=130 ){
  56. this.sj.age = age
  57. } else{
  58. console.log('年龄必须是1到130之间')
  59. }
  60. }
  61. }
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