javascript实现继承的几种主要方法

//1.原型链继承
                var supClass = function(name, sex) {
                    this.name = name || 'red'
                    this.sex = sex || 'good'
                    this.father = 'father'
                    this.say = function() {
                        console.log(this.name)
                    }
                }
                supClass.prototype = {
                    sayHi() {
                        console.log(this.name + ' hi')
                    }
                }
                
                /*var sonClass = function(name, sex) {
                    this.name = name
                    this.sex = sex
                }
                console.log(sonClass.prototype)
                sonClass.prototype = new supClass()      //核心代码
                sonClass.prototype.constructor = sonClass//核心代码
                
                var son1 = new sonClass('red', 's')
                son1.say()*/
            //优点: 简单,容易实现
            //缺点: 多拷贝了一份supClass的属性过来,并且supClass的方法每生成一个新的
            //        sonClass就要再重新拷贝一份,造成了不必要的浪费
            
            
            //2.构造函数继承
                /*var sonClass = function(name, sex, type) {
                    supClass.call(this)
                    this.type = type
                    this.sex = sex
                    this.name = name
                }                
                var son1 = new sonClass('jay', 'man', 'goodman')
                son1.say()*/
            //优点: 简单,容易实现,可以同时继承多个父对象(三姓家奴)
            //缺点: 只能继承定义在父元素上的属性或方法,而对于父元素原型对象上的属性或方法
            //则无法继承
            
            //3.混合继承
                /*var sonClass = function(name, sex, type) {
                    supClass.call(this)
                    this.type = type
                    this.sex = sex
                    this.name = name
                }
                sonClass.prototype = new supClass()
                sonClass.prototype.constructor = sonClass
                
                var son1 = new sonClass('jay', 'man', 'goodman')
                son1.say()
                son1.sayHi()*/
            //优点: 几乎完美
            //缺点: 实现复杂,使用原型继承的部分仍然没有解决会多拷贝一份父类属性从而造成
            //不必要的空间浪费的问题(可以在sonClass的prototype中看到被屏蔽的继承自父类的属性)
            
            //4.寄生组合式继承
                //使用工厂函数将父元素的原型剥离出来单独赋值给子元素的原型
                function birth(f, s) {
                    var prototype = Object(f.prototype)
                    s.prototype = prototype
                    s.prototype.constructor = s
                }
                
                //组合式继承
                
                /*var sonClass = function(name, sex, type) {
                    supClass.call(this)
                    this.type = type
                    this.sex = sex
                    this.name = name
                }
                birth(supClass, sonClass)
                
                var son1 = new sonClass('jay', 'man', 'goodman')
                son1.say()
                son1.sayHi()*/
                
                //优点: 完美
                //缺点: 实现困难
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值