js 4大继承模式

js四大继承方式
2020-05-09 锋芒bq9g6… 转自 初出茅庐小… 私有
修改
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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值