JS中的继承

JS继承的实现方式
既然要实现继承,那么首先我们得有一个父类,代码如下:

// 定义一个动物类
function Animal (name) {
  // 属性
  this.name = name || 'Animal';
  // 实例方法
  this.sleep = function(){
    console.log(this.name + '正在睡觉!');
  }
}
// 原型方法
Animal.prototype.eat = function(food) {
  console.log(this.name + '正在吃:' + food);
};

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

function Cat(){ 
}
Cat.prototype = new Animal();
Cat.prototype.name = 'cat';
var cat = new Cat();
console.log(cat.name);
console.log(cat.eat('fish'));
console.log(cat.sleep());
console.log(cat instanceof Animal); //true 
console.log(cat instanceof Cat); //true

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

function Cat(name){
  Animal.call(this);
  this.name = name || 'Tom';
}
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // false
console.log(cat instanceof Cat); // true

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

function Cat(name){
  var instance = new Animal();
  instance.name = name || 'Tom';
  return instance;
}
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // false

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

function Cat(name){
Animal.call(this);
  this.name = name || 'Tom;
}
Cat.prototype=new Animal();
var cat = new Cat();
console.log(cat.name);
console.log(cat.sleep());
console.log(cat instanceof Animal); // true
console.log(cat instanceof Cat); // true

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

/*
         * js里面的继承
         * 概念 :继承  子类继承父类
         * 做面向对象的开发以及框架库的封装
         * 举例说明继承
         * 声明一个父类
         * 声明动物类  属性和行为
         *
         * 声明一个子类对象
         * 面向对象 有三大特征: 封装  继承  多态
         *
         *
         * 子类继承父类  第一种方式   原型链继承
         * */
    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 = "犬科";
}
    //原型链继承  继承完成之后  __proto__
    Dog.prototype = new Animal();

    /*
     * 原型链追加
     * */
    Dog.prototype.color = "red";
    var dog = new Dog();
    /*检测对象的类型的*/
    console.log(typeof dog);
    console.log(dog instanceof Dog);//true
    console.log(dog instanceof Animal);//true
    console.log(dog);
    /*
     * 实例化对象
     * */
    /*var animal=new Animal();
     console.log(animal);*/

    /*var array=new Array();
     console.log(array);*/

    //在数组上封装一个方法去调用执行  进行数组的排序
    //    Array.prototype.mySort=function (){
    //
    //    }
    //    var a=[1,3,2,4,0];
    //    a.mySort();


    /*
     * 构造继承
     * call   apply  对象指针的替换
     *
     * 区别:  传递的参数的方式不一样
     * call 有多个参数
     * apply 有两个参数
     * */
    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);
    console.log(small.eat());
    console.log(small.writezuoye());


    /*
     * 实例继承  new对象返回对象
     * 不能实现多继承
     * */
    function f1() {
        this.name = null;
        this.sleep = function () {
            return "睡觉";
        }
    }

    function f2() {
        var f = new f1();
        return f;
    }
    var fchild = new f2();
    console.log(fchild);
    console.log(fchild instanceof f2);//false
    console.log(fchild instanceof f1);//true

    var ff = f2();

    /*
     * 组合继承  去弥补原型链继承和构造继承的缺点
     * */

    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);


    console.log(window);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值