js继承

继承:子类继承父类
1、原型链继承
核心:将父类的实例作为子类的原型

	function Animal() {
        this.name = null;
        this.sex = null;
        this.sleep = function () {
            return "睡觉";
        };
        this.eat = function () {
            return "吃";
        };
        this.indexof = function () {
            console.log("索引");
        }
    }
    function Dog() {
        this.type = "犬科";
    }
    Dog.prototype = new Animal();

原型链的追加:

	Dog.prototype.color = "red";
    var dog = new Dog();
    console.log(typeof dog);//类型为object
    //instanceof用来判断B是否在A的原型链上,只有通过new出来的基本类型才能通过instanceof判断
    console.log(dog instanceof Dog);//Dog存在dog的原型链上,返回true
    console.log(dog instanceof Animal);
    console.log(dog);

特点:
非常纯粹的继承关系,实例是子类的实例,也是父类的实例,父类新增原型方法/原型属性,子类都能访问到,简单,易于实现
缺点:
要想为子类新增属性和方法,必须要在new Animal()这样的语句之后执行,不能放到构造器中,无法实现多继承。创建子类实例时,无法向父类构造函数传参
2、构造继承
核心:使用父类的构造函数来增强子类实例,等于是复制父类的实例属性给子类(没用到原型)

	function People() {
        this.name = arguments[0];
        this.sex = arguments[1];
        this.eat = function () {
            return this.name + "正在吃饭!";
        }
    }
    function Student() {
        this.score = arguments[0];
        this.writezuoye = function () {
            return this.name + "写作业";
        }
    }
    //构造继承不能继承父类的原型方法和属性
    Student.prototype.work = function () {
        return this.name + "跑步";
    }
    function smallchildren(name, sex, score) {
        People.call(this,name,sex);
        People.apply(this, [name, sex]);
        Student.call(this, score);
    }
    var small = new smallchildren("毛豆", "男", 487);
    console.log(small instanceof  People);//false
    console.log(small instanceof  smallchildren);//true
    console.log(small);//smallchildren对象
    console.log(small.eat());//毛豆正在吃饭
    console.log(small.writezuoye());//毛豆写作业

特点:
创建子类实例时,可以向父类传递参数。可以实现多继承(call多个父类对象)
缺点:
实例并不是父类的实例,只是子类的实例。只能继承父类的实例属性和方法,不能继承原型属性/方法。无法实现函数复用,每个子类都有父类实例函数的副本,影响性能
3、实例继承

	function f1() {
        this.name = null;
        this.sleep = function () {
            return "睡觉";
        }
    }
    function f2() {
        var f = new f1();
        return f;
    }
    var fchild = new f2();
    console.log(fchild);//f1对象
    console.log(fchild instanceof f2);//false
    console.log(fchild instanceof f1);//true
    var ff = f2();

特点:
不限制调用方式,不管是new 子类()还是子类(),返回的对象具有相同的效果
缺点:
实例是父类的实例,不是子类的实例。不支持多继承
4、组合继承

function Mutou() {
        this.name = arguments[0];
        this.make = function () {
            return "制作" + this.name;
        }
    }
    function Bandeng(name) {
        Mutou.call(this, name);
    }
    Bandeng.prototype = new Mutou();
    var ban = new Bandeng("板凳");
    console.log(ban);
    console.log(ban.make());
    console.log(ban instanceof Bandeng);
    console.log(ban instanceof Mutou);

特点:
弥补了方式2 的缺陷
缺点:
生成了两个实例,消耗内存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值