JS继承

①什么是继承
去一个对象里面取一个属性,如果自己没有就去父级里面找,找到这个属性之后会继承下来成为这个对象继承下来的属性。
②js为什么要使用继承
继承的概念是在面向对象编程里面提出来的,属于面向对象的一大特点。面向对象编程思想将世界中的人或物静态属性用对象属性来模拟,现实世界的动态效果用对象的方法来模拟。面向对象的三大特点:封装,继承,多态。
继承将共用的东西提出来作为父类的属性方法,继承到所有的子类当中使用,做到代码的可重用性。js响应面向对象编程有了继承。
③继承的种类

//父级
//构造函数实现
function Parent(name,age){
     this.name = name || '';
     this.age = age;
     this.money = [1000];
     this.sayHello = function(){
         console.log('我的名字是'+this.name+',我今年'+this.age+'岁了');
        }
}
Parent.prototype.money = function(money){
     this.money.push(money);
}
//传统继承
function Son(name,age){
     this.name = name;
     this.age = age;
}
Son.prototype = new Parent('王','18');
var son1 = newSon('静','19');
console.log(son1.name,son1.age);//静,19
son1.sayHello();//我的名字是静,我今年19岁了
console.log(son1.money);//[1000];
son1.makeMoney(2000);
console.log('son1.money after:',son1.money);[1000,2000]
var son2 = new Son('筱','20');
console.log(son2.money);[1000,2000]
console.log(son1 instanceof Son);//true
console.log(son1 instanceof Parent);//true;
//优点:实例是子类的实例,也属于父级的实例子类可以访问到父类及父类原型上的属性和方法;
//缺点:原型对象上的引用属性,多有实例都是共享的,在一个实例上修改,所有实例都跟着变化;不能够同时继承多个父类;
//借用构造函数的继承
function Son(name,age){
     Parent.call(this);
     this.name = name;
     this.age = age;
}
var son1 = newSon('静','19');
console.log(son1.name,son1.age);//静,19
son1.sayHello();//我的名字是静,我今年19岁了
console.log(son1.money);//[1000];
son1.makeMoney(2000);//error
console.log(son1 instanceof Son);//true
console.log(son1 instanceof Parent);//false;
//优点:解决了传统继承中共享属性问题,创建子类实例可以向父级传递参数可以继承多个父类
//缺点:只能继承父类构造函数里面的属性方法,无法实现函数复用每个子类都有父类实例的副本,实例只是子类的实例不是父类的实例
//组合继承,通过调用父类的构造函数,继承父类构造函数的属性方法,将父类实例作为子类原型
//继承父类原型上的属性和方法,实现函数复用
function Son(name,age){
     Parent.call(this,name,age);
     }
     Son.prototype = new Parent('王','18');
var son1 = newSon('静','19');
console.log(son1.name,son1.age);//静,19
son1.sayHello();//我的名字是静,我今年19岁了
console.log(son1.money);//[1000];
son1.makeMoney(2000);
console.log('son1.money after:',son1.money);[1000,2000]
var son2 = new Son('筱','20');
console.log(son2.money);[1000]
console.log(son1 instanceof Son);//true
console.log(son1 instanceof Parent);//true;
//优点:继承多父类,父类原型和构造函数里的方法都可继承,函数可以复用,父类的引用值不会共享
//缺点:调用了两次父类构造函数,产生了两份父类实例,消耗了内存
//循环拷贝继承
function Son(name,age){
    var p = new Parent(name,age);
    for(var prop in p){
    Son.prototype[prop] = p[prop];
    }
}
var son1 = newSon('静','19');
console.log(son1.name,son1.age);//静,19
son1.sayHello();//我的名字是静,我今年19岁了
console.log(son1.money);//[1000];
son1.makeMoney(2000);
console.log('son1.money after:',son1.money);[1000,2000]
var son2 = new Son('筱','20');
console.log(son2.money);[1000]
console.log(son1 instanceof Son);//true
console.log(son1 instanceof Parent);//false;
//优点:继承多个父类,继承父类所有属性和方法
//缺点:效率低实力不属于父类实例
//多继承
function Cooker(){
this.food = ['馒头','花卷']this.eating = function(){
    console.log('吃'+this.food);
   }
}
function Son(name,age){
     Parent.call(this,name,age);
     Cooker.call(this);
     }
     for(prop in Parent.prototype){
     Son.prototype[prop] = Parent.prototype[prop];
     }
      for(prop in Cooker.prototype){
     Son.prototype[prop] = Cooker.prototype[prop];
     }
     
     Son.prototype = new Parent('王','18');
var son1 = newSon('静','19');
console.log(son1.name,son1.age);//静,19
son1.sayHello();//我的名字是静,我今年19岁了
son1.eating();//吃馒头,花卷
console.log(son1.money);//[1000];
son1.makeMoney(2000);
console.log('son1.money after:',son1.money);[1000,2000]
var son2 = new Son('筱','20');
console.log(son2.money);[1000]
console.log(son1 instanceof Son);//true
console.log(son1 instanceof Parent);//false;
//优点:继承多个父类,继承所有属性方法
//缺点:效率低,实例不属于父类实例
//共享原型继承
Parent.prototype.money = [2000];
function Son(name,age){
     Parent.call(this,name,age);
     }
     Son.prototype = Parent.protitype;
var son1 = newSon('静','19');
console.log(son1.name,son1.age);//静,19
son1.sayHello();//ERROR:SON1.SAYHELLO IS NOT A FUNCTION;
console.log(son1.money);//[2000];
son1.makeMoney(2000);//ERROR:PUSH UNDEFIEND
console.log('son1.money after:',son1.money);[2000,3000]
var son2 = new Son('筱','20');
console.log(son2.money);[1000]
console.log(son1 instanceof Son);//true
console.log(son1 instanceof Parent);//true;
//优点:简单;
//缺点:只能继承父类原型属性方法,子类共享原型上的属性
//圣杯模式继承
function Son(name,age){
     Parent.call(this,name,age);
     }
     function inherit(Children,Parent){
     funtion Temp(){}
     Temp.prototype = Parent.prototype();
     Children.prototype = new Temp();
     Children.prototype.constructor = Children;
     Children.prototype.uber = Parent;
    
}
inherit(Son,Parent);
var son1 = newSon('静','19');
console.log(son1.name,son1.age);//静,19
son1.sayHello();//我的名字是静,我今年19岁了
console.log(son1.money);//[1000];
son1.makeMoney(2000);
console.log('son1.money after:',son1.money);[1000,2000]
var son2 = new Son('筱','20');
console.log(son2.money);[1000]
console.log(son1 instanceof Son);//true
console.log(son1 instanceof Parent);//true;
//实现复杂不能实现多继承

最好的方式是圣杯模式继承和js循环拷贝两种

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值