原型链继承

构造函数

/* new 先创造了一个实例化对象,并且把this给了实例化对象per1 */

        let per1=new Person();

        per1.fn();

        function Car(color,price){

            this.color=color;

            this.price=price;

            this.tro=function(){

                document.write(`BMW${this.color}${this.price}会跑`);

            }

        }

        let p1=new Car('蓝色','3w');

        p1.tro();

原型

 /* 每个函数都有一个prototype(原型)属性 */

        /* 是一个指针,指向一个对象 */

        function Person(name, age) {

            this.name = name;

            this.age = age;

        }

        this.prototype.eat = function () {

            document.write(`${this.name}${this.age}会吃饭`);

        }

        Person.prototype.weight='70kg';

        let p1 = new Person('zhangsan', 18);

        p1.eat();

用原型的方法去除空格

原型链继承-实例化对象

/* 构造函数可以理解为一个类 */

        function Person() {

            this.head=1;

            this.foot=2;

        }

        function Student(name,no){

            this.name=name;

            this.no=no;

        }

        /* 让学生类 继承 人类的头和脚 */

        /* Student的prototype指向一个Person的实例 */

        Student.prototype=new Person();

        /* 加上constructor属性,并将这个属性指回原来的构造函数 */

        Student.prototype.constructor=Student;

        let stu1=new Student();

        console.log(stu1);

        /* let p1 = new Person('zhangsan', 30);

        console.log(p1 instanceof Person); */

        /* instanceof 来判断这个实例是不是这个构造函数实例化出来的对象 */

原型链继承-直接继承原型

function Person(){

        }

        Person.prototype.head=1;

        Person.prototype.foot=2;

        let p1=new Person();

        /* 不变的属性都可以写入prototype中 */

        function Student(){

        }

        /* Student想要继承Person的属性 */

        /* 直接继承prototype */

        Student.prototype=Person.prototype;

        let stu1=new Student();

        /* 缺点

           任何对Student.prototype的修改都会反映到Person.prototype

           Student的constructor不会指向自己 */

        Student.prototype.name='zhangsan';

        console.log(p1);

原型链继承-利用空对象作为中介

           空对象,几乎不占内存

           修改prototype对象,不会影响别人的prototype对象 */

        /* 父类 */

        function Person() { }

        Person.prototype.head = 1;

        Person.prototype.foot = 2;

        /* 子类想继承父类的属性 */

        function Student() { }

        /* 创建一个空对象 */

        function f() { }

        /* 吧父类的原型直接赋值给空对象的原型上 */

        f.prototype = Person.prototype;

        /* 把空对象的实例化对象 给到子类的原型上 */

        Student.prototype = new f();

        /* 构造器都是指向自己 */

        Student.prototype.constructor = Student;

        Student.prototype.age = 30;

        function Car() { };

        Car.prototype.color = 'red';

        Car.prototype.price = '3w';

        function extend(child, parent) {

            function kong() { };

            kong.prototype = parent.prototype;

            child.prototype = new kong();

            child.prototype.constructor = child;

        }

封装空对象

        extend(Bmw, Car);

        function Bmw() {

            this.tro = function () {

                document.write(this.color + this.price);

            }

        };

        let qq = new Bmw();

        qq.tro()

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

推荐阅读更多精彩内容