My World.

It's the loneliest feeling not to know who you are.

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

继承到目前为止我所掌握的总共有六种,下边的是所举的例子(A为父类,B为子类)

 

1. 原型继承:将父类的实例赋值给子类的原型

 举个例子:

function A(){

  this.name = 'cc'

}

A.prototype.x = 56;

function B(){

  this.age = 19

}

B.prototype = new A;

var a =new A;

var    b=new B;

 

这就是原型继承 将父类的私有和公有都继承在子类的原型上,成为子类的公有属性。

 

2.call继承:将父类私有的继承为子类私有的

function A(){

  this.name = 'cc'

}

A.prototype.x = 56;

function B(){

  this.age = 19

  A.call(this)

}

 

3.冒充对象继承:将父类的私有和公有继承到子类私有

 

 

function A(){

 

  this.name = 'cc'

 

}

 

A.prototype.x = 56;

 

function B(){

 

  this.age = 19

  var temp = new A;

  for(var key in temp){

    this[key] = temp[k]

  }

  temp = null;

 

}

 

 

 

var a =new A;

 

var    b=new B;

 

 

 4.混合继承: 私有继承私有,公有继承私有和公有(此处的私有一共继承了两遍) 

 混合继承是call和原型继承的结合

 

 

function A(){

  this.name = 'cc'

}

A.prototype.x = 56;

function B(){

  this.age = 19

  A.call(this)

}

B.prototype = new A;

 

5. 组合继承 私有的继承为私有的 公有的继承为公有的

 

 

function A(){

 

  this.name = 'cc'

 

}

 

A.prototype.x = 56;

 

function B(){

 

  this.age = 19

  A.call(this)

 

}

 

B.prototype = Object.create;

 

var a =new A;

 

var    b=new B;

 

 

6.中间类继承

function f(){

arguments.__proto__ = Array.prototype;

arguments.shift()

}

f(12,26,39)

 

arguments 不是一个数组,没有array的那些自带的方法,现在我们想argumentsarray的那些方法,将arguments的原型执行Array内置类的原型。

 

 

 

 

 

posted on 2019-01-07 19:03  blankOne  阅读(119)  评论(0编辑  收藏  举报