类的继承和扩展有什么区别?

var*_*ius 1 javascript ecmascript-6

我一直在寻找asnwer很长时间,我觉得它有点复杂.类的继承和扩展有什么区别?

我的问题是在读完这本电子书后出生的,我使用的是extends语法,所以这让我很奇怪.

扩展类

class A {
    a = 2;

    constructor(x) {
        this.a = x;
    }
}

class B extends A {
}
Run Code Online (Sandbox Code Playgroud)

类继承

class A {
    a = 4;

    A(x) {
        a = x;
    }

    drive() {
        output( "A" )
    }
}
class B inherits A {
    drive() {
        inherited:drive()
        output( "B" )
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以使用constructor,当inherits类?还是name constructor扩展课程?

使用super或时有什么区别inherited

我可以inherited在扩展类时使用语法吗?我读到这super是子类的构造函数引用其父类的构造函数的直接方法.

Ber*_*rgi 5

inherits不是ES6中的关键字.在class声明的那个地方,只有extends有效,你有一个语法错误.

既不是a = 4;集体也不是inherited:drive().您在本书中发现的这一部分甚至明确指出" 为继承的类考虑这个松散的伪代码(发明的语法) ".