一、实例从构造函数的原型身上继承方法
原理:
任何一个new出来的实例上都可使用原型对象上的方法,视为最基础的继承。(在原型上创建方法和在函数内创建方法都可以继承)
function Fn(){
this.name = "admin";
}
// 在原型对象上新建一个方法
Fn.prototype.show = function(){
console.log(this.name);
}
var f = new Fn();
console.log(f); //Fn {name: "admin"}
console.log(f.name); //admin
// 实例调用这个方法
console.log(f.show); //f(){console.log(tihs.name)}
二、构造函数继承
原理:
改变this指向,使用bind(),call(),apply()改变this指向
优缺点:
只能继承构造函数中的属性和方法,不能继承原型上的属性和方法
简单方便,创建子类实例时,可以向父类的构造器传参;
可以实现多继承,可以继承多个构造函数中的内容
// 改变this指向,继承构造函数中的属性
function Parent(n,a){
this.name = n;
this.age = a;
}
function Child(n,a){
// 在子构造函数中调用父构造函数
Parent.call(this,n,a); //改变this的指向为child,复制成一个新对象
}
var p = new Parent("大明",48);
var c = new Child("小明",18);
console.log(p);
console.log(c);
三、原型对象继承
原理:
通过原型对象prototype来实现继承
优缺点:
- 可以继承原型的方法和属性,不能继承构造函数的方法和属性
function Parent(n){
this.name = n;
}
Parent.prototype.show = function(){
console.log(this.name);
}
// 浅拷贝
// Child.prototype = Parent.prototype;
// 深拷贝
for(var i in Parent.prototype){
Child.prototype[i] = Parent.prototype[i];
}
function Child(n){
this.name = n;
}
Child.prototype.show = function(){
console.log("hello");
}
var p = new Parent("小头爸爸");
var c = new Child("大头儿子");
// console.log(Parent.prototype); //{show: ƒ, constructor: ƒ}
p.show();
c.show();
console.log(p);
console.log(c);
// 浅拷贝
// Child.prototype = Parent.prototype;
只是改变了地址,子函数的constructor指向了父函数的constructor,如果修改子函数的原型对象的方法,会同时改变父函数的
image.png
// 深拷贝
for(var i in Parent.prototype){
Child.prototype[i] = Parent.prototype[i];
}
地址和值都改变,子函数的constructor指向不变
image.png
四、原型链继承
原理:
Child的原型对象指向Parent实例化的对象,Child的实例,可以访问Parent构造函数内部的this的属性和原型上的方法
优缺点:
既可以继承构造函数中的方法和属性,又可以继承原型上的方法和属性
不方便传参
原型上的属性是共享的,一个实例修改了原型属性,另一个实例的原型属性也会被修改
function Parent(n,a){
this.name = n;
this.age = a;
this.hi = "hello";
}
Parent.prototype.show = function(){
console.log(this.name);
}
function Child(n,a,s){
Parent.call(this,n,a);
this.score = s;
}
// Child的原型对象指向Parent实例化的对象
Child.prototype = new Parent();
//子的原型对象的constructor被改变,所以需要手动指回
Child.prototype.constructor = Child;
Child.prototype.school = function(){
console.log("go school");
}
var p = new Parent("大明",48);
var c = new Child("小明",18,100);
console.log(p);
console.log(c);
console.log(hi);
// Child的原型对象指向Parent实例化的对象
Child.prototype = new Parent();
image.png
//子的原型对象的constructor被改变,所以需要手动指回
Child.prototype.constructor = Child;
image.png
五、混合继承
原理:
改变this指向继承+原型对象继承
优缺点:
- 既能继承构造函数中的属性,又能继承构造函数的prototype上的方法
function Parent(n){
this.name = n;
}
Parent.prototype.show = function(){
console.log(this.name);
}
function Child(n){
// 改变this指向改变构造函数上的属性
Parent.call(this,n);
}
// 原型对象继承(深拷贝)原型上的方法
for(var i in Parent.prototype){
Child.prototype[i] = Parent.prototype[i];
}
// 检测是改变子构造函数的方法是否影响父构造函数的方法
// Child.prototype.show = function(){
// console.log("hello");
// }
var p = new Parent("小明");
var c = new Child("小红");
p.show();
c.show();
六、ES6的class类继承:
原理:
构造函数继承(改变this指向)+原型链继承
优缺点:
- 既能继承属性又能继承方法
关键字extends:子类可以通过extends继承父类里面的属性和方法;
super关键字:用于访问和调用父类上的函数;
可以调用父类里面的构造函数,也可以调用父类里面的普通函数方法;
class Parent {
constructor(n){
this.name = n;
}
show(){
console.log(this.name);
}
}
class Child extends Parent { //继承Parent的属性和方法
constructor(n){
super(n); //调用Parent,并传参
}
// show(){
// console.log("hello");
// }
}
var p = new Parent("小头爸爸");
var c = new Child("大头儿子");
p.show();
c.show();
console.log(p);
console.log(c);
image.png
补充相关知识点:
构造函数
- 主要用来初始化对象,即为对象成员变量赋初始值,和new一起使用。把对象中一些公共的属性和方法抽取出来,然后封装到这个函数里面。
new的作用
- 内存中创建一个新的空对象;
- 让this指向这个新对象;
- 执行构造函数里面的代码,给这个新对象添加属性和方法;
- 检查原函数里有没有主动返回对象,没有就返回新对象(所以构造函数里不需要return)。
原型对象prototype:
- 每个函数都有一个prototype属性,指向另外一个对象。这个prototype就是一个对象,这个对象的所有属性和方法,都会被构造函数所拥有。
- 可以把不变的方法,直接定义在prototype对象上,所有对象的实例就可以共享这个方法。
- 一般情况下,我们的公共属性定义到构造函数里面,公共的方法放到原型对象身上。
对象原型proto:
对象实例里面都会有一个属性proto指向构造函数的prototype原型对象。
对象原型意义:为对象的查找机制提供一个方向,但是它是一个非标准属性,因此在实际开发中,一般不直接使用,它只能是内部指向原型对象prototype
个人学习总结分享,若有不当之处,欢迎留言交流~~