当前位置: 移动技术网 > IT编程>开发语言>JavaScript > javascript的几种继承方法介绍

javascript的几种继承方法介绍

2019年07月31日  | 移动技术网IT编程  | 我要评论

1.原型链继承:构造函数、原型和实例的关系:每个构造函数都有一个原型对象,原型对象都包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。确认原型和实例之间的关系用instanceof。

原型链继承缺点:字面量重写原型会中断关系,使用引用类型的原型,并且子类型还无法给超类型传递参数

function parent(){
    this.name='mike';
  }
  function child(){
    this.age=12;
  }
  //儿子继承父亲(原型链)
  child.prototype=new parent();//child继承parent,通过原型形成链条
  var test=new child();
  console.log(test.age);
  console.log(test.name);//得到被继承的属性
  //孙子继续原型链继承儿子
  function brother(){
    this.weight=60;
  }
  brother.prototype=new child();//继承原型链继承
  var brother=new brother();
  console.log(brother.name);//继承了parent和child,弹出mike
  console.log(brother.age);//12

  console.log(brother instanceof child);//ture
  console.log(brother instanceof parent);//ture
  console.log(brother instanceof object);//ture

2.构造函数实现继承:又叫伪造对象或经典继承。
构造函数实现继承缺点:借用构造函数虽然解决了原型链继承的两种问题,但没有原型,则复用无从谈起,所以需要原型链+借用构造函数模式。

function parent(age){
    this.name=['mike','jack','smith'];
    this.age=age;
  }
  function child(age){
    parent.call(this,age);//把this指向parent,同时还可以传递参数
  }
  var test=new child(21);
  console.log(test.age);//21
  console.log(test.name);

  test.name.push('bill');
  console.log(test.name);//mike,jack,smith,bill

3.组合继承:使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。这样即通过在原型上定义方法实现了函数复用,又保证每个实现都有它自己的属性。

缺点:无论什么情况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在创建子类型原型的时候,另一次是在子类型构造函数内部。

function parent(age){
    this.name=['mike','jack','smith'];
    this.age=age;
  }
  parent.prototype.run=function(){
    return this.name+' are both '+this.age;
  }
  function child(age){
    parent.call(this,age);//给超类型传参,第二次调用
  }
  child.prototype=new parent();//原型链继承,第一次调用
  var test1=new child(21);//写new parent(21)也行
  console.log(test1.run());//mike,jack,smith are both 21

  var test2=new child(22);
  console.log(test2.age);
  console.log(test1.age);
  console.log(test2.run());
  //这样可以使test1和test2分别拥有自己的属性age同时又可以有run方法

4.原型式继承:借助原型可以基于已有的对象创建新对象,同时还不必因此创建自定义类型。它要求必须有一个对象可以作为另一个对象的基础。

function object(o){
    function f(){};
    f.prototype=o;
    return new f();
  }
  var person={
    name:'nicho',
    friends:['shell','jim','lucy']
  }
  var anotherperson = object(person);
  anotherperson.name = 'greg';
  anotherperson.friends.push('rob');
  console.log(anotherperson.friends);//["shell", "jim", "lucy", "rob"]

  var yetanotherperson = object(person);
  yetanotherperson.name = 'linda';
  yetanotherperson.friends.push('barbie');
  console.log(yetanotherperson.friends);//["shell", "jim", "lucy", "rob", "barbie"]

  console.log(person.friends);//["shell", "jim", "lucy", "rob", "barbie"]

ecmascript5通过新增object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和(可选的)一个为新对象定义属性的对象。

var person2={
    name:'nicho',
    friends:['shell','jim','lucy']
  };
  var anop2=object.create(person2);
  anop2.name="greg";
  anop2.friends.push('rob');
  console.log(anop2.friends);//["shell", "jim", "lucy", "rob"]

  var yetp2=object.create(person2);
  yetp2.name="linda";
  yetp2.friends.push('barbie');
  console.log(yetp2.friends);//["shell", "jim", "lucy", "rob", "barbie"]

  console.log(person2.friends);//["shell", "jim", "lucy", "rob", "barbie"]
  /*以这种方式指定的任何属性都会覆盖原型对象上的同名属性。*/
  var threep=object.create(person,{
    name:{value:'red'}
  });
  console.log(threep.name);//red,如果threep中无name则输出person2里的name值nicho

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

function object(o){
    function f(){};
    f.prototype=o;
    return new f();
  };
  function createanother(o){
    var cl=object(o);
    cl.sayhi=function(){
      console.log('hi');
    }
    return cl;
  };
  var person={
    name:'nick',
    friends:['shelby','court','van']
  }
  var anotherperson=createanother(person);
  anotherperson.sayhi();//hi
  console.log(anotherperson.name);//nick
  console.log(anotherperson.friends);//["shelby", "court", "van"]

  /*这个例子中的代码基于 person 返回了一个新对象—— anotherperson 。 新对象不仅具有 person
   的所有属性和方法,而且还有自己的 sayhi() 方法*/

寄生组合式继承:无论什么情况下,都会调用两次超类型构造函数,一次是在创建子类型原型的时候,另一次是在创建子类型原型的时候,另一次是在子类型构造函数内部,这样子类型最终会包含超类型对象的全部实例属性,我们不得不在调用子类型构造函数时重写这些属性。因此出现了寄生组合式继承。

6.寄生组合式继承:借用构造函数来继承属性,通过原型链的混成形式来继承方法。基本思路:不必为了指定子类型的原型而调用超类型的构造函数。本质上就是使用寄生式继承来继承超类型的原型,然后再将结果指定给子类型的原型。

function supertype(name){
    this.name=name;
    this.colors=['red','blue','green'];
  }
  supertype.prototype.sayname=function(){
    console.log(this.name);
  }
  function subtype(name,age){
    supertype.call(this,name);
    this.age=age;
  }
  function object(o){
    function f(){};
    f.prototype=o;
    return new f();
  };
  /*inheritprototype此函数第一步是创建超类型原型的一个副本。第二步是为创建的副本添加constructor属性,
  * 从而弥补因重写原型而失去的默认的constructor属性,第三步将新创建的对象(副本)赋值给子类型的原型*/
  function inheritprototype(subtype,supertype){
    var prototype=object(supertype.prototype);//创建对象
    prototype.constructor=subtype;//增强对象
    subtype.prototype=prototype;//指定对象
  }
  inheritprototype(subtype,supertype);
  subtype.prototype.sayage=function(){
    console.log(this.age);
  }

  var p=new subtype('xiaoli',24);
  console.log(p.sayname());
  console.log(p.sayage());
  console.log(p.colors)

此方法优点:只调用了一次父类supertype构造函数,并且因此避免了在subtype.prototype上面创建不必要的多余的属性。同时原型链还能保持不变,还能正常使用instanceof和isprototypeof();

以上这篇javascript的几种继承方法介绍就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网