深入学习js系列是自己阶段性成长的见证,希望通过文章的形式更加严谨、客观地梳理js的相关知识,也希望能够帮助更多的前端开发的朋友解决问题,期待我们的共同进步。
如果觉得本系列不错,欢迎点赞、评论、转发,您的支持就是我坚持的最大动力。
这篇文章我们聊聊JavaScript中继承
1、原型链继承
// 创建一个构造函数 Parent
function Parent() {
// 属性name 赋值 kevin
this.name = "kevin";
}
// 在构造函数的原型对象上添加 getName 方法
Parent.prototype.getName = function() {
// 打印 name
console.log(this.name);
};
// 创建 另一个 构造函数
function Child() {}
// 子类的原型指向父类的实例
Child.prototype = new Parent();
// 创建子类实例
var child1 = new Child();
// 打印 name 属性 kevin
console.log(child1.getName()); // kevin
这种写法有什么问题呢? 1.引用类型的属性被所有实例共享,举个例子:
function Parent() {
// 添加的name属性是一个数组类型 赋值 "kevin", "daisy"
this.names = ["kevin", "daisy"];
}
// 子类构造函数
function Child() {}
// 将父类的实例对象 赋值给子类的原型对象
Child.prototype = new Parent();
// 实例化子类
var child1 = new Child();
// 子类给name 属性添加 一个元素
child1.names.push("yayu");
console.log(child1.names); // ["kevin", "daisy", "yayu"]
// 再创建一个实例
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy", "yayu"]
上面的例子中,我们使用原型继承的方式让 child
子类继承 parent
的属性并且父类的属性是一个数组,也就是引用类型。当我们实例化子类生成一个对象 child1
的时候,child1
能够访问父类的 name
属性,并且向 name
属性中添加了一个数据。当我们再实例化一个子类 child2
时候 这个时候打印 ["kevin", "daisy", "yayu"]
这并不是我们想要看到的结果。
2、借用构造函数(经典继承)
function Parent() {
// 添加引用类型属性
this.names = ["kevin", "daisy"];
}
// 子类构造函数
function Child() {
// 内部使用call 执行 Preent 改变 父类的this指向 指向子类
Parent.call(this);
}
var child1 = new Child();
child1.names.push("yayu");
console.log(child1.names); // ["kevin", "daisy", "yayu"]
var child2 = new Child();
console.log(child2.names); // ["kevin", "daisy"]
观察上述代码实现们发现, 这种 继承的方式解决了第一种 继承方式那种引用类型的问题。
1、避免了引用类型被所有的 实例共享。
2、可以 向父类Parent
中传参。
举个例子:
function Parent(name) {
this.name = name;
}
function Child(name) {
Parent.call(this, name);
}
var child1 = new Child("kevin");
console.log(child1.name); // kevin
var child2 = new Child("daisy");
console.log(child2.name); // daisy
上述代码中,在创建子类实例的时候,向构造函数中传递了 name 参数 通过这个子类的构造函数又传参给了父类的构造函数,从而可以按照需求 进行打印名字。
当然这种继承方式也是优缺点的,所有的方法都在构造函数中声明,当我们实例化对象的 时候,每次都对会调用构造函数,从而重新创建一遍方法。
3、组合继承(双剑合璧)
原型链继承和经典继承双剑合璧。
// 创建 父类构造函数
function Parent(name) {
// 接收属性 接收name 参数
this.name = name;
this.colors = ["red", "blue", "green"];
}
// 在父类的原型上面添加方法
Parent.prototype.getName = function() {
console.log(this.name);
};
// 创建子类
function Child(name, age) {
// 子类的构造函数中 执行父类的构造函数接收参数。
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
// 将子类的原型对象的contructor 属性重新赋值 Child
Child.prototype.constructor = Child;
var child1 = new Child("kevin", "18");
child1.colors.push("black");
console.log(child1.name); // kevin
console.log(child1.age); // 18
console.log(child1.colors); // ["red", "blue", "green", "black"]
var child2 = new Child("daisy", "20");
console.log(child2.name); // daisy
console.log(child2.age); // 20
console.log(child2.colors); // ["red", "blue", "green"]
这种组合继承的实现方式融合了构造函数和 原型链继承 的优点,是被广泛使用的一种继承方式
4.原型式继承
function createObj(o) {
function F() {}
F.prototype = o;
return new F();
}
就是 ES5 Object.create 的模拟实现,将传入的对象作为创建的对象的原型。 缺点: 包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。
var person = {
name: "kevin",
friends: ["daisy", "kelly"]
};
var person1 = createObj(person);
var person2 = createObj(person);
person1.name = "person1";
console.log(person2.name); // kevin
person1.firends.push("taylor");
console.log(person2.friends); // ["daisy", "kelly", "taylor"]
注意:修改 person1.name 的值,person2.name 的值并未发生改变,并不是因为 person1 和 person2 有独立的 name 值,而是因为 person1.name = 'person1',给 person1 添加了 name 值,并非修改了原型上的 name 值。
5. 寄生式继承
创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。
function createObj(o) {
var clone = Object.create(o);
clone.sayName = function() {
console.log("hi");
};
return clone;
}
缺点:跟借用构造函数模式一样,每次创建对象都会创建一遍方法。
6. 寄生组合式继承
为了方便大家阅读,在这里重复一下组合继承的代码:
function Parent(name) {
this.name = name;
this.colors = ["red", "blue", "green"];
}
Parent.prototype.getName = function() {
console.log(this.name);
};
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
Child.prototype = new Parent();
var child1 = new Child("kevin", "18");
console.log(child1);
组合继承最大的缺点是会调用两次父构造函数。
一次是设置子类型实例的原型的时候:
Child.prototype = new Parent();
一次在创建子类型实例的时候:
var child1 = new Child("kevin", "18");
回想下 new 的模拟实现,其实在这句中,我们会执行:
Parent.call(this, name);
在这里,我们又会调用了一次 Parent 构造函数。
所以,在这个例子中,如果我们打印 child1 对象,我们会发现 Child.prototype 和 child1 都有一个属性为 colors,属性值为['red', 'blue', 'green']。
那么我们该如何精益求精,避免这一次重复调用呢?
如果我们不使用 Child.prototype = new Parent() ,而是间接的让 Child.prototype 访问到 Parent.prototype 呢?
看看如何实现:
function Parent(name) {
this.name = name;
this.colors = ["red", "blue", "green"];
}
Parent.prototype.getName = function() {
console.log(this.name);
};
function Child(name, age) {
Parent.call(this, name);
this.age = age;
}
// 关键的三步
var F = function() {};
F.prototype = Parent.prototype;
Child.prototype = new F();
var child1 = new Child("kevin", "18");
console.log(child1);
最后我们封装一下这个继承方法:
function object(o) {
function F() {}
F.prototype = o;
return new F();
}
function prototype(child, parent) {
var prototype = object(parent.prototype);
prototype.constructor = child;
child.prototype = prototype;
}
// 当我们使用的时候:
prototype(Child, Parent);
引用《JavaScript高级程序设计》中对寄生组合式继承的夸赞就是:
这种方式的高效率体现它只调用了一次 Parent 构造函数,并且因此避免了在 Parent.prototype 上面创建不必要的、多余的属性。与此同时,原型链还能保持不变;因此,还能够正常使用 instanceof 和 isPrototypeOf。开发人员普遍认为寄生组合式继承是引用类型最理想的继承范式。
深入学习JavaScript系列目录
- #1 【深入学习js之——原型和原型链】
- #2 【深入学习js之——词法作用域和动态作用域】
- #3 【深入学习js之——执行山下文栈】
- #4 【深入学习js之——变量对象】
- #5 【深入学习js之——作用域链】
- #6 【深入学习js之——实际开发场景中的this指向】
- #7 【深入学习js之——执行上下文】
- #8 【深入学习js之——闭包】
- #9 【深入学习js之——参数按值传递】
- #10 【深入学习js之——call和apply】
- #11 【深入学习js之——类数组对象与arguments】
- #12 【深入学习js之——创建对象的各种方式以及优缺点】
欢迎添加我的个人微信讨论技术和个体成长。
欢迎关注我的个人微信公众号——指尖的宇宙,更多优质思考干货