JavaScript继承
目录
- es5 构造函数
- es6 class
es5
function A() {
this.a = 1;
}
function B() {
this.b = 2;
}
function C() {
A.call(this);
B.call(this);
this.c =3;
}
C.prototype = Object.create(A.prototype);
Object.assign(C.prototype,B.prototype);
C.prototype.constructor = C;
var c = new C();
es6 class
class A {
constructor() {
this.x =1;
}
print() {
console.log(this.x);
}
}
class B extends A {
constructor() {
super();
this.x =2;
}
m() {
super.print();
}
}
let b = new B();