Blogger Information
Blog 21
fans 0
comment 0
visits 9986
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1103 实例演示对象与数组的解构赋值 2. 实例演示访问器属性,并描述与普通属性之间的区别与联系
放手去爱
Original
380 people have browsed it

对象与数组的解构赋值

  1. // ! 1. 数组
  2. let [uname, email] = ['杨过', '123@qq.com']
  3. console.log(uname, email)
  4. // 更新
  5. // 在 [],{},()之前的分号不能省略
  6. ;[uname, email] = ['小龙女', '1456@qq.com']
  7. console.log(uname, email)
  8. console.log('--------------')
  9. // 1. 值数量 < 变量数量, 默认值
  10. ;[uname, email, gender = '女'] = ['潘金莲', '789@qq.com']
  11. console.log(uname, email, gender)
  12. // 2. 值数量 > 变量数量, ...rest
  13. ;[uname, ...arr] = ['潘金莲', '789@qq.com', '女']
  14. console.log(uname, arr)
  15. console.log(uname, ...arr)
  16. console.log('--------------')
  17. //? 应用场景: 交接二个数
  18. let x = 10
  19. let y = 20
  20. console.log(`x = ${x}, y = ${y}`)
  21. ;[y, x] = [x, y]
  22. console.log(`x = ${x}, y = ${y}`)
  23. console.log('=================')
  24. // ! 2. 对象解构
  25. // ? 变量名 === 属性名
  26. let { num, price } = { num: 2, price: 5000 }
  27. console.log(num, price)
  28. // [],{},()之前分号不能省略
  29. // {}不能出现在等号左边(左值),所以加上括号()转成表达式
  30. ;({ num, price } = { num: 5, price: 6000 })
  31. console.log(num, price)
  32. // 命名冲突: 别名
  33. let { pname, price: uniPrice } = { pname: '手机', price: 8000 }
  34. // 访问用别名
  35. console.log(pname, uniPrice)
  36. console.log('-----------------')

访问器属性,并描述与普通属性之间的区别与联系

  1. let course = {
  2. lesson: {
  3. name: 'ES6',
  4. score: 60,
  5. },
  6. // 读取接口
  7. getScore() {
  8. return {
  9. name: this.lesson.name,
  10. course: this.lesson.score,
  11. }
  12. },
  13. // 设置接口
  14. setScore(score) {
  15. if (score >= 0 && score <= 100) {
  16. this.lesson.score = score
  17. } else {
  18. console.log('成绩必须在1-100分之间')
  19. }
  20. },
  21. }
  22. console.log(course.lesson.score)
  23. console.log(course.getScore())
  24. course.setScore(69)
  25. console.log(course.getScore())
  26. console.log('-------------------')
  27. // ? 用户习惯
  28. // console.log(course.score);
  29. // course.score = 90
  30. // ? 使用"访问器属性" 就可以实现
  31. course = {
  32. lesson: {
  33. name: 'ES6',
  34. score: 60,
  35. },
  36. score: 99,
  37. // getScore ===> get score
  38. // 普通方法 ===> 访问器属性
  39. // 如果使用访问器属性, get必须要有, set 可选
  40. get score() {
  41. return {
  42. name: this.lesson.name,
  43. course: this.lesson.score,
  44. }
  45. },
  46. // 将一个方法,伪装成"属性"
  47. set score(score) {
  48. if (score >= 0 && score <= 100) {
  49. this.lesson.score = score
  50. } else {
  51. console.log('成绩必须在1-100分之间')
  52. }
  53. },
  54. }
  55. // 经过包装和伪造, 现在就可以像访问属性一个访问一个方法了
  56. // course.score === course.get score()
  57. console.log(course.score)
  58. course.score = 50
  59. console.log(course.score)
  60. // ! 访问器属性 > 同名属性
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