继承的多种方式

在这里插入图片描述

1. 原型链继承

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

function Child() {}

Child.prototype = new Parent();

const child = new Child();
child.getName();
console.log(child.name);

引用类型的属性被所有实例共享

function Parent() {
  this.names = ["xiaohong", "xiaoming"];
}
Parent.prototype.getName = function () {
  console.log(this.names);
};

function Child() {}

Child.prototype = new Parent();

const child1 = new Child();
child1.names.push("xiaozhang");
console.log(child1.names);
const child2 = new Child();
console.log(child2.names);

2. 构造函数

function Parent() {
  this.names = ["xiaohong", "xiaoming"];
}
function Child() {
  Parent.call(this);
}

const child1 = new Child();
child1.names.push("xiaozhang");
console.log(child1.names);

const child2 = new Child();
console.log(child2.names);

优点:
1. 避免了引用类型的属性被所有实例共享
2. 可以向Parent中传参
缺点:
1. 方法都在构造函数中定义,每次创建实例都会创建一遍方法。

3. 组合继承

function Parent(name) {
  this.name = name;
  this.colors = ["red", "blue", "green"];
}

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

function Child(name) {
  Parent.call(this, name);
}
Child.prototype = new Parent();
const child = new Child("zhangsan");
child.getName();
console.log(child.name);
// 优点:融合原型链继承和构造函数的优点,是 JavaScript 中最常用的继承模式。

4. 原型继承

function createObj(obj) {
  function F() {}
  F.prototype = obj;
  const ex = new F();
  return new F();
}

// 缺点:包含引用类型的属性值始终都会共享相应的值,这点跟原型链继承一样。
// example:
var person = {
  name: "zhangsan",
  friends: ["xiaoming", "xiaohong"],
};

// person1.__proto__ === (new F()).__proto__ === F.protptype === obj
var person1 = createObj(person);
person1.friends.push("xiaoshuai");
var person2 = createObj(person);
console.log(person1.friends, person2.friends); // [ 'xiaoming', 'xiaohong', 'xiaoshuai' ]  [ 'xiaoming', 'xiaohong', 'xiaoshuai' ]

// console.log(person1.friends, person2.friends);

5. 寄生式继承

// 创建一个仅用于封装继承过程的函数,该函数在内部以某种形式来做增强对象,最后返回对象。
function createObj(o) {
  var clone = Object.create(o);
  clone.sayName = function () {
    console.log("hi");
  };
  return clone;
}
  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaScript多种继承方式,以下是几种常见的方式: 1. 原型链继承:通过将父类实例赋值给子类的原型来实现继承。 ``` function Parent() { this.name = 'Parent'; } Parent.prototype.sayName = function() { console.log(this.name); } function Child() {} Child.prototype = new Parent(); var child = new Child(); child.sayName(); // 输出 Parent ``` 2. 构造函数继承:通过在子类构造函数中调用父类构造函数来实现继承。 ``` function Parent() { this.name = 'Parent'; } Parent.prototype.sayName = function() { console.log(this.name); } function Child() { Parent.call(this); } var child = new Child(); child.sayName(); // 报错,因为子类没有继承父类的原型方法 ``` 3. 组合继承:结合原型链继承和构造函数继承方式。 ``` function Parent() { this.name = 'Parent'; } Parent.prototype.sayName = function() { console.log(this.name); } function Child() { Parent.call(this); } Child.prototype = new Parent(); Child.prototype.constructor = Child; var child = new Child(); child.sayName(); // 输出 Parent ``` 4. 寄生组合继承:对组合继承进行优化,避免了在子类原型上创建不必要的父类实例。 ``` function Parent() { this.name = 'Parent'; } Parent.prototype.sayName = function() { console.log(this.name); } function Child() { Parent.call(this); } Child.prototype = Object.create(Parent.prototype); Child.prototype.constructor = Child; var child = new Child(); child.sayName(); // 输出 Parent ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值