让子类的原型对象指向父类的实例,当子类实例找不到对应的方法时,它会顺着原型链往上找
function Parent(){
this.name='blackpink'
}
Parent.prototype.getName=function(){
return this.name
}
function Child(){
}
Child.prototype=new Parent()
Child.prototype.constructor=Child
const child=new Child()
console.log(child.name)//blackpink
console.log(child.getName())//blackpink
//------------------------------------------------
function Parent(){
this.name=['blackpink']
}
Parent.prototype.getName=function(){
return this.name
}
function Child(){
}
Child.prototype=new Parent()
Child.prototype.constructor=Child
const child1=new Child()
const child2=new Child()
child1.name[0]='jisoo'
console.log(child1.name)//jisoo
console.log(child2.name)//jisoo
原型链的缺点:①多个原型指向同一个实例,当有多个实例化子对象时,修改一个会影响其他对象
②无法实现super功能,即无法对父类进行传参