04.第6章:面向对象的程序设计

1.构造函数

任何一个函数只要被new使用了,就是构造函数。
构造函数用来存放每一个实例自己具有的属性和方法。

<script>
    function Person(name,age){
        //每一个实例有自己的名字和年龄,而不是所有的实例共享同一个名字和年龄
        this.name=name;
        this.age=age;
    }
    let p=new Person('abc',20);
    console.log(p);
</script>

可以看到上面创建的P实例对象,具有__proto__属性,指向的对象就是原型对象。同时构造函数Person中prototype属性指向的也是该原型对象,完全全等。

2.原型对象原型链

  • 任何一个构造函数在创建的时候,都有一个prototype属性,该属性指向原型对象。
  • 任何一个引用对象(数组、对象、函数),都有一个__proto__属性,该属性也指向原型对象。
  • 原型对象中有constructor属性,指向构造函数;有__proto__属性指向父类的原型对象。

原型对象中有一个constructor属性,指向构造函数Person(name,age)。
原型对象也有__proto__,指向其父类的原型对象,这里是指向Object的原型对象。

原型对象用来存放所有实例共享的属性和方法。

<script>
    function Person(name,age){
        //每一个实例有自己的名字和年龄,而不是所有的实例共享同一个名字和年龄
        this.name=name;
        this.age=age;
    }
   //在原型对象中创建共享的方法
    Person.prototype.getName=function(){
      return this.name;
    };

    let p=new Person('abc',20);
    //当在实例中找不到调用的属性或方法时,就会沿着__proto__属性指向的原型对象一层层的向上查找。(原型链)
    //这里调用的是原型对象中的方法
    console.log('name:'+p.getName());
    console.log(p);

    //在p实例对象的__proto__的__proto__中查找,
    //也就是Object构造函数的prototype指向的原型对象,
    //这个Object.prototype.__proto__===null。避免死循环
    p.toString();
</script>

3.instanceof

用于判断引用类型的实例是属于哪个构造函数的方法。

原理:

  • 判断实例的__proto__指向的原型对象(隐试原型)与构造函数的prototype属性所指向的原型对象(显式原型)是否是同一个。


  • 子类型的实例instanceof父类型的构造函数,也会返回true


  • 要更加详细的识别是子类型的实例
实例对象.__proto__.constructor===构造函数

4.继承的实现方式

1.使用构造函数实现继承

<script>
    /*1.使用构造函数实现继承*/
    function Father(name,age){
        this.name=name;
        this.age=age;
    }
    //父类原型对象中的方法
    Father.prototype.getName=function(){
      console.log(this.name);
    };

    //父类原型对象中的方法
    Father.prototype.getAge=function(){
        console.log(this.age);
    };

    function Son(name,age){
        Father.call(this,name,age);
    }

    let son =new Son('abc',13);
    console.log(son);

</script>

缺点:
只能继承父类实例的属性和方法,不能继承父类原型对象中的属性和方法。

2.使用原型链继承

<script>
    /*1.使用构造函数实现继承*/
    function Father(name,age){
        this.name=name;
        this.age=age;
    }
    //父类原型对象中的方法
    Father.prototype.getName=function(){
      console.log(this.name);
    };

    //父类原型对象中的方法
    Father.prototype.getAge=function(){
        console.log(this.age);
    };

    function Son(name,age){
        //Father.call(this,name,age);
    }

    //使用原型链实现继承,将子类构造函数的prototype指向的原型对象赋值
    //为父类的一个实例。
    Son.prototype=new Father();


</script>

缺点:父类所有的属性和方法,在子类中都成了原型对象中的属性和方法。

3.组合式继承
使用call方法来继承构造函数中的属性和方法,
使用原型链来继承原型对象的属性和方法。

<script>
    /*1.使用构造函数实现继承*/
    function Father(name,age){
        this.name=name;
        this.age=age;
    }
    //父类原型对象中的方法
    Father.prototype.getName=function(){
      console.log(this.name);
    };

    //父类原型对象中的方法
    Father.prototype.getAge=function(){
        console.log(this.age);
    };

    function Son(name,age){
        Father.call(this,name,age);
    }

    //使用原型链实现继承,将子类构造函数的prototype指向的原型对象赋值
    //为父类的一个实例。
    Son.prototype=new Father();


</script>

缺点:子类的原型对象中有父类构造函数中的属性和方法,同时子类的实例中也有父类构造函数中的属性和方法,继承了两次。

4.改进的组合继承1

<script>
    /*1.使用构造函数实现继承*/
    function Father(name,age){
        this.name=name;
        this.age=age;
    }
    //父类原型对象中的方法
    Father.prototype.getName=function(){
      console.log(this.name);
    };

    //父类原型对象中的方法
    Father.prototype.getAge=function(){
        console.log(this.age);
    };

    function Son(name,age){
        Father.call(this,name,age);
    }

    //使用原型链实现继承,将子类构造函数的prototype指向的原型对象赋值
    //为父类构造函数的原型对象。
    Son.prototype=Father.prototype;


</script>

缺点:用了父类的原型对象,父类原型对象中的constructor指向父类的构造函数。

5.改进的组合继承2

<script>
    /*1.使用构造函数实现继承*/
    function Father(name,age){
        this.name=name;
        this.age=age;
    }
    //父类原型对象中的方法
    Father.prototype.getName=function(){
      console.log(this.name);
    };

    //父类原型对象中的方法
    Father.prototype.getAge=function(){
        console.log(this.age);
    };

    function Son(name,age){
        Father.call(this,name,age);
    }

    //使用原型链实现继承,将子类构造函数的prototype指向的原型对象赋值
    //为父类构造函数的原型对象的副本。
    Son.prototype=Object.create(Father.prototype);
    //设置constructor为父类的构造函数
    Son.prototype.constructor=Son;


</script>



5.new关键字的执行过程

<script>
    function Person(name,age) {
        this.name=name;
        this.age=age;
    }

    function mynew(fn,name,age){
        //1.创建一个继承自构造函数原型对象的空对象
        let o=Object.create(fn.prototype);
        //2.调用构造函数为该对象赋值
        let k=fn.call(o,name,age);
        //3.如果构造函数中返回的k是对象,则返回k
        if(typeof k==='object')
            return k;
        //4.如果不是返回o
        else
            return o;

    }

    let person=mynew(Person,'abc',13);
    console.log(person);

</script>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容