18_继承
本题考点:call、原型链
根据题目要求,将构造函数“Chinese”继承于“Human”,并且实现各项功能,核心步骤有:
- 首先通过Human.prototype.getName给“Human”的原型添加“getName”函数
- 然后通过Chinese.prototype将“Chinese”的原型挂载在“Human”构造函数的实例上
- 修复“Chinese”的原型链
- 最后通过Chinese.prototype.getAge给“Chinese”的原型添加“getAge“函数
参考答案:
function Human(name) {
this.name = name
this.kingdom = 'animal'
this.color = ['yellow', 'white', 'brown', 'black']
}
Human.prototype.getName = function() {
return this.name
}
function Chinese(name,age) {
Human.call(this,name)
this.age = age
this.color = 'yellow'
}
Chinese.prototype = new Human()
Chinese.prototype.constructor = Chinese
Chinese.prototype.getAge = function() {
return this.age
}