javascript继承实现

方式1. 构造函数绑定

function A() {
this.am = "aaa";
this.af = function() {
console.log("aaafff");
}
}
function B() {
A.call(this, arguments);//A.apply(this. arguments);
this.bm = "bbb";
}
var b = new B();
console.log(b.am);
b.af();

方式2. 利用prototype

function A() {
this.am = "aaa1";
}
A.prototype.af = function() {
console.log("f1f1");
}
function B() {
this.bm = "xxx";
}
B.prototype = new A();
B.prototype.constructor = B;
B.prototype.bf = function() {
console.log("22222");
}
var b = new B();
console.log(b.am);
b.af();
b.bf();

原文地址:https://www.cnblogs.com/feilv/p/4095306.html