跳到主要内容
青山指南正在为您准备页面
医疗级专业核验
透明收费 · 拒绝隐形 每天 08:00-20:00
资讯

SE5寄生组合式继承

养老服务、照护经验与行业动态,为家庭提供实用参考。

SE5寄生组合式继承
   // ES5继承三步骤:
        // 1).在子类构造函数中调用父类构造函数,让子类实例化出来的对象拥有父类的属性
        // 2).让子类的原型成为父类的实例,让子类实例化出来的对象拥有父类的方法
        // 3).找回丢失的构造函数
        // 定义父类
        function User(username, password) {
            this.username = username;
            this.password = password;
        }
        User.prototype.login = function () {
            console.log(this.username + "登录成功!");
        }
        // 定义子类
        function Student(uid, ...arr) {
            this.uid = uid;
            // 1)
            User.apply(this, arr);
        }
        Student.prototype = Object.create(User.prototype)
        Student.prototype.constructor = Student
        let p1 = new Student("001", "小明", "151568")
        console.log(p1);
        p1.login()

        //寄生组合式继承原理
        function create(obj) {
            function F() {

            };
            F.prototype = obj;
            return new F();
        }
咨询顾问返回首页
分享经验

发表评论

读者交流

全部评论

0

还没有评论,欢迎留下第一条讨论。