文章阅读

沉浸阅读完整内容

写文章

SE5寄生组合式继承

正在统计阅读人数 正在统计字数
登录收藏0
字号 100%

   // 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();
        }

推荐阅读

4 篇精选
文章 / 41九九乘法表 文章 / 39js三种注册事件 文章 / 40个人主页 文章 / 42JS数组去重

发表评论

评论 0

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

分享精彩,传递价值

生成专属海报,分享你的文章,让更多人发现好内容

浏览器不支持 Canvas,无法生成海报。

每日签到

每天来这里,记录一点持续积累。

当前积分--积分
本月签到--
本月获得--积分
正在读取