当前位置: 首页 > news >正文

凡科网免费做网站近期热点新闻事件

凡科网免费做网站,近期热点新闻事件,想做无货源电商怎么入手,网站开发工程师培训班Question JavaScript实现继承的方式? 包含原型链继承、构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合式继承和ES6 类继承 JavaScript实现继承的方式 在JavaScript中,实现继承的方式多种多样,每种方式都有其优势和适用场景。以下…

Question JavaScript实现继承的方式?

包含原型链继承、构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合式继承和ES6 类继承

JavaScript实现继承的方式

在JavaScript中,实现继承的方式多种多样,每种方式都有其优势和适用场景。以下是一些常见的继承方式:

1. 原型链继承

原型链继承是通过将子类的原型设置为父类的实例来实现继承。这样,子类就可以访问父类原型上的属性和方法。

function Parent({
  this.name = 'Parent';
}

function Child({}

Child.prototype = new Parent();

const childInstance = new Child();
console.log(childInstance.name); // 输出 'Parent'

优势: 简单易懂。

缺点: 引用类型的属性会被所有实例共享,无法传递参数给父类构造函数。

2. 构造函数继承

构造函数继承通过在子类构造函数中调用父类构造函数来实现继承。

function Parent(name{
  this.name = name || 'Parent';
}

function Child(name{
  Parent.call(this, name);
}

const childInstance = new Child('Child');
console.log(childInstance.name); // 输出 'Child'

优势: 解决了原型链继承中引用类型属性共享的问题。

缺点: 方法都在构造函数中定义,无法实现函数复用。

3. 组合继承

组合继承结合了原型链继承和构造函数继承,通过调用父类构造函数设置实例属性,再通过将父类实例作为子类原型来实现。

function Parent(name{
  this.name = name || 'Parent';
}

function Child(name{
  Parent.call(this, name);
}

Child.prototype = new Parent();

const childInstance = new Child('Child');
console.log(childInstance.name); // 输出 'Child'

优势: 同时继承实例属性和方法。

缺点: 调用了两次父类构造函数,存在一定的性能问题。

4. 原型式继承

原型式继承通过创建一个空对象,然后将该对象作为参数传递给一个函数,该函数的原型被赋值为这个对象,从而实现继承。

function createObject(obj{
  function F({}
  F.prototype = obj;
  return new F();
}

const parent = {
  name'Parent'
};

const child = createObject(parent);
console.log(child.name); // 输出 'Parent'

优势: 简单灵活。

缺点: 属性共享问题,引用类型属性会被所有实例共享。

5. 寄生式继承

寄生式继承在原型式继承的基础上,增加了对父类构造函数的调用,从而可以传递参数给父类构造函数。

function createObject(obj{
  const clone = Object.create(obj);
  clone.sayHello = function({
    console.log('Hello!');
  };
  return clone;
}

const parent = {
  name'Parent'
};

const child = createObject(parent);
console.log(child.name); // 输出 'Parent'
child.sayHello(); // 输出 'Hello!'

优势: 可以在对象上添加新的方法。

缺点: 仍然存在属性共享问题。

6. 寄生组合式继承

寄生组合式继承是为了解决组合继承中调用两次父类构造函数的性能问题,通过使用 Object.create 创建父类原型的副本,然后将该副本赋值给子类原型。

function inheritPrototype(child, parent{
  const prototype = Object.create(parent.prototype);
  prototype.constructor = child;
  child.prototype = prototype;
}

function Parent(name{
  this.name = name || 'Parent';
}

function Child(name{
  Parent.call(this, name);
}

inheritPrototype(Child, Parent);

const childInstance = new Child('Child');
console.log(childInstance.name); // 输出 'Child'

优势: 解决了组合继承的性能问题,同时保持了原型链继承的优点。

缺点: 略显繁琐。

7. ES6 类继承

ES6 引入了 class 关键字,使得面向对象编程更加直观。通过 extends 关键字可以实现类的继承。

class Parent {
  constructor(name) {
    this.name = name || 'Parent';
  }
}

class Child extends Parent {
  constructor(name) {
    super(name);
  }
}

const childInstance = new Child('Child');
console.log(childInstance.name); // 输出 'Child'

优势: 语法更加简洁,易读易写。

缺点: 底层仍然是基于原型链的继承。

100+小程序源码关注公众号回复 5 获取(不想看激励视频的可私信)

alt

本文由 mdnice 多平台发布

http://www.tj-hxxt.cn/news/24238.html

相关文章:

  • 网站关键词长度网站建设的方法有哪些
  • wordpress多网站整合在一个后台网站策划是什么
  • 国家企业信用信息公示网官方seo整体优化
  • dz网站数据备份恢复最近疫情最新消息
  • 搬瓦工vps做网站速度怎么样百度引流免费推广怎么做
  • 已有域名 做网站网络广告网站
  • 重庆网站托管外包公司哪家好推广链接怎么自己搞定
  • asp.net网站开发四酷全书种子搜索神器
  • 网站外链建设seo tdk
  • 网站做多久搜索引擎优化的定义是什么
  • 一般做美食网站的产品需求职业技能培训机构
  • 西安网站建设工程常德网站建设制作
  • 制作宣传册用什么app沈阳专业seo关键词优化
  • 怎样低成本做网站推广网络推广大概需要多少钱
  • 沈阳网站建设小工作室广州企业推广
  • 营销型网站建设考试答案推广普通话宣传标语
  • 数商云是外包吗关键词优化seo公司
  • 中企动力科技股份有限公司厦门分公司沙洋县seo优化排名价格
  • 网站开发的运行可行性专业公司网络推广
  • 购物商城英语seo综合查询网站源码
  • 做网站商城怎么样百度人工服务24小时电话
  • 做网站页面一般用什么软件友情链接实例
  • 中交建设招标有限公司网站360提交入口网址
  • 广西自治区政府网站建设要求太原关键词排名优化
  • 网页设计免费模板图片关键词排名优化易下拉技术
  • 网站开发是网站后台开发吗经典营销案例
  • nodejs做网站能保护源代码吗网络营销与直播电商专业介绍
  • 微博网站开发友情手机站
  • 网站备案好弄吗推广计划书范文
  • 做网站确定什么主题好搜索引擎优化英文简称