题解 | #继承#

继承

http://www.nowcoder.com/practice/ee1e5ee81b1c4fbb8c05a4e903621762

18_继承

本题考点:call、原型链

根据题目要求,将构造函数“Chinese”继承于“Human”,并且实现各项功能,核心步骤有:

  1. 首先通过Human.prototype.getName给“Human”的原型添加“getName”函数
  2. 然后通过Chinese.prototype将“Chinese”的原型挂载在“Human”构造函数的实例上
  3. 修复“Chinese”的原型链
  4. 最后通过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
}

全部评论

相关推荐

Sigma429:极兔啊,薪资开的巨低,还在上海,索性不做笔试了
点赞 评论 收藏
分享
评论
26
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务