思路:注意,第一点,子函数中需要使用 父函数.call 调用父函数构造函数;注意:第二点,子函数的prototype属性需要指向一个新创建的父函数实例对象。
<script type="text/javascript">
function Human(name) {
this.name = name
this.kingdom = 'animal'
this.color = ['yellow', 'white', 'brown', 'black']
}
function Chinese(name,age) {
//注意:第一点,子函数中需要使用 父函数.call 调用父函数构造函数
Human.call(this,name)
this.age = age
this.color = 'yellow'
}
Human.prototype.getName=function(){
return this.name
}
//注意:第二点,子函数的prototype属性需要指向一个新创建的父函数实例对象
Chinese.prototype=new Human()
Chinese.prototype.getAge=function(){
return this.age
}
</script>
总结:注意,类的继承和函数的继承的区别!类的继承中:子类需要使用extends关键字和super关键字;函数的继承中:子函数需要使用父函数.call调用父函数的构造函数,子函数需要将prototype属性指向一个新创建的父函数实例对象。



京公网安备 11010502036488号