当前位置: 移动技术网 > IT编程>开发语言>JavaScript > js继承的几种方式

js继承的几种方式

2019年07月12日  | 移动技术网IT编程  | 我要评论

最近在面试的时候,遇到过两次问继承实现的几种方式,这里能我给大家列举了以下的这几种,给大家参考参考

方式一:借助构造函数实现继承

这里使用的原理就是在child里面,把parent的this指向改为是child的this指向,从而实现继承

function parent(name){
    this.name=name;
    }
    parent.prototype.saihi=function(){
        console.log("hello")
    }
    function child(name,age,gender){
        parent.call(this,name)
        this.age=age;
        this.gender=gender;
    }
    let child=new child("王磊",20,"男")
    console.log(child.name);// 王磊
    child.sayhi(); // uncaught typeerror:child.sayhi is not a function

缺点:只能解决属性的继承,使用属性的值不重复,但是父级类别的方法不能继承

方式二:借助原型链实现继承

第二种方式就是把child的原型改为是parent的实例,从而实现继承

     function parent(name,gender){
            this.name=name;
            this.gender=gender;
            this.list=[1,2,3]
        }
        parent.prototype.eat=function(){
            console.log("晚餐时间到")
        }
        function child(age){
            this.age=age;
        }
        child.prototype=new parent("李白","男");
        var child=new child(20);
        var child2=new child(30);
        child.eat();
        console.log(child.list,child2.list);// [1,2,3] [1,2,3]
        child.list.push(4)
        console.log(child.list);// [1,2,3,4]        
        console.log(child2.list);// [1,2,3,4]

缺点:因为child的原型对象都是new parent,所以实例化出来的对象的属性都是一样的,而且parent上面的引用类型只要有一个实例对象修改了,其他也会跟着修改.因为他们原型对象都是共用的

方式三:组合型

方式三的话是结合了方式一和方式二来实现继承

function person(school){
            this.school=school;
        }
        person.prototype.skill=function(){
            console.log("学习");
        }
        function student(school,name,age,gender){
            parent.call(this,school);
            this.name=name;
            this.age=age;
            this.gender=gender;
        }
        student.prototype=person.prototype;
        let student=new student("广铁一中","王菲菲",14,"女");
        console.log(student.prototype===person.prototype)
        console.log(student.constructor)

缺点:父类的原型对象调用了两次,没有必要,而且student实例的构造函数是来自于person

方式四:组合方式优化

function parent(name,play){
            this.name=name;
            this.play=play;
        }
        function child(name,play,age){
            parent.call(this,name,play);
            this.age=age;
        }
        child.prototype=parent.prototype;
        let child=new child("张三","玩",20);
        let child2=new child("李四","吃",10)
        console.log(child,child2)
        console.log(child.prototype===child2.prototype); true
        console.log(child.constructor); // 构造函数指向的是parent

缺点:child实例的构造函数来自于parent

方式五: 组方式优化

只是这种方式的话,你必须得理解object.create()方法的使用,他创建的对象是在原型上面的

function parent(name,play){
            this.name=name;
            this.play=play;
        }
        function child(name,play,age){
            parent.call(this,name,play);
            this.age=age;
        }
        child.prototype=object.create(parent.prototype);// 隔离了父类和子类的构造函数,父类的添加到了__proto__属性上
        child.prototype.constructor=child
        let child=new child("张三","玩",20);
        let child2=new child("李四","吃",10)
       
        console.log(child.constructor)

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网