13.es6类+继承.html 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. /**
  11. * ES6提供了class(类)的概念;
  12. * 通过class定义关键字,可以是定义类
  13. * class 定义类
  14. * constructor 定义构造函数初始化
  15. * extends 继承父类
  16. * super 调用父级构造函数方法
  17. * static 定义静态的方法和属性
  18. */
  19. // class定义类
  20. class Phone {
  21. // 构造函数
  22. constructor(color,rain,holiday) {
  23. this.color = color;
  24. this.rain = rain;
  25. this.holiday = holiday;
  26. }
  27. // 类的方法
  28. call() {
  29. console.log("打电话");
  30. }
  31. green() {
  32. console.log("这是绿色")
  33. }
  34. }
  35. //es6的继承
  36. class newPhone extends Phone {
  37. constructor(color,rain,holiday,book,vase){
  38. super(color,rain,holiday);
  39. this.book = book;
  40. this.vase = vase;
  41. }
  42. music() {
  43. console.log("音乐");
  44. }
  45. static end() {
  46. console.log("结束");
  47. }
  48. }
  49. var a1 = new Phone('红色','晴天','过年');
  50. console.log(a1);
  51. var a2 = new newPhone('12','23','34','掘金','花瓶');
  52. console.log(a2,'a2');
  53. a2.call();
  54. a2.music();
  55. newPhone.end();
  56. </script>
  57. </body>
  58. </html>