Skip to content

JavaScript原型继承

js
function A() {
    this.a = 'A'
}

A.prototype.showA = function () {
    console.log(this.a)
};

function B() {
    A.call(this);
    this.b = 'B'
}

B.prototype = Object.create(A.prototype);
B.prototype.constructor = B;	// 把B原型的构造函数修复为B
B.prototype.showB = function () {
    console.log(this.b)
};

b = new B();
b.showA();
b.showB();
console.log('b instanceof B: ', b instanceof B);
console.log('b instanceof A: ', b instanceof A);

打印结果:

js
A
B
b instanceof B:  true
b instanceof A:  true

封装可复用继承函数:

js
function inherit(parent, child) {
    child.prototype = Object.create(parent.prototype);
    child.prototype.constructor = child;
}

实现继承:

js
function A(a) {
    this.a = a
}

A.prototype.showA = function () {
    console.log(this.a)
};

function B(a, b) {
    A.call(this, a);
    this.b = b
}

inherit(A, B);  // 继承

B.prototype.showB = function () {
    console.log(this.b)
};

b = new B('aaa', 'bbb');
console.log(b.a);
console.log(b.b);
b.showA();
b.showB();
console.log('b instanceof B: ', b instanceof B);
console.log('b instanceof A: ', b instanceof A);

打印结果:

aaa
bbb
aaa
bbb
b instanceof B:  true
b instanceof A:  true