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

好域名做网站二手物品交易网站设计英文

好域名做网站,二手物品交易网站设计英文,国际新闻最新消息今天简短,德州市建设街小学官方网站Question JavaScript实现继承的方式#xff1f; 包含原型链继承、构造函数继承、组合继承、原型式继承、寄生式继承、寄生组合式继承和ES6 类继承 JavaScript实现继承的方式 在JavaScript中#xff0c;实现继承的方式多种多样#xff0c;每种方式都有其优势和适用场景。以下… 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); // 输出 Parentchild.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 获取不想看激励视频的可私信 本文由 mdnice 多平台发布
http://www.w-s-a.com/news/483742/

相关文章:

  • 太原网站设计排名设计本装修效果图
  • 网站个人中心模板石家庄网站系统开发
  • 优秀的电子商务网站教育公司网站建设文案
  • 网站开发市场成本网站链接推广工具
  • 猪八戒做网站排名常州seo博客
  • wordpress 网站遭篡改如何优化公司的网站
  • 汉中公司做网站网站建设的风格设置
  • 网站建议怎么写怎么做网页连接
  • 站长工具seo综合查询下载安装软件平台搭建包括哪几个方面
  • 做网站怎么存放视频支付功能网站建设
  • 庆阳手机网站设计兰州网站的优化
  • 企业网站托管有必要吗项目管理资格证书
  • 检索类的网站建设个人博客网页模板图片
  • 贵阳网站建设搜q479185700做网站有什么语言好
  • 制作公司主页网站贵阳网站建设技术托管
  • 广西建设网站网址多少钱南京江北新区地图
  • 网站建设及优化 赣icp外包服务美剧
  • wordpress添加菜单深圳优化网站排名
  • 免费下载建设银行官方网站重点专业建设验收网站
  • 建行官方网站登录怎样制作悬浮的WordPress
  • 建设一个网站需要几个角色广告设计与制作就业前景
  • 侵入别人的网站怎么做怎么修改网站排版
  • 网站如何提交百度收录什么最便宜网站建设
  • 商丘网站建设想象力网络做公司网站需要准备什么
  • 滁州新手跨境电商建站哪家好网站推广运作怎么做
  • 烟台有没有做网站大连建设工程信息网专家库
  • 网站建设明确细节商贸有限公司的经营范围
  • 南宁微网站开发做的好的有哪些网站
  • 好的素材下载网站读书网网站建设策划书
  • 东莞南城网站建设wordpress用户投稿插件