原型对象加对象冒充组合继承

原型对象加冒充组合继承

	 // 原型对象加对象冒充组合继承
        function Person(name, type) {
            this.name = name;
            this.type = type;
            this.say = () => {
                console.log(`${this.name}喜欢${this.type}`)
            }
        }
        Person.prototype.sex = '男'
        Person.prototype.hoy = () => {
            console.log('hoy.....')
        }

        function Son(name, age) {
            //继承构造函数的属性和方法  call :string  apply : Array
            // Person.call(this, age, name)
            Person.apply(this, [age, name])
        }
        //继承构造韩式的原型链的属性和方法
        Son.prototype = new Person()
        //Son构造函数的constructor 需要重新指向 Son构造函数
        Son.prototype.constructor = Son
        Son.prototype.test = '测试'
        Son.prototype.func = () => {
            console.log('方法')
        }
        const newSon = new Son('张三', '篮球')
        console.log(newSon)
        newSon.say()
        newSon.hoy()
        newSon.func()
        console.log(newSon.sex)
        console.log(newSon.test)
        console.log(newSon.__proto__)

es6类继承

	class Father {
            static a = 20;
            static b = 30;
            constructor() {
                this.name = '三三'
                this.firstName = '冯'
            }
            get() {
                console.log(`我的名字叫${this.firstName}${this.name}`)
            }
        }
        const newFather = new Father();
        newFather.get()
        console.log(newFather, '00')
        console.log(newFather.name)
        console.log(newFather.firstName)
        console.log(Father.a)
        console.log(Father.b)
        class Son extends Father {
            constructor() {
                super()
            }
        }
        const newSon = new Son();
        console.log(newSon, '11')
        console.log(Son.a)
        console.log(Son.b)
        newSon.get()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呵呵的牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值