Blogger Information
Blog 18
fans 0
comment 0
visits 8472
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
class类的继承和数组常用API演示
时间在渗透
Original
489 people have browsed it
  1. // 一、实例演示class类与extends,super等的用法
  2. // 父类
  3. let Small_Head_Dad = class {
  4. // 构造函数:声明属性
  5. constructor(name) {
  6. this.name = name;
  7. // 技能
  8. this.sing = '会高歌一曲';
  9. this.cook = '一桌满汉全席';
  10. }
  11. // 方法
  12. say(){
  13. return this.name + this.sing
  14. }
  15. // 方法
  16. dinner(){
  17. return this.name + this.cook
  18. }
  19. }
  20. // 创建新对象
  21. let datou = new Small_Head_Dad('爸爸')
  22. // 输出父亲会唱歌
  23. console.log(datou.say())
  24. // 儿子继承唱歌 继承
  25. class Big_Head_Son extends Small_Head_Dad {
  26. constructor(name,sing,status) {
  27. // super 调用父类成员
  28. super(name,sing,status);
  29. // 子类扩展的属性
  30. this.status = '上学'
  31. }
  32. // 子类方法
  33. school(){
  34. return this.name + '还在'+this.status
  35. }
  36. // 子类方法
  37. skill(){
  38. return this.name + '会'+this.sing
  39. }
  40. }
  41. let son = new Big_Head_Son('儿子')
  42. // 儿子继承了父亲的唱歌方法
  43. console.log(son.say())
  44. // 子类方法
  45. console.log('子类方法(school):'+son.school())
  46. console.log('子类方法(skill):'+son.skill())
  47. // 二、 实例演示字符串,数组常用API (至少5个以上)
  48. // replace()替换
  49. let arr = '今天天气怎么样?';
  50. console.log(arr.replace('怎么样?','晴朗!')) // 今天天气晴朗!
  51. // substring() 提取字符
  52. console.log(arr.substring(0,4)) // 今天天气
  53. // split() 字符串 -> 数组
  54. console.log(arr.split('怎')) // [ '今天天气', '么样?' ]
  55. // toString() 将数组转为字符串
  56. let arr2 = ['1','3','5','7']
  57. console.log(arr2.toString()) // ['1','3','5','7'] => 1,3,5,7
  58. // reverse() 翻转数组
  59. console.log(arr2.reverse()) // [ '7', '5', '3', '1' ]
  60. // push() 在末尾添加元素
  61. console.log(arr2.push('新增')) // 返回值是数组长度
  62. console.log(arr2) // [ '7', '5', '3', '1', '新增' ]
  63. // pop() 删除数组末尾的一个元素
  64. console.log(arr2.pop()) // 返回值是删除的那个元素
  65. console.log(arr2) // [ '7', '5', '3', '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