10.es6类.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. // 类 class声明类
  11. class Person {
  12. // 1.构造器 属性
  13. constructor(name,age){
  14. this.name = name;
  15. this.age = age;
  16. }
  17. // 2.方法
  18. say() {
  19. console.log("hello")
  20. }
  21. // static 类方法
  22. static hello() {
  23. console.log("你好啊");
  24. }
  25. }
  26. // 继承类
  27. // child要继承person中的属性及方法
  28. class Child extends Person {
  29. constructor(name,age,aaa,bbb) {
  30. // super() 继承父类
  31. super(name,age);
  32. this.aaa = aaa;
  33. this.bbb = bbb;
  34. }
  35. hi() {
  36. console.log("hi")
  37. }
  38. }
  39. // 调用
  40. // 实例化对象
  41. let per = new Person("John",20)
  42. console.log(per.name,per.age);
  43. per.say();
  44. // per.hello()
  45. Person.hello()
  46. let children = new Child("Lucy",10,11,22);
  47. console.log(children.name,children.age,children.aaa,children.bbb);
  48. children.say();
  49. children.hi()
  50. </script>
  51. </body>
  52. </html>