ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

day28 继承

2021-10-13 16:02:50  阅读:159  来源: 互联网

标签:function console day28 继承 log Student id name


1、原型继承

    如何实现元素继承

    让子类的原型对象等于父类的任意实例化对象

    实现继承的作用:

    1.子类可以直接使用父类派生的属性和方法

    2.子类可以添加自己新的属性和方法

    子类对象h,是如何访问父类的属性,父类原型方法,子类的属性及子类的原型方法

    原型链图

    向上找:在访问属性或者方法是,首先在子类中查找,如果没有找到,则一层一层向父类查找

    如果最上层未找到则报错

<script>
    function Animal(name){
        this.name=name;
    }
    Animal.prototype.eat=function(){
        console.log("Animal eat")
    }
    function Human(id){
        this.id=id;
    }
    Human.prototype=new Animal("name");
    Human.prototype.study=function(){
        console.log("Human study")
    }
    let h=new Human(1)
    h.name="name1";
    console.log(h.name)
    h.eat();
    console.log(h.id);
    h.study();
</script>

2、原型链继承

<script>
    function Animal(name){
        this.name=name;
    }
    Animal.prototype.eat=function(){
        console.log("eat");
    }
    function Human(id){
        this.id=id;
    }
    Human.prototype=new Animal("name");
    Human.prototype.makeTools=function(){
        console.log("制作工具")
    }
    function Studnet(score){
        this.score=score;
    }
    Student.prototype=new Human(1);
    Student.prototype.study=function(){
        console.log("study");
    }
    let s=new Student("100");
    console.log(s.name,s.id,s.score);
    s.eat();
    s.makeTools();
    s.sudy();
</script>

3、原型继承的缺陷

    注意事项:

    1.为子类对象添加原型方法时,必须先实现继承关系

    2.由父类派生给子类的属性,是无法在子类对象构造时,进行初始化的

    3.一旦实现了继承关系,则子类的原型对象的值就不能再修改了

4、深拷贝和浅拷贝

<script>  
  function Student(id, name) {
        this.id = id;
        this.name = name;
    }

    //克隆一个调用该函数的对象
    Student.prototype.clone = function() {
        //开辟空间
        let item = new Student(this.id, this.name);

        return item;
    }

    let s1 = new Student(1, "高洋");
    let s2 = s1.clone();

    s2.name = "赵四";

    console.log(s1, s2);
</script>

    拷贝:用已有对象初始化一个新的对象

    let a=123;

    let b=a;

    内置类型和引用类型在内存中存储的区别:

    内置基本类型只有一块栈空间,存储的就是数据本身

    引用类型有凉快空间,一块是栈空间,存储堆空间的地址,

    另一块是堆空间,存储真正的数据

    深浅拷贝:只针对于引用类型

    浅拷贝:只拷贝引用地址,但是并未开辟空间

    深拷贝:开辟空间且赋值

5、apply和call继承

    apply和call继承的作用:用来解决原型继承时,无法初始化由父类派生给子类的属性
    apply和call的继承的缺陷,不能继承父类原型对象上的属性和方法

<script>
    // apply和call继承的作用:用来解决原型继承时,无法初始化由父类派生给子类的属性
    // apply和call的继承的缺陷,不能继承父类原型对象上的属性和方法
    function Human(id,name){
        this.id=id;
        this.name=name;
    }
    function Student(id,name,score){
        // 借用构造方法
        Human.call(this,id,name);
        this.score=score;
    }
    let s=new Student(1,"name1",100);
    console.log(s);
    s.eat();
</script>

6、混合继承

<script>
    // 混合继承:1.父类属性通过apply和call继承
    //          2.原型属性和方法,通过原型对象继承
    function Anmial(id,name){
        this.name=name;
        this.id=id;
    }
    Animal.prototype.eat=function(){
        console.log("animal eat");
    }
    function Student(id,name,score){
        // 属性的继承
        Animal.apply(this,[id,name]);
        this.score=score;
    }
    // 原型对象上的方法和属性继承
    Student.prototype=new Animal();
    Student.prototype.study=function(){
        console.log("Student study");
    }
    let s=new Student(1,"name1",100);
    console.log(s);
    s.eat();
    s.study();
</script>

7、ES6继承方法

<script>
    class Animal{
        //constructor构造方法
        constructor(id,name){
            this.id=id;
            this.name=name;
        }
        eat(){
            console.log("Animal eat");
        }
    }
    // extends继承的关键字
    // super:借用父类的构造方法
    class Student extends Animal{
        constructor(id,name,score){
            super(id,name);//这句话必须放在第一行
            this.score=score;
        }
        study(){
            console.log("Student study");
        }
    }
    let a=new Animal(1,"name1");
</script>

标签:function,console,day28,继承,log,Student,id,name
来源: https://blog.csdn.net/serendipitymm/article/details/120742832

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有