博主信息
博文 18
粉丝 0
评论 0
访问量 13956
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
class类的继承和数组常用API演示
时间在渗透
原创
949人浏览过
  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' ]

批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学