题解 | #继承#

继承

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

//考查js继承的几种方式
//如下使用寄生式组合继承
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.getName = function (){
                returnthis.name
            }
            //Chinese.prototype = new Human()
            Chinese.prototype = Object.create(Human.prototype)
            Chinese.prototype.getAge = function(){
                returnthis.age
            }

全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务