面向对象的程序设计(二)

2017-04-25  本文已影响0人  aermin

tips:接下去会在github写博客,简书不再更新和修改文章,欢迎大家逛逛我的新博客点击查看 ,我会尽量用更容易理解的方式写好每一篇博客,大家一起学习交流😄。

继承

概念

OO语言中支持两种继承方式:实现继承,接口继承

由于函数没有签名,在ECMAScript中只支持实现继承,而且是依靠原型链来实现的

原型链

[图片上传失败...(image-c69d0c-1522079505212)]

原型链的问题(缺点)

默认的原型

如何确定原型和实例之间的关系

       alert(instance instanceof Object)    //true  
        alert(instance instanceof SuperType)    //true  
        alert(instance instanceof Subtype)    //true  
        alert(Object.prtotype.isPropertyOf(instance))    //true  
        alert(SuperType.prtotype.isPropertyOf(instance))    //true  
        alert(Subtype.prtotype.isPropertyOf(instance))    //true  

定义方法时要注意的几个问题

基本思想:利用原型,让一个引用类型继承另一个引用类型的属性和方法

例子:

function SuperType(){  
    this.SuperProperty = true;  
};  
SuperType.prototype.getSuperProperty = function(){return this.SuperProperty};  
  
function SubType(){  
    this.SubProperty = false;  
};  
SubType.prototype = new SuperType();  
SubType.prototype.getSubProperty = function(){return this.getSubProperty};  
  
var instance = new SubType();  
alert(instance.getSuperProperty()) //true  

借用构造函数

缺点

传递参数

  function SuperType (name) {function   
    this.name= name;  
    }  
    function SubType () {  
    //继承了Super'l'ype, 同时还传递了参数  
    superType.call(this, "Nicholas");  
    //实例属性  
    this.age= 29;}  
    var instance= new SubType();  
    alert(instance.name) ; //“Nicholas” alert(instance.age); //29  
    

基本思想:在子类型构造函数中调用超类型构造函数

    function SuperType(){  
    this.color = ["red","blue","yellow"]      
};  
function SubType(){  
    SuperType.call(this);  
};  
  

组合继承

概念

优点:既通过在原型上定义方法实现了函数的复用,又能够保证每个实例都有它自己的属性

组合继承避免了原型链和借用构造函数的缺陷,融合了它们的优点,成为javascript中最常用的继承模式

function SuperType(name){

    this.name = name;  
    this.color = ["red","blue","yellow"]  
};  
SuperType.prototype.sayName = function(){alert(this.name)};  
function SubType(name){  
    SuperType.call(this,name);  
    this.age = age;  
};  
SubType.prototype = new SuperType();  
SubType.prototype.constructor = SubType;  
SubType.prototype.sayAge = function(){alert(this.age)};  

原型式继承

Object.creat()

     var person = {name:"Nick" ,friends:["Tom","Sam"]};  
        var anotherPerson =Object.create(person);  
       var person = {name:"Nick" ,friends:["Tom","Sam"]};  
        var anotherPerson =Object.create(person,{  
            name:{  
            value:"Greg"  
            }  
        });  

核心

function object(o){  
    function F(){};  
    F.prototype = o;  
    return new F();  
}  

例子

var person = {name:"Nick" ,friends:["Tom","Sam"]};  
var anotherPerson =object(person);  
//person成为了anotherPerson的原型  

寄生式继承

寄生式继承的思路与寄生构造函数和工厂模式类似,即创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真地是它做了所有工作一样返回对象。

缺点:不能实现函数复用

function creatAnother(original){  
    var clone = object(original);  
    clone.sayHi = function(){alert("Hi")};  
    return clone;  
}  

在主要考虑对象而不是自定义类型和构造函数的悄况下,寄生式继承也是一种有用的模式。

寄生组合式继承

所谓寄生组合式继承,即通过借用构造函数来继承属性, 通过原型链的混成形式来继承方法。

基本思路: 不必为了指定子类型的原型而调用超类型的构造函数,我们所需要的无非就是超类型原型的一个副本而已。本质上,就是使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。

目的:解决组合继承模式中调用两次超类型构造函数,导致属性重写,影响性能的问题

function SuperType(name){  //超类型构造函数  
        this.name=name;  
        this.color={"red","blue","green"]  
      }  
     SuperType.prototype.sayName=function(){  
     alert(this.name);  
     function SubType(name,age){  
         SuperType.call(this,name) //第二次调用  
         this.age = age;  
     }  
     SubType.prototype = new SuperType(); //第一次调用  
     SubType.prototype.construtor =SuperType;  
     SubType.protptype.sayAge =function(){  
         alert(this.age)  

这个例子高效体现在它只调用了一次SuperType的构造函数,并且避免了再prototype上创建不必要的、多余的属性。同时还能保证原型链不变

   function inheritPrototype(subType,superType){  
    //prototype成为superType的一个副本,而不用调用构造函数  
    var prototype = SuperType.prototype;  
    prototype.constructor = SubType;  
    SubType.prototype = prototype;  
};  
  

例子

function SuperType(name){  
    this.name = name;  
};  
SuperType.prototype.sayName = function(){alert(this.name)};  
function SubType(name,age){  
    SuperType.call(this,name);  
    this.age = age;  
}  
inheritPrototype(Subtype,SuperType);  
SubType.prototype.sayAge = function(){alert(this.age)};  

    function inheritPrototype(SubType,SuperType){   
      var prototype = object(SuperType.prototype);//创建对象  
     prototype.constructor = subType;//增强对象  
      subType.prototype = prototype ;//指定对象  
  }  
上一篇 下一篇

猜你喜欢

热点阅读