javascript如何实现面向对象和继承机制

来源:互联网 发布:评价金庸小说 知乎 编辑:程序博客网 时间:2024/06/14 04:58

面向对象:

function MyObject(){    this.name="myObject";    this.type="class";    this.methodA=function(){        alert(this.name);    }    this.methodB=function(){        return this.type;    }}var myObject=new MyObject();myObject.methodA();var type=myObject.methodB();alert(type);

继承机制:
1.原型链方式:
基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法。
实现原型链有一种基本模式,其代码大致如下。

function SuperType(){    this.property=true;}SuperType.prototype.getSuperValue=function(){    return this.property;};function SubType(){    this.subproperty=false;}//继承了SuperTypeSubType.prototype=new SuperType();SubType.prototype.getSubValue=function(){    return this.subproperty;};var instance=new SubType();alert(instance.getSuperValue); //true

2.借用构造函数:
基本思想是在子类型构造函数的内部调用超类型构造函数。函数只不过是在特定环境中执行代码的对象,因此通过使用apply()和call()方法也可以在(将来)新创建的对象上执行构造函数,如下所示:

function SuperType(){    this.colors=["red","blue","green"];}function SubType(){    //继承了SuperType    SuperType.call(this);}var instance1=new SubType();instance1.colors.push("black");alert(instance1.colors); //"red,blue,green,black"var instance2=new SubType();alert(instance2.colors); //"red,blue,green"

3.组合继承:
组合继承,有时候也叫伪经典继承,指的是将原型链和借用构造函数的技术组合到一起,从而发挥二者之长的一种继承模式。其背后的思路是使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。这样,既通过在原型上定义方法实现了函数复用,又能够保证每个实例都有它自己的属性。例如:

function SuperType(name){    this.name=name;    this.colors=["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.constructor=SubType;SubType.prototype.sayAge=function(){    alert(this.age);};var instance1=new SubType("Nicholas",29);instance1.colors.push("black");alert(instance1.colors); //"red,blue,green,black"instance1.sayName(); //"Nicholas";instance1.sayAge(); //29var instance2=new SubType("Greg",27);alert(instance2.colors); //"red,blue,green"instance2.sayName(); //"Greg";instance2.sayAge(); //27

4.原型式继承:
借助原型可以基于已有的对象创建新对象,同时还不必因此创建自定义类型。(可以在不必预先定义构造函数的情况下实现继承,其本质是执行对给定对象的浅复制。而复制得到的副本还可以得到进一步改造。)例如:

var person={    name:"Nicholas",    friends:["Shelby","Court","Van"]};var anotherPerson=object(person);anotherPerson.name="Greg";anotherPerson.friends.push("Rob");var yetAnotherPerson=object(person);yetAnotherPerson.name="Linda";yetAnotherPerson.friends.push("Barbie");alert(person.friends); //"Shelby,Court,Van,Rob,Barbie"

5.寄生式继承:
寄生式继承的思路与寄生构造函数和工厂模式类似,即创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真的是它做了所有工作一样返回对象。(为了解决组合继承模式由于多次调用超类型构造函数而导致的低效率问题,可以将这个模式与组合继承一起使用。)例如:

function creatAnother(original){    var clone=object(original); //通过调用函数创建一个个新对象    clone.sayHi=function(){ //以某种方式来增强这个对象        alert("hi");    };    return clone; //返回这个对象}

在这个例子中,creatAnother()函数接收了一个参数,也就是将要作为新对象基础的对象。然后,把这个对象(original)传递给object()函数,将返回的结果赋值给clone。再为clone对象添加一个新方法sayHi(),最后返回clone对象。可以像下面这样来使用creatAnother()函数:

var person={    name:"Nicholas",    friends:["Shelby","Court","Van"]};var anotherPerson=creatAnother(person);anotherPerson.sayHi(); //"hi"

使用寄生式继承来为对象添加函数,会由于不能做到函数复用而降低效率;这一点与构造函数模式类似。

6.寄生组合式继承:
即通过借用构造函数来继承属性,通过原型链的混成形式来继承方法。其背后的基本思路是:不必为了指定子类型的原型而调用超类型的构造函数,我们所需要的无非就是超类型原型的一个副本而已。本质上,就是使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。(集寄生式继承和组合继承的优点于一身,是实现基于类型继承的最有效方式。)寄生组合式继承的基本模式如下所示:

function inheritPrototype(subType,superType){    var prototype=Object(superType.prototype); //创建对象    prototype.constructor=subType; //增强对象    subType.prototype=prototype; //指定对象}
0 0