分享

js四大继承方式

 初出茅庐小菜鸟 2019-08-06

1、继承方式一:原型链

Grand.prototype.lastName = 'kj';

     function Grand(){

         this.wife = 'xh';

     }

    Father.prototype= new Grand();

     function Father(){

         this.name = 'gx';

     }

     Son.prototype= new Father();

     function Son(){

        this.name = 'lk';

     }

     var son = new Son();

2、继承方式二: 构造函数(通过call())

function Person(name,age){

         this.name = name;

         this.age = age;

     }

     function Student(name,age,grade){

        Person.call(this,name,age);

         this.grade = grade;

     }

     function Miny(name,age,grade,sex){

         Student.call(this,name,age,grade);

         this.sex = sex;

     }

     var mingy = new Miny('wang',16,17,'nan');

     function A(){

         this.m1='aaa';

     }

     function B(){

         A.call(this);

     }

     var w=new B();

3、第三种方式:共享原型

Father.prototype.age = 30;

     function Father(){

     }

     function Son(){

     }

     Son.prototype = Father.prototype;

     var son = new Son();

     var father = new Father();

4、第四种方式:圣杯模式

person.prototype.name = 'huluwa';

function person(){

}

function student(){

}

//媒介

function inherit(person,student){

function f(){}

f.prototype =  person.prototype;

student.prototype = new f();

student.prototype.constructor = student;

student.prototype.super = person.prototype;

}

inherit(person,student)

var student = new student();

//继承

person.prototype = {

name : 'huluwa';

age : 17;

}

function person(){

}

student.prototype = new person();

function student(){}

var stu = new student();

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多