前端-对js原型继承的简单举例
function A(name,color){
this.name=name;
this.color=color;
}
A.prototype.getColor=function(){
return this.color
}
A.prototype.getName=function(){
return this.name
}
function B(name ,color,type){
//继承方法1
A.call(this,name,color)
this.type=type
}
B.prototype.getType=function(){
return this.type
}
//继承方法2
Object.setPrototypeOf(B.prototype,A.prototype)
var a =new B('cc','yellow','nv')
console.log(a.name)
console.log(a.color,a.type)
// 结果 cc
// 结果 yellow nv
var b=new A('cc','yellow','nv')
console.log(b)
//结果 A {name: "cc", color: "yellow"}color: "yellow"name: "cc"__proto__: Object