前端-对js原型继承的简单举例

2023-03-13,,

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

前端-对js原型继承的简单举例的相关教程结束。

《前端-对js原型继承的简单举例.doc》

下载本文的Word格式文档,以方便收藏与打印。