欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

实例介绍JavaScript中多种组合继承

程序员文章站 2023-11-03 19:29:22
1. 组合继承:又叫伪经典继承,是指将原型链和借用构造函数技术组合在一块的一种继承方式。 下面来看一个例子: function supertype(name)...

1. 组合继承:又叫伪经典继承,是指将原型链和借用构造函数技术组合在一块的一种继承方式。

下面来看一个例子:

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.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(); //29

 

 var instance2 = new subtype("greg", 27);

 alert(instance2.colors); //red,blue,green

 instance2.sayname(); //greg

 instance2.sayage(); //27

组合继承避免了原型链和借用构造函数的缺陷,融合它们的优点。

2. 原型式继承

可以在不必预先定义构造函数的情况下实现继承,其本质是执行对给定对象的浅复制。而复制得到的副本还可以得到进一步的改造。

function object(o) {

  function f(){};

  f.prototype = o;

  return new f;

 }

 

 var person = {

  name: "nicholas",

  friends: ["shelby", "court", "van"]

 };

 

 var antherperson = object(person);

 antherperson.name = "greg";

 antherperson.friends.push("rob");

 

 var antherperson = object(person);

 antherperson.name = "linda";

 antherperson.friends.push("barbie");

 

 alert(person.friends); //shelby,court,van,rob,barbie

3. 寄生式继承

与原型式继承非常相似,也是基于某个对象或某些信息创建一个对象,然后增强对象,最后返回对象。为了解决组合继承模式由于多次调用超类型构造函数而导致的低效率问题,可以将这个模式与组合继承一起使用。

function object(o) {

  function f(){};

  f.prototype = o;

  return new f;

 }

 function createanother(original) {

  var clone = object(original);

  clone.sayhi = function() {

   alert("hi");

  };

  return clone;

 }

 

 var person = {

  name: "nicholas",

  friends: ["shelby", "court", "van"]

 };

 

 var anotherperson = createanother(person);

 anotherperson.sayhi();

4. 寄生组合式继承

集寄生式继承和组合继承的优点与一身,是实现基本类型继承的最有效方式。

//继承原型

 function extend(subtype, supertype) {

  function f(){};

  f.prototype = supertype.prototype;

 

  var prototype = new f;

  prototype.constructor = subtype;

  subtype.prototype = prototype;

 }

 

 //超类方法

 function supertype(name) {

  this.name = name;

  this.colors = ["red", "blue", "green"];

 }

 supertype.prototype.sayname = function() {

  return this.name;

 }

 

 //子类方法

 function subtype(name, age) {

  supertype.call(this, name);

  this.age = age;

 }

 

 //继承超类的原型

 extend(subtype, supertype);

 

 //子类方法

 subtype.prototype.sayage = function() {

  return this.age;

 }

 

 var instance1 = new subtype("shelby");

 var instance2 = new subtype("court", 28);

 

 instance1.colors.push('black');

 

 alert(instance1.colors); //red,blue,green,black

 alert(instance2.colors); //red,blue,green

 

 alert(instance1 instanceof subtype); //true

 alert(instance1 instanceof supertype); //true

这段例子的高效率体现在它只调用了一次supertype构造函数,并且因此避免了在subtype.prototype上面创建不必要的多余的属性。与此同时,原型链还能保持不变。因此,还能正常使用instanceof 和 isprototypeof()。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。