0
点赞
收藏
分享

微信扫一扫

继承(组合继承)

沈芏 2022-03-14 阅读 115

描述

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

<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
    </head>
    <body>
    	
        <script type="text/javascript">
            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"构造函数继承于"Human"构造函数(重点记住,新知识点)
            Chinese.prototype=new Human()
            Chinese.prototype.constructor=Chinese
            Chinese.prototype.getAge=function(){
                return this.age
            } 

            
        </script>
    </body>
</html>
举报

相关推荐

0 条评论