首先简单的介绍一下面向对象的三大特性
- 封装: 将属性和方法封装成一个对象,实现一系列的功能,使用的时候只需要向外界暴露接口,并不需要关心对象内部是怎么样实现的。
- 继承: 在js中,一个对象没有某些属性或者方法,另外一个对象中有,那么我们可以从另外一个对象中拿过来用。
- 多态: 在js中是不支持多态的(其实是父类指针指向子类对象)
接下来我们主要介绍js中的几种常用的继承方式
- 混入式继承
// 构造函数
function Parent(){};
function Child(){};
// 实例化对象
var parent = new Parent();
var child = new Child();
for(var k in parent){
child[k] = parent[k]
}
优点:简单易理解
缺点:针对父类中的引用类型,多个实例对象会共享,所以在操作的同一个引用类型时会相互影响
- 原型链继承
- 修改原型对象实现继承
//构造函数 function Parent(){ this.name = 'zs' this.arr = [1,2,3]; } function Child(){} var person = new Parent(); for(var k in person){ Child.prototype[k] = person[k]; // 让孩子原型去继承父亲new出来对象的属性 } var child1 = new Child(); child1.arr.push(4); console.log(child1.arr); // [1,2,3,4] var child2 = new Child(); console.log(child1.arr); // [1,2,3,4] console.log(Child.prototype.constructor) // ==>指向Child构造函数
- 通过替换原型对象实现继承(子类的原型指向父类的实例)
// 构造函数 function Parent(){ this.name = 'zs' this.arr = [1,2,3]; } function Child(){} var person = new Parent(); Child.protoType = person; // 替换原型对象之后,constructor属性会丢失 Child.protoType.constructor = Child; // 解决constructor属性丢失的问题
优点: 简单易理解
缺点: 原型链中的引用类型的属性会被所有的实例共享,即所有的实例对象使用的是同一份数据,会相互影响。无法向父类构造函数传参。通过原型链实现继承后,不能再使用字面量的方式创建原型对象,因为会覆盖原型链。
- 经典继承(原型式继承,子类的原型指向父类副本的实例)
var obj = Object.create(obj1);// 创建一个obj对象,原型对象是是obj1, 但是他有兼容性有问题
// 解决兼容性问题
if(Object.create){
var obj = Object.create(obj1);
} else {
function F = {};
F.prototype = obj1;
var f = new F; // 构造函数没有参数时,创建对象后边的小括号可以不加
return f
}
- 借用构造函数继承
function Parent(name){
this.name = name;
}
function Child(){
Parent.call(this); // 将Parent的this指向通过Child构造函数new出来的对象 等同于 Parent.call(child1)
}
var child1 = new Child(); // 这样每个实例都会有一份Parent中的属性方法的副本,而且每个实例都是单独的,互相不影响
优点: 这样每个实例都会有一份Parent中的属性方法的副本,而且每个实例都是单独的,互相不影响。子类对象可以向父类对象进行传参的操作了
缺点: 每个实例都拷贝一份,占用内存大,尤其是方法过多的时候,构造函数继承是需要把所有的属性和方法都放到构造函数中,那与在原型上添加方法实现复用是冲突的。
Child只能继承Parent自身的属性,而无法继承Parent原型中的方法。
- 组合继承(将原型链和借用构造函数组合到一起,原型链实现对原型方法的继承,而通过借用构造函数来实现对实例属性的继承)
//父类
function Parent(name) {
this.name = name
this.arr = [1,2,3]
}
Parent.prototype.getName = function() { // ① 我们将 Parent 类中需要复用的方法提取到 Parent.prototype 中
console.log(this.name)
}
// 子类
function Child(name, age) {
// ③ 为保证 child1 和 child2 拥有各自的父类属性副本,我们在 Child 构造函数中,还是使用了 Parent.call ( this ) 方法。
// 它只能继承构造函数里的属性和方法
Parent.call(this, name)
this.age = age
}
// ② Child 的原型对象为 Parent 类的一个实例。
// 这样 child1 就能访问到 Parent 原型对象上的属性和方法了
Child.prototype = new Parent();
Child.prototype.constructor = Child
Child.prototype.getAge = function() { // age用的是Child自己的属性
console.log(this.age)
}
var child1 = new Child("张三", 20)
child1.arr.push(4)
console.log(child1.arr) //1,2,3,4
child1.getName() //张三
child1.getAge() //20
var child2 = new Child("李四", 21)
console.log(son2.arr) //1,2,3
child2.getName() //李四
child2.getAge() //21
- 寄生式继承
创建一个仅用于封装继承过程的函数
function inherit(o) {
var clone = Object.create(o) // 创建一个新对象
clone.sayHi = function() { // 让返回的新对象不仅继承person的属性和方法,还可以拥有自己的sayHi方法
console.log('hi')
}
return clone // 返回这个对象
}
var person = {
name: 'zs'
}
var child = inherit(person);
child.sayHi()
- 寄生组合继承 (完美的继承)
由于组合继承,会导致父类的构造函数被调用两次,而寄生继承不用实例化父类,而是实例化一个临时副本实现了相同的原型链继承。
// 父类
function Parent(name,colors){
this.name = name;
this.colors = colors;
}
Parent.prototype.getParentProperty = function(){ return this.name; }
// 子类
function Child(job,name,colors){
Parent.call(this,name,colors); // 属性继承
this.job=job;
}
Child.prototype.getChildPrototype = function(){ return this.job; }
function inherit(child,parent){ // 原型式继承,子类会继承父类原型上的属性和方法,而不能继承构造函数的方法
var prototype = Object.create(parent.prototype); // 创建父类原型的副本
prototype.constructor = child;
child.prototype = prototype; // 将子类的原型指向副本
}
inherit(Child,Parent);
var instance = new Child("doctor","Jack",["red","green"]);
console.log(instance.getChildPrototype()); //输出"doctor"
console.log(instance.getParentProperty()); //输出"Jack",成功调用在父类原型定义的方法