Blogger Information
Blog 19
fans 0
comment 0
visits 10757
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组与对象的解构赋值与访问器属性和普通属性之间的区别与联系
牙森江
Original
539 people have browsed it

1.数组与对象的解构赋值


1.1.数组的解构赋值

  1. 1. 值数量 < 变量数量, 默认值
  2. ;[uname, phone, gender ] = ['Mr.Y', '18095411***','男']
  3. console.log(uname, phone, gender)
  4. 代码运行结果:Mr.Y 18095411***
  5. 2. 值数量 > 变量数量, ...rest
  6. ;[uname, ...arr] = ['M','18095','男']
  7. console.log(uname,...arr)
  8. 代码运行结果:M 18095

1.2.对象的解构赋值

  1. let { num, goods } = { num: 2, goods: 3000 }
  2. console.log(num, goods)
  3. ;({ num, goods } = { num: 1, goods: 6000 })
  4. console.log(num, goods)
  5. 运行代码结果:
  6. 2 3000
  7. 1 6000

2.访问器属性

  1. let course = {
  2. jslesson: {
  3. name: 'JS',
  4. score: 80,
  5. },
  6. getScore() {
  7. return {
  8. name: this.jslesson.name,
  9. course: this.jslesson.score,
  10. }
  11. },
  12. setScore(score) {
  13. if (score >= 0 && score <= 100) {
  14. this.jslesson.score = score
  15. } else {
  16. console.log('成绩必须在1-100分之间')
  17. }
  18. },
  19. }
  20. console.log(course.jslesson.score)
  21. console.log(course.getScore())
  22. course.setScore(69)
  23. console.log(course.getScore())
  24. 运行结果:
  25. 80
  26. { name: 'JS', course: 80 }
  27. { name: 'JS', course: 69 }
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