js
里经常使用的以下两种继承方式:原型链继承(对象间的继承) 类式继承(构造函数间的继承)
因为js
不像java
那样是真正面向对象的语言,js
是基于对象的,它没有类的概念。因此,要想实现继承,能够用js
的原型prototype机制或者用apply
和call
方法去实现java
在面向对象的语言中,咱们使用类
来建立一个自定义对象。然而js
中全部事物都是对象,那么用什么办法来建立自定义对象呢?这就须要用到js
的原型:segmentfault
咱们能够简单的把prototype
看作是一个模版,新建立的自定义对象都是这个模版(prototype
)的一个拷贝 (实际上不是拷贝而是连接,只不过这种连接是不可见,新实例化的对象内部有一个看不见的__Proto__
指针,指向原型对象)。app
js
能够经过构造函数和原型的方式模拟实现类的功能。 另外,js
类式继承的实现也是依靠原型链来实现的。函数
类式继承
是在子类型构造函数的内部调用超类型的构造函数。
严格的类式继承并非很常见,通常都是组合着用:ui
1 function Super(){ 2 this.colors=["red","blue"]; 3 } 4 5 function Sub(){ 6 Super.call(this); 7 }
原型式继承
是借助已有的对象建立新的对象,将子类的原型指向父类,就至关于加入了父类这条原型链this
为了让子类继承父类的属性(也包括方法),首先须要定义一个构造函数。而后,将父类的新实例赋值给构造函数的原型。代码以下:spa
1 <script> 2 function Parent(){ 3 this.name = 'mike'; 4 } 5 6 function Child(){ 7 this.age = 12; 8 } 9 Child.prototype = new Parent();//Child继承Parent,经过原型,造成链条 10 11 var test = new Child(); 12 alert(test.age); 13 alert(test.name);//获得被继承的属性 14 //继续原型链继承 15 function Brother(){ //brother构造 16 this.weight = 60; 17 } 18 Brother.prototype = new Child();//继续原型链继承 19 var brother = new Brother(); 20 alert(brother.name);//继承了Parent和Child,弹出mike 21 alert(brother.age);//弹出12 22 </script>
以上原型链继承还缺乏一环,那就是Object
,全部的构造函数都继承自Object
。而继承Object
是自动完成的,并不须要咱们本身手动继承,那么他们的从属关系是怎样的呢?prototype
能够经过两种方式来肯定原型和实例之间的关系。操做符instanceof
和isPrototypeof()
方法:指针
1 alert(brother instanceof Object)//true 2 alert(test instanceof Brother);//false,test 是brother的超类 3 alert(brother instanceof Child);//true 4 alert(brother instanceof Parent);//true
只要是原型链中出现过的原型,均可以说是该原型链派生的实例的原型,所以,isPrototypeof()
方法也会返回true
code
在js
中,被继承的函数称为超类型(父类,基类也行),继承的函数称为子类型(子类,派生类)。使用原型继承主要由两个问题:
一是字面量重写原型会中断关系,使用引用类型的原型,而且子类型还没法给超类型传递参数。
伪类解决引用共享和超类型没法传参的问题,咱们能够采用“借用构造函数”技术
1 <script> 2 function Parent(age){ 3 this.name = ['mike','jack','smith']; 4 this.age = age; 5 } 6 7 function Child(age){ 8 Parent.call(this,age); 9 } 10 var test = new Child(21); 11 alert(test.age);//21 12 alert(test.name);//mike,jack,smith 13 test.name.push('bill'); 14 alert(test.name);//mike,jack,smith,bill 15 </script>
借用构造函数虽然解决了刚才两种问题,但没有原型,则复用无从谈起,因此咱们须要原型链+借用构造函数
的模式,这种模式称为组合继承
1 <script> 2 function Parent(age){ 3 this.name = ['mike','jack','smith']; 4 this.age = age; 5 } 6 Parent.prototype.run = function () { 7 return this.name + ' are both' + this.age; 8 }; 9 function Child(age){ 10 Parent.call(this,age);//对象冒充,给超类型传参 11 } 12 Child.prototype = new Parent();//原型链继承 13 var test = new Child(21);//写new Parent(21)也行 14 alert(test.run());//mike,jack,smith are both21 15 </script>
组合式继承是比较经常使用的一种继承方法,其背后的思路是 使用原型链实现对原型属性和方法的继承,而经过借用构造函数来实现对实例属性的继承。这样,既经过在原型上定义方法实现了函数复用,又保证每一个实例都有它本身的属性。
call()
的用法:调用一个对象的一个方法,以另外一个对象替换当前对象。
1 call([thisObj[,arg1[, arg2[, [,.argN]]]]])
这种继承借助原型并基于已有的对象建立新对象,同时还不用建立自定义类型的方式称为原型式继承
1 <script> 2 function obj(o){ 3 function F(){} 4 F.prototype = o; 5 return new F(); 6 } 7 var box = { 8 name : 'trigkit4', 9 arr : ['brother','sister','baba'] 10 }; 11 var b1 = obj(box); 12 alert(b1.name);//trigkit4 13 14 b1.name = 'mike'; 15 alert(b1.name);//mike 16 17 alert(b1.arr);//brother,sister,baba 18 b1.arr.push('parents'); 19 alert(b1.arr);//brother,sister,baba,parents 20 21 var b2 = obj(box); 22 alert(b2.name);//trigkit4 23 alert(b2.arr);//brother,sister,baba,parents 24 </script>
原型式继承首先在obj()
函数内部建立一个临时性的构造函数 ,而后将传入的对象做为这个构造函数的原型,最后返回这个临时类型的一个新实例。
这种继承方式是把原型式+工厂模式结合起来,目的是为了封装建立的过程。
1 <script> 2 function create(o){ 3 var f= obj(o); 4 f.run = function () { 5 return this.arr;//一样,会共享引用 6 }; 7 return f; 8 } 9 </script>
组合式继承是js
最经常使用的继承模式,但组合继承的超类型在使用过程当中会被调用两次;一次是建立子类型的时候,另外一次是在子类型构造函数的内部
1 <script> 2 function Parent(name){ 3 this.name = name; 4 this.arr = ['哥哥','妹妹','父母']; 5 } 6 7 Parent.prototype.run = function () { 8 return this.name; 9 }; 10 11 function Child(name,age){ 12 Parent.call(this,age);//第二次调用 13 this.age = age; 14 } 15 16 Child.prototype = new Parent();//第一次调用 17 </script>
以上代码是以前的组合继承,那么寄生组合继承,解决了两次调用的问题。
1 <script> 2 function obj(o){ 3 function F(){} 4 F.prototype = o; 5 return new F(); 6 } 7 function create(parent,test){ 8 var f = obj(parent.prototype);//建立对象 9 f.constructor = test;//加强对象 10 } 11 12 function Parent(name){ 13 this.name = name; 14 this.arr = ['brother','sister','parents']; 15 } 16 17 Parent.prototype.run = function () { 18 return this.name; 19 }; 20 21 function Child(name,age){ 22 Parent.call(this,name); 23 this.age =age; 24 } 25 26 inheritPrototype(Parent,Child);//经过这里实现继承 27 28 var test = new Child('trigkit4',21); 29 test.arr.push('nephew'); 30 alert(test.arr);// 31 alert(test.run());//只共享了方法 32 33 var test2 = new Child('jack',22); 34 alert(test2.arr);//引用问题解决 35 </script>
全局函数apply
和call
能够用来改变函数中this
的指向,以下: