1. function Teacher() {
    2. this.name = 'Ms';
    3. this.age = 18;
    4. }
    5. const t = new Teacher();
    6. Teacher.prototype.car = 't1'
    7. function Student() {
    8. this.name = 'jack';
    9. this.age = 22;
    10. }
    11. Student.prototype = Teacher.prototype;
    12. Student.prototype.color = 'red'
    13. const s = new Student();
    14. console.log(s);
    15. console.log(t);

    通过这种方法改变原型的指向存在问题,当我们改变student原型的时候,teacher的原型也发生改变
    javascript纯面向对象开发需要使用到的一个模式,来对对象之间原型继承做中间层代理避免重复继承与代码杂乱
    我们需要制造一个中间层来改变这一现状

    1. function Teacher() {
    2. this.name = 'Jack'
    3. }
    4. Teacher.prototype.color = 'red';
    5. function Student() {
    6. this.name = 'Tom';
    7. }
    8. function Stop() {}
    9. Stop.prototype = Teacher.prototype;
    10. const stop = new Stop();
    11. Student.prototype = stop;
    12. Student.prototype.style = 'cute';
    13. const s = new Student();
    14. const t = new Teacher();
    15. console.log(s);
    16. console.log(t);

    这样就可以改变 被称之
    原理如下
    image.png
    封装圣杯模式

    1. // 圣杯模式模块化封装
    2. let inherit = (function (){
    3. let Buffer = function (){};
    4. return function (Target,Origin) {
    5. Buffer.prototype = Origin.prototype;
    6. Target.prototype = new Buffer();
    7. Target.prototype.constructor = Target;
    8. Target.prototype.super_class= Origin;
    9. }
    10. })();
    11. function Teacher(){}
    12. Teacher.prototype.name = 'Jack';
    13. function Student(){}
    14. function Butter(){}
    15. inherit(Student,Teacher);
    16. Student.prototype.age = 19;
    17. let s= new Student();
    18. let t = new Teacher();
    19. console.log(s)
    20. console.log(t)