寄生组合继承

185 阅读1分钟


        function intheritProto(son, fa) {
            const proto = Object.create(fa.prototype)
            proto.constructor = son
            son.prototype = proto
        }
        function fa(name) {
            this.name = name
        }
        fa.prototype.intro = function () {
            console.log('fa', this.name);
        }
        function son(name, age) {
            fa.call(this, name)
            this.age = age
        }
        intheritProto(son, fa)
        son.prototype.intro = function () {
            console.log('son', this.name, this.age);
        }
        var son1 = new son('wenhao', '21')
        var fa1 = new fa('haha')