zoukankan      html  css  js  c++  java
  • javascript的几种继承

    1.原型链继承:构造函数、原型和实例的关系:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。确认原型和实例之间的关系用instanceof。

    原型链继承缺点:字面量重写原型会中断关系,使用引用类型的原型,并且子类型还无法给超类型传递参数

    function Parent(){
            this.name='mike';
        }
        function Child(){
            this.age=12;
        }
        //儿子继承父亲(原型链)
        Child.prototype=new Parent();//Child继承Parent,通过原型形成链条
        var test=new Child();
        console.log(test.age);
        console.log(test.name);//得到被继承的属性
        //孙子继续原型链继承儿子
        function Brother(){
            this.weight=60;
        }
        Brother.prototype=new Child();//继承原型链继承
        var brother=new Brother();
        console.log(brother.name);//继承了Parent和Child,弹出mike
        console.log(brother.age);//12
    
        console.log(brother instanceof Child);//ture
        console.log(brother instanceof Parent);//ture
        console.log(brother instanceof Object);//ture
    View Code

    2.构造函数实现继承:又叫伪造对象或经典继承。

    构造函数实现继承缺点:借用构造函数虽然解决了原型链继承的两种问题,但没有原型,则复用无从谈起,所以需要原型链+借用构造函数模式。

    function Parent(age){
            this.name=['mike','jack','smith'];
            this.age=age;
        }
        function Child(age){
            Parent.call(this,age);//把this指向Parent,同时还可以传递参数
        }
        var test=new Child(21);
        console.log(test.age);//21
        console.log(test.name);
    
        test.name.push('bill');
        console.log(test.name);//mike,jack,smith,bill
    View Code

    3.组合继承:使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。这样即通过在原型上定义方法实现了函数复用,又保证每个实现都有它自己的属性。

    缺点:无论什么情况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在创建子类型原型的时候,另一次是在子类型构造函数内部。

    function Parent(age){
            this.name=['mike','jack','smith'];
            this.age=age;
        }
        Parent.prototype.run=function(){
            return this.name+' are both '+this.age;
        }
        function Child(age){
            Parent.call(this,age);//给超类型传参,第二次调用
        }
        Child.prototype=new Parent();//原型链继承,第一次调用
        var test1=new Child(21);//写new Parent(21)也行
        console.log(test1.run());//mike,jack,smith are both 21
    
        var test2=new Child(22);
        console.log(test2.age);
        console.log(test1.age);
        console.log(test2.run());
        //这样可以使test1和test2分别拥有自己的属性age同时又可以有run方法
    View Code

    4.原型式继承:借助原型可以基于已有的对象创建新对象,同时还不必因此创建自定义类型。它要求必须有一个对象可以作为另一个对象的基础。

    function object(o){
            function F(){};
            F.prototype=o;
            return new F();
        }
        var person={
            name:'nicho',
            friends:['shell','jim','lucy']
        }
        var anotherPerson = object(person);
        anotherPerson.name = 'Greg';
        anotherPerson.friends.push('Rob');
        console.log(anotherPerson.friends);//["shell", "jim", "lucy", "Rob"]
    
        var yetAnotherPerson = object(person);
        yetAnotherPerson.name = 'Linda';
        yetAnotherPerson.friends.push('Barbie');
        console.log(yetAnotherPerson.friends);//["shell", "jim", "lucy", "Rob", "Barbie"]
    
        console.log(person.friends);//["shell", "jim", "lucy", "Rob", "Barbie"]
    View Code

    ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和(可选的)一个为新对象定义属性的对象。

    var person2={
            name:'nicho',
            friends:['shell','jim','lucy']
        };
        var anoP2=Object.create(person2);
        anoP2.name="Greg";
        anoP2.friends.push('Rob');
        console.log(anoP2.friends);//["shell", "jim", "lucy", "Rob"]
    
        var yetP2=Object.create(person2);
        yetP2.name="Linda";
        yetP2.friends.push('Barbie');
        console.log(yetP2.friends);//["shell", "jim", "lucy", "Rob", "Barbie"]
    
        console.log(person2.friends);//["shell", "jim", "lucy", "Rob", "Barbie"]
        /*以这种方式指定的任何属性都会覆盖原型对象上的同名属性。*/
        var threeP=Object.create(person,{
            name:{value:'red'}
        });
        console.log(threeP.name);//red,如果threeP中无name则输出person2里的name值nicho
    View Code

    5.寄生式继承:思路与寄生构造函数和工厂模式类似,即创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真地是它做了所有工作一样返回对象。

    function object(o){
            function F(){};
            F.prototype=o;
            return new F();
        };
        function createAnother(o){
            var cl=object(o);
            cl.sayHi=function(){
                console.log('hi');
            }
            return cl;
        };
        var person={
            name:'nick',
            friends:['shelby','court','van']
        }
        var anotherPerson=createAnother(person);
        anotherPerson.sayHi();//hi
        console.log(anotherPerson.name);//nick
        console.log(anotherPerson.friends);//["shelby", "court", "van"]
    
        /*这个例子中的代码基于 person 返回了一个新对象—— anotherPerson 。 新对象不仅具有 person
         的所有属性和方法,而且还有自己的 sayHi() 方法*/
    View Code

    寄生组合式继承:无论什么情况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在创建子类型原型的时候,另一次是在子类型构造函数内部,这样子类型最终会包含超类型对象的全部实例属性,我们不得不在调用子类型构造函数时重写这些属性。因此出现了寄生组合式继承。

    6.寄生组合式继承:借用构造函数来继承属性,通过原型链的混成形式来继承方法。基本思路:不必为了指定子类型的原型而调用超类型的构造函数。本质上就是使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。

    function SuperType(name){
            this.name=name;
            this.colors=['red','blue','green'];
        }
        SuperType.prototype.sayName=function(){
            console.log(this.name);
        }
        function SubType(name,age){
            SuperType.call(this,name);
            this.age=age;
        }
        function object(o){
            function F(){};
            F.prototype=o;
            return new F();
        };
        /*inheritPrototype此函数第一步是创建超类型原型的一个副本。第二步是为创建的副本添加constructor属性,
        * 从而弥补因重写原型而失去的默认的constructor属性,第三步将新创建的对象(副本)赋值给子类型的原型*/
        function inheritPrototype(subType,superType){
            var prototype=object(superType.prototype);//创建对象
            prototype.constructor=subType;//增强对象
            subType.prototype=prototype;//指定对象
        }
        inheritPrototype(SubType,SuperType);
        SubType.prototype.sayAge=function(){
            console.log(this.age);
        }
    
        var p=new SubType('xiaoli',24);
        console.log(p.sayName());
        console.log(p.sayAge());
        console.log(p.colors)
    View Code

    此方法优点:只调用了一次父类SuperType构造函数,并且因此避免了在SubType.prototype上面创建不必要的多余的属性。同时原型链还能保持不变,还能正常使用instanceof和isPrototypeOf();



    作者:haoxl
    出版:http://www.cnblogs.com/haoxl/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。

  • 相关阅读:
    tp5怎么验证手机号码
    awk理论详解、实战
    chpasswd、dd命令、find实战、添加系统服务、buffer、cached
    python中执行shell命令的几个方法
    rsync使用详解
    sort、dirname、添加环境变量、修改主机名、别名IP、静态路由
    awk数组详解、实战
    python使用Queue进行进程间通信
    python生成器、迭代器、__call__、闭包简单说明
    python装饰器的深度探究
  • 原文地址:https://www.cnblogs.com/haoxl/p/5248854.html
Copyright © 2011-2022 走看看