天天看點

#yyds幹貨盤點# 前端歌謠的刷題之路-第九十一題-繼承

前言

我是歌謠 我有個兄弟 巅峰的時候排名c站總榜19 叫前端小歌謠 曾經我花了三年的時間創作了他 現在我要用五年的時間超越他 今天又是接近兄弟的一天人生難免坎坷 大不了從頭再來 歌謠的意志是永恒的 放棄很容易 但是堅持一定很酷 本題目源自于牛客網 微信公衆号前端小歌謠

題目

請補全JavaScript代碼,實作以下功能:

1. 給"Human"構造函數的原型對象添加"getName"方法,傳回目前執行個體"name"屬性

2. 将"Chinese"構造函數繼承于"Human"構造函數

3. 給"Chinese"構造函數的原型對象添加"getAge"方法,傳回目前執行個體"age"屬性

#yyds幹貨盤點# 前端歌謠的刷題之路-第九十一題-繼承
#yyds幹貨盤點# 前端歌謠的刷題之路-第九十一題-繼承

編輯

 核心代碼

<!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>      
#yyds幹貨盤點# 前端歌謠的刷題之路-第九十一題-繼承

總結