Blogger Information
Blog 35
fans 0
comment 0
visits 16757
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
1. 实例演示class类与extends,super等的用法 2. 实例演示字符串,数组常用API
手机用户311660634
Original
360 people have browsed it
  1. // 1. 实例演示class类与extends,super等的用法 2. 实例演示字符串,数组常用API (至少5个以上)
  2. let user = class {
  3. constructor(name, age, sex){
  4. this.name = name
  5. this.age = age
  6. this.sex = sex
  7. }
  8. say(){
  9. return `${this.name} (${this.age}岁 ${this.sex})`
  10. }
  11. static nation = `CHINA`
  12. }
  13. // user1继承user类的属性功能
  14. class user1 extends user {
  15. constructor(name, age, sex, cm){
  16. // super是调用父类(user)的成员
  17. super(name, age, sex)
  18. // 这是子类的自我扩展
  19. this.cm = cm
  20. }
  21. say(){
  22. // super.say是调用user.say,this.cm是子类自己添加的属性
  23. return` ${super.say}, (${this.cm})`
  24. }
  25. }
  26. // 数组常用API
  27. // 1. ...rest这是压缩和展开
  28. arr = [1,2,3]
  29. // arr的数组内容在数组arr1中展开
  30. let arr1 = [...arr]
  31. // 下面的...arr是数组之前自有内容,456是添加内容,结果1,2,3,4,5,6
  32. arr = [...arr, 4, 5, 6]
  33. // 2.array.from();类数组变成真正的数组
  34. const likeArr = {
  35. 0: 'red',
  36. 1: 'blue',
  37. 2: 'green',
  38. length: 3,
  39. }
  40. // 用array.from直接把类数组变成真正的数组
  41. const data = Array.from(likeArr)
  42. // 3.new Set()去重
  43. const arr2 = [1,1,2,2,3,3]
  44. console.log(new Set(arr2)) // [1,2,3]
  45. // 4.sort()排序
  46. console.log(arr2.sort((a,b)=>b-a))
  47. //结果[3,3,2,2,1,1] sort((a,b)=>b-a) 后面的b-a是倒序的意思,如果是a-b就是正序
  48. // 5.join()在数组值中间加入自己想要的东西
  49. console.log(arr2.join('-'))//[3-3-2-2-1-1]
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