ES6 class类的继承

桃扇骨 2023-02-22 03:53 198阅读 0赞

类的继承:extends关键字用于类声明或者类表达式中,以创建一个类,该类是另一个类的子类。

extends关键字用来创建一个普通类或者内建对象的子类

一、继承一个类:

  1. class Foo {
  2. constructor(){
  3. this.a = 1;
  4. }
  5. func1(){
  6. console.log("func1");
  7. }
  8. }
  9. class Bar extends Foo{
  10. constructor(){
  11. super();
  12. this.b = 2;
  13. }
  14. func2(){
  15. console.log("func2");
  16. }
  17. }
  18. var bar = new Bar();
  19. console.log(bar);

20200701141254565.png

拓展一个内置对象:

  1. class myDate extends Date{
  2. constructor(){
  3. super();
  4. this.name = "myDate";
  5. }
  6. func(){
  7. console.log("拓展内置对象Date");
  8. }
  9. }
  10. var date = new myDate();
  11. console.log(date,date.name);Wed Jul 01 2020 14:31:36 GMT+0800 (中国标准时间) "myDate"
  12. date.func() 拓展内置对象Date

二、super方法

必须在constructor中先执行super方法,必须将super方法执行写在constructor最前面

以下的代码会报错,当创建bar新的对象的时候,执行new Bar()时,会报错。this是undefined。

  1. class Foo {
  2. constructor(){
  3. this.a = 1;
  4. }
  5. func1(){
  6. console.log("func1");
  7. }
  8. }
  9. class Bar extends Foo{
  10. constructor(){
  11. this.c = 3; 报错:thisundefined
  12. super();
  13. this.b = 2;
  14. }
  15. func2(){
  16. console.log("func2");
  17. }
  18. }
  19. var bar = new Bar();

子类继承父类的静态方法

  1. class Foo {
  2. constructor(){
  3. this.a = 1;
  4. }
  5. static func(){
  6. console.log("father static function")
  7. }
  8. func1(){
  9. console.log("func1");
  10. }
  11. }
  12. class Bar extends Foo{
  13. constructor(){
  14. super();
  15. this.b = 2;
  16. this.c = 3;
  17. }
  18. func2(){
  19. console.log("func2");
  20. Bar.func();//子类可以继承父类的静态方法func
  21. }
  22. }
  23. var bar = new Bar();
  24. console.log(bar); Bar {a: 1, b: 2, c: 3}
  25. bar.func2(); 执行func2函数,又执行了func函数

也可以继承静态属性:静态属性设置写在子类前,或子类后都是可以,但是一定要在实例对象创建前。

  1. class Foo {
  2. constructor(){
  3. this.a = 1;
  4. }
  5. static func(){
  6. console.log("father static function")
  7. }
  8. func1(){
  9. console.log("func1");
  10. }
  11. }
  12. class Bar extends Foo{
  13. constructor(){
  14. super();
  15. this.b = 2;
  16. this.c = 3;
  17. }
  18. func2(){
  19. console.log(Bar.prop);
  20. }
  21. }
  22. Foo.prop = "static prop";
  23. var bar = new Bar();
  24. console.log(bar);
  25. bar.func2(); 打印:static prop

三、先理解《class类的深入理解》文章的例子,下面实例中,新建的类Bar继承Foo类

  1. let myName = "zhu";
  2. class Foo{
  3. name = myName;
  4. constructor(x,y){
  5. this.a = x;
  6. this.b = y;
  7. var c = "2222";
  8. this.getFooP = Foo.p;
  9. }
  10. sum(){
  11. Foo.q = "Foo类可以用在方法中"
  12. console.log(this.a+this.b);
  13. }
  14. static f1(){
  15. return "类Foo的静态方法"
  16. }
  17. get prop(){
  18. return this.a;
  19. }
  20. set prop(value){
  21. this.a = value;
  22. }
  23. }
  24. Foo.p = "静态属性";
  25. Foo.f2 = function(){
  26. return "类Foo的静态方法"
  27. }
  28. let foo = new Foo(1,2);
  29. console.log(foo);
  30. class Bar extends Foo{
  31. constructor(a,b){
  32. super(a,b);
  33. this.d = "100";
  34. }
  35. fun1(){
  36. return "fun1";
  37. }
  38. }
  39. let bar = new Bar(10,20);
  40. console.log(bar);

结果:

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3p5ejAwMDAwMDAw_size_16_color_FFFFFF_t_70

子类Bar继承了父类Foo的静态方法和属性。

20201109160721887.png

总结:

(1)实例对象bar得到父类的实例对象foo属性和方法。即constructor的this内容和Foo类结构体内的属性

(2)在bar对象的原型Bar.prototype上继承了Foo类的原型Foo.prototype

(3)子类Bar继承了父类Foo的静态方法和属性。

发表评论

表情:
评论列表 (有 0 条评论,198人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ES6 class

    ES6中的类 js中生成实例对象的传统方法是通过构造函数。而ES6提供了更接近传统语言的写法,引入了class类这个概念,作为对象的模板,通过class类关键字,可以定义

    相关 ES6 class继承

    类的继承:`extends`关键字用于[类声明][Link 1]或者[类表达式][Link 2]中,以创建一个类,该类是另一个类的子类。 `extends`关键字用来创建一个

    相关 ES6--Class

    Class概述 概述 在ES6中,class (类)作为对象的模板被引入,可以通过 class 关键字定义类。 class 的本质是 function。 它可以