利用中介对象继承

102 阅读1分钟

/* 不占内存 修改Student的prototype对象不会影响到Person的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;
    let stu1 = new Student();
    console.log(stu1);
    /* Person不会被Student.prototype影响 */
    Student.prototype.name = 'zhangsan'
    let p = new Person();
    console.log(p);

    /* 原型链就是一层一层向上找的过程 */
    /* 原型链继承就是利用了上面的这种特性 */