0
点赞
收藏
分享

微信扫一扫

前端歌谣的刷题之路-第九十一题-继承


 

前言

我是歌谣 我有个兄弟 巅峰的时候排名c站总榜19 叫前端小歌谣 曾经我花了三年的时间创作了他 现在我要用五年的时间超越他 今天又是接近兄弟的一天人生难免坎坷 大不了从头再来 歌谣的意志是永恒的 放弃很容易 但是坚持一定很酷 本题目源自于牛客网 微信公众号前端小歌谣

题目

请补全JavaScript代码,实现以下功能:
1. 给"Human"构造函数的原型对象添加"getName"方法,返回当前实例"name"属性
2. 将"Chinese"构造函数继承于"Human"构造函数
3. 给"Chinese"构造函数的原型对象添加"getAge"方法,返回当前实例"age"属性

前端歌谣的刷题之路-第九十一题-继承_html

前端歌谣的刷题之路-第九十一题-继承_原型对象_02编辑

 核心代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>继承</title>
</head>

<body>
  <!-- 请补全JavaScript代码,实现以下功能:
1. 给"Human"构造函数的原型对象添加"getName"方法,返回当前实例"name"属性
2. 将"Chinese"构造函数继承于"Human"构造函数
3. 给"Chinese"构造函数的原型对象添加"getAge"方法,返回当前实例"age"属性 -->
  <script>
    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
    }
  </script>
</body>

</html>

前端歌谣的刷题之路-第九十一题-继承_原型对象_03

总结

按照题意一步步书写即可 没有太多的不同之处


举报

相关推荐

0 条评论