js继承的方式你知道那些

继承

在java中,我们对继承并不陌生,java面向对象,其中他的一个特性就是继承,因为继承有很多好处,那么在js中我们是怎么实现继承的呢?你知道有哪些方式呢?

先列举我们常用的继承的方式:

  1. 原型链继承,原型链的知识在前面记录了原型
    在上篇中,我们知道可以给原型上添加属性或者方法,这样实例之间是可以公用这些属性和方法的,这减少了内存消耗。
  2. 构造函数继承
  3. 组合继承
  4. 寄生组合式继承

下面来分别讲讲他们的好处和不足,以及一步步优化。

原型链继承

实现

function Parent() {
  this.name = "parentName";
}

Parent.prototype.getName = function () {
  console.log(this.name);
};

function Child() {}

// Parent的实例同时包含实例属性方法和原型属性方法,所以把new Parent()赋值给Child.prototype。
// 如果仅仅Child.prototype = Parent.prototype,那么Child只能调用getName,无法调用.name
// 当Child.prototype = new Parent()后, 如果new Child()得到一个实例对象child,那么
// child.__proto__ === Child.prototype;
// Child.prototype.__proto__ === Parent.prototype
// 也就意味着在访问child对象的属性时,如果在child上找不到,就会去Child.prototype去找,如果还找不到,就会去Parent.prototype中去找,从而实现了继承。
Child.prototype = new Parent();
// 因为constructor属性是包含在prototype里的,上面重新赋值了prototype,所以会导致Child的constructor指向[Function: Parent],有的时候使用child1.constructor判断类型的时候就会出问题
// 为了保证类型正确,我们需要将Child.prototype.constructor 指向他原本的构造函数Child
Child.prototype.constructor = Child;

var child1 = new Child();

child1.getName(); // parentName

隐含的问题

  1. 如果有属性是引用类型的,一旦某个实例修改了这个属性,所有实例都会受到影响。
    因为他们调用的是统一个引用地址,这样实例之间的数据会互相污染,如果有多个实例的情况下,这显然是不太符合我们想要的效果的。
function Parent() {
  this.actions = ["eat", "run"];
}
function Child() {}

Child.prototype = new Parent();
Child.prototype.constructor = Child;

const child1 = new Child();
const child2 = new Child();

child1.actions.pop();
// 当child1改变的时候,也影响了child2的属性值
console.log(child1.actions); // ['eat']
console.log(child2.actions); // ['eat']
  1. 创建 Child 实例的时候,不能传参
    当我们在调用原型上的方法的时候,我们确实不能传参数,只能使用已有的,这显得不是那么的灵活。

构造函数继承

看到上面的问题 1,我们想一下该怎么解决呢?

能不能想办法把 Parent 上的属性方法,添加到 Child 上呢?而不是都存在原型对象上,防止被所有实例共享。
那我们听到比较多你的就是构造函数了吧。

实现

针对问题 1. 我们可以使用 call 来复制一遍 Parent 上的操作

function Parent() {
  this.actions = ["eat", "run"];
  this.name = "parentName";
}

function Child() {
  Parent.call(this);
  // 这句代码的含义是改变this的指向,使this指向Child的实例
}

const child1 = new Child();
const child2 = new Child();

child1.actions.pop();

console.log(child1.actions); // ['eat']
console.log(child1.actions); // ['eat', 'run']

解决了问题1,那问题2可以解决吗?
针对问题 2. 我们应该怎么传参呢?

function Parent(name, actions) {
  this.actions = actions;
  this.name = name;
}

function Child(id, name, actions) {
// 这里把name传到Parent,当然也可以传多个参数
  Parent.call(this, name); // 如果想直接传多个参数, 可以Parent.apply(this, Array.from(arguments).slice(1));
  this.id = id;
}

const child1 = new Child(1, "c1", ["eat"]);
const child2 = new Child(2, "c2", ["sing", "jump", "rap"]);

console.log(child1.name); // { actions: [ 'eat' ], name: 'c1', id: 1 }
console.log(child2.name); // { actions: [ 'sing', 'jump', 'rap' ], name: 'c2', id: 2 }

这样就达到了我们的预期。

隐含的问题

属性或者方法想被继承的话,只能在构造函数中定义。而如果方法在构造函数内定义了,那么每次创建实例都会创建一遍方法,多占一块内存。有没有觉得解决了原型链上的问题,但是又失去了原型链继承的优点。

function Parent(name, actions) {
  this.actions = actions;
  this.name = name;
  this.eat = function () {
    console.log(`${name} - eat`);
  };
}

function Child(id) {
  Parent.apply(this, Array.prototype.slice.call(arguments, 1));
  this.id = id;
}

const child1 = new Child(1, "c1", ["eat"]);
const child2 = new Child(2, "c2", ["sing", "jump", "rap"]);

console.log(child1.eat === child2.eat); // false
// 可以看出child1和child2引用的不是同一个指针呢

那么,我们是否可以取其精华,去其糟粕呢,留下各自的好,用对方的优势去弥补自身的不足,强强联合。

组合继承

通过原型链继承我们实现了基本的继承,方法存在 prototype 上,子类可以直接调用。但是引用类型的属性会被所有实例共享,并且不能传参。

通过构造函数继承,我们解决了上面的两个问题:使用 call 在子构造函数内重复一遍属性和方法创建的操作,并且可以传参了。

但是构造函数同样带来了一个问题,就是构造函数内重复创建方法,导致内存占用过多。

是不是突然发现原型链继承是可以解决方法重复创建的问题? 所以我们将这两种方式结合起来,这就叫做组合继承

实现

function Parent(name, actions) {
  this.name = name;
  this.actions = actions;
}

Parent.prototype.eat = function () {
  console.log(`${this.name} - eat`);
};

function Child(id) {
  Parent.apply(this, Array.from(arguments).slice(1));
  this.id = id;
}

Child.prototype = new Parent();
Child.prototype.constructor = Child;

const child1 = new Child(1, "c1", ["hahahahahhah"]);
const child2 = new Child(2, "c2", ["xixixixixixx"]);

child1.eat(); // c1 - eat
child2.eat(); // c2 - eat

console.log(child1.eat === child2.eat); // true

隐含的问题

调用了两次构造函数,做了重复的操作

  1. Parent.apply(this, Array.from(arguments).slice(1));
  2. Child.prototype = new Parent();

寄生组合式继承

上面重复调用了 2 次构造函数,想一下,我们可以精简掉哪一步?

我们可以考虑让 Child.prototype 间接访问到 Parent.prototype

实现

function Parent(name, actions) {
  this.name = name;
  this.actions = actions;
}

Parent.prototype.eat = function () {
  console.log(`${this.name} - eat`);
};

function Child(id) {
  Parent.apply(this, Array.from(arguments).slice(1));
  this.id = id;
}

// 模拟Object.create的效果
// 如果直接使用Object.create的话,可以写成Child.prototype = Object.create(Parent.prototype);
let TempFunction = function () {};
TempFunction.prototype = Parent.prototype;
Child.prototype = new TempFunction();

Child.prototype.constructor = Child;

const child1 = new Child(1, "c1", ["hahahahahhah"]);
const child2 = new Child(2, "c2", ["xixixixixixx"]);

也许有的同学会问,为什么一定要通过桥梁的方式让 Child.prototype 访问到 Parent.prototype?
直接 Child.prototype = Parent.prototype 不行吗?
答:不行!!

咱们可以来看一下

function Parent(name, actions) {
  this.name = name;
  this.actions = actions;
}

Parent.prototype.eat = function () {
  console.log(`${this.name} - eat`);
};

function Child(id) {
  Parent.apply(this, Array.from(arguments).slice(1));
  this.id = id;
}

Child.prototype = Parent.prototype;

Child.prototype.constructor = Child;

console.log(Parent.prototype); // Child { eat: [Function], childEat: [Function] }

Child.prototype.childEat = function () {
  console.log(`childEat - ${this.name}`);
};

const child1 = new Child(1, "c1", ["hahahahahhah"]);

console.log(Parent.prototype); // Child { eat: [Function], childEat: [Function] }

可以看到,在给 Child.prototype 添加新的属性或者方法后,Parent.prototype 也会随之改变,这可不是我们想看到的。

嗯,每天坚持输出一篇文章和算法题,本来今天好像不是很有状态,但是想到还有任务没有完成,于是又提起精神,很也快进入状态。每天保持进步一点点吧。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值