原型和原型链实现继承

原型和原型链,两个类之间实现继承

组合继承(原型继承+构造函数继承,既能解决属性问题又能解决方法问题)

function Person(name,age){

this.name = name;

this.age = age;

}

Person.prototype.play = function(){

    console.log(我爱玩);

}

function Child(name,age,score){

Person.call(this,name,age);

this.score = score;

}

Child.prototype = new Person()

var chil = new Child(‘哈利’,10,100);

chil.play()   //我爱玩

在子构造函数内部调用父构造函数.call(this,name,age,score)   //可以继承属性

子构造函数的原型=new 父构造函数()                     //可以继承共用的方法和属性,这里不传形参就不用继承属性

这样两者结合就可以实现继承方法和属性

猜你喜欢

转载自www.cnblogs.com/dylAlex/p/11002232.html