描述
请补全JavaScript代码,实现以下功能:
- 给"Human"构造函数的原型对象添加"getName"方法,返回当前实例"name"属性
- 将"Chinese"构造函数继承于"Human"构造函数
- 给"Chinese"构造函数的原型对象添加"getAge"方法,返回当前实例"age"属性
代码
function Human(name) {
this.name = name
this.kingdom = 'animal'
this.color = ['yellow', 'white', 'brown', 'black']
}
function Chinese(name, age) {
Human.call(this, name)
this.age = age
this.color = 'yellow'
}
// 通过Human.prototype:给“Human”的原型添加“getName”函数
Human.prototype.getName = function() { return this.name; }
//通过Chinese.prototype:将“Chinese”的原型挂载在“Human”构造函数的实例上
Chinese.prototype = new Human();
//修复“Chinese”的原型链
Chinese.prototype.contructor = Chinese;
//通过Chinese.prototype:给“Chinese”的原型添加“getAge“函数
Chinese.prototype.getAge = function() { return this.age; }