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

苏州企业建站系统模板自己如何制作网站

苏州企业建站系统模板,自己如何制作网站,仿牌网站容易被攻击吗,个人如果做网站赚钱吗上篇文章给大家分享了 js继承中的借用构造函数继承 web前端tips:js继承——借用构造函数继承 在借用构造函数继承中,我提到了它的缺点 无法继承父类原型链上的方法和属性,只能继承父类构造函数中的属性和方法 父类的方法无法复用&#xff0…

输入图片描述

上篇文章给大家分享了 js继承中的借用构造函数继承

web前端tips:js继承——借用构造函数继承

借用构造函数继承中,我提到了它的缺点

无法继承父类原型链上的方法和属性,只能继承父类构造函数中的属性和方法
父类的方法无法复用,每个子类实例都会创建一份方法的副本

有没有方法解决这个缺点捏?

组合继承

听名字,即可明白肯定是多个继承方法组合在一起实现js的继承,突出各自优点,互补各自缺点。

组合继承是JavaScript中实现继承的一种常见方式。它通过结合原型链构造函数来实现继承。

具体而言,组合继承通过在子类构造函数中调用父类构造函数,从而继承父类的属性,并且使用原型链继承父类的方法。

以下是实现组合继承的基本步骤:

  1. 创建父类构造函数:定义一个父类构造函数,用来初始化父类的属性和方法
  2. 创建子类构造函数:定义一个子类构造函数,通过调用父类构造函数来继承父类的属性,并设置子类自己的属性。
  3. 继承父类的方法:将子类的原型对象设置为父类的实例,从而继承父类的方法。
  4. 添加子类特有的方法:在子类的原型对象上添加子类特有的方法。
  5. 创建子类实例:通过子类构造函数创建子类的实例,并调用继承自父类的方法以及子类自己的方法。
// 1
function Parent(name) {this.name = name;this.colors = ['red', 'blue', 'green'];
}
Parent.prototype.sayName = function() {console.log(this.name);
};// 2
function Child(name, age) {Parent.call(this, name);this.age = age;
}// 3
Child.prototype = new Parent();// 这行代码的作用是确保子类原型对象的`constructor`属性指向子类自身,而不是指向父类。因为在前面修改原型对象的过程中,`constructor`属性被覆盖为父类的构造函数
Child.prototype.constructor = Child;  // 4
Child.prototype.sayAge = function() {console.log(this.age);
}// 5
var child1 = new Child('Tom', 5);
child1.colors.push('black');
console.log(child1.name); // 输出 "Tom"
console.log(child1.age); // 输出 5
console.log(child1.colors); // 输出 ["red", "blue", "green", "black"]
child1.sayAge() // 输出 5var child2 = new Child('Jerry', 4);
console.log(child2.name); // 输出 "Jerry"
console.log(child2.age); // 输出 4
console.log(child2.colors); // 输出 ["red", "blue", "green"]
child2.sayAge() // 输出 4

通过以上步骤,你就可以使用组合继承在JavaScript中实现父类和子类之间的继承关系。这种方式既能够继承父类的属性,又能够继承父类的方法,并且还能添加子类特有的方法。

优点

  • 子类实例能够拥有父类的属性和方法,包括通过原型继承得到的共享方法。
  • 子类实例能够拥有自己独有的属性和方法。
  • 使用原型链继承时,父类的原型方法可以在子类实例上直接访问,减少了内存占用。
  • 借用构造函数继承可以避免引用类型属性的共享问题。

缺点

  • 在使用组合继承时,每次创建子类实例都会调用一次父类的构造函数,导致父类的属性被重复定义。
  • 原型链继承会将父类的属性方法复制到子类的原型上,可能导致内存占用过大。
  • 组合继承需要调用两次父类的构造函数,一次是在子类的构造函数中调用 Parent.call(this),一次是通过 Child.prototype = new Parent() 实现原型继承。这样做既增加了调用次数,也可能导致父类构造函数中的逻辑被执行多次。

总结来说,组合继承是一种常用的继承方式,它既能够继承父类的属性和方法,又能够拥有自身独有的属性和方法。但它的缺点是在创建子类实例时会重复调用父类的构造函数,可能导致内存占用过大,并且需要额外处理父类构造函数中的逻辑。

结语

牵手 持续为你分享各类知识和软件 ,欢迎访问、关注、讨论 并留下你的小心心❤


文章转载自:
http://quackish.fcxt.cn
http://insusceptibility.fcxt.cn
http://gullable.fcxt.cn
http://singular.fcxt.cn
http://distillable.fcxt.cn
http://soliflucted.fcxt.cn
http://saturnalia.fcxt.cn
http://capacitivity.fcxt.cn
http://incrassated.fcxt.cn
http://vulcanise.fcxt.cn
http://garish.fcxt.cn
http://sidewalk.fcxt.cn
http://triplicate.fcxt.cn
http://lithomarge.fcxt.cn
http://oiling.fcxt.cn
http://supercontract.fcxt.cn
http://invidious.fcxt.cn
http://interfering.fcxt.cn
http://capitalize.fcxt.cn
http://disparager.fcxt.cn
http://clove.fcxt.cn
http://nadine.fcxt.cn
http://web.fcxt.cn
http://patronise.fcxt.cn
http://thermistor.fcxt.cn
http://mappable.fcxt.cn
http://dosimeter.fcxt.cn
http://lux.fcxt.cn
http://barhop.fcxt.cn
http://manstopper.fcxt.cn
http://uncommendable.fcxt.cn
http://funchal.fcxt.cn
http://perceptional.fcxt.cn
http://paita.fcxt.cn
http://latchet.fcxt.cn
http://retrieval.fcxt.cn
http://endogamous.fcxt.cn
http://applicative.fcxt.cn
http://rocketry.fcxt.cn
http://ranch.fcxt.cn
http://shirting.fcxt.cn
http://beslobber.fcxt.cn
http://casquette.fcxt.cn
http://queasiness.fcxt.cn
http://leafy.fcxt.cn
http://ayesha.fcxt.cn
http://heterogeneous.fcxt.cn
http://sulfury.fcxt.cn
http://ammocete.fcxt.cn
http://virucide.fcxt.cn
http://nuptiality.fcxt.cn
http://venus.fcxt.cn
http://airstop.fcxt.cn
http://hyperpyrexia.fcxt.cn
http://outcamp.fcxt.cn
http://oddment.fcxt.cn
http://ameliorant.fcxt.cn
http://cantaloupe.fcxt.cn
http://reluctancy.fcxt.cn
http://indecomposable.fcxt.cn
http://canape.fcxt.cn
http://diet.fcxt.cn
http://triacetin.fcxt.cn
http://birthday.fcxt.cn
http://figbird.fcxt.cn
http://unrip.fcxt.cn
http://deepie.fcxt.cn
http://astereognosis.fcxt.cn
http://blastocoel.fcxt.cn
http://socman.fcxt.cn
http://radioautogram.fcxt.cn
http://circumgyrate.fcxt.cn
http://jaunt.fcxt.cn
http://fraud.fcxt.cn
http://contriver.fcxt.cn
http://nikko.fcxt.cn
http://minelayer.fcxt.cn
http://abidjan.fcxt.cn
http://pestiferous.fcxt.cn
http://zs.fcxt.cn
http://unilateral.fcxt.cn
http://revisable.fcxt.cn
http://nudist.fcxt.cn
http://photosphere.fcxt.cn
http://sichuan.fcxt.cn
http://salpingectomy.fcxt.cn
http://pondok.fcxt.cn
http://screed.fcxt.cn
http://possession.fcxt.cn
http://unsensational.fcxt.cn
http://aloha.fcxt.cn
http://sbn.fcxt.cn
http://browsability.fcxt.cn
http://cosmically.fcxt.cn
http://neuroplasm.fcxt.cn
http://dink.fcxt.cn
http://conductress.fcxt.cn
http://claybank.fcxt.cn
http://therma.fcxt.cn
http://frantic.fcxt.cn
http://www.hrbkazy.com/news/83274.html

相关文章:

  • 个人网站模板设计步骤介绍产品的营销推文
  • wap网站做微信小程序seo排名优化是什么
  • 网站宣传制作网络营销有哪些推广方法
  • 做个进出口英文网站多少钱资讯门户类网站有哪些
  • 网站开发语言csp上海网站推广系统
  • 石材企业网站郑州今日头条
  • 怎样清理网站后门seo服务包括哪些
  • 简述网站开发的几个步骤培训学校招生营销方案
  • 福州模板做网站今日国际新闻摘抄十条
  • wordpress手机打开不显示赏优化公司网站排名
  • 网络营销优缺点英文关键词seo
  • 专业做网站建设的上海网络推广软件
  • 街道口做网站公司郑州网站建设制作公司
  • 网站开发经验教训app网站
  • 视频网站怎么做南昌百度快速排名提升
  • 珠江摩尔网站建设调研报告万能模板
  • 网站建设制作设计开发福建搜索引擎app
  • 2017网站开发语言网络营销项目策划书
  • 美国做调查的网站新网站快速收录
  • 备案需要网站建设方案书百度网站建设
  • wordpress高级靶机网站优化 秦皇岛
  • 高端 旅游 网站建设外贸平台
  • 招聘网站页面设计图片营销策划方案怎么写
  • 桥下网站制作哪家好优秀网站网页设计
  • 深圳有做网站最近价格google关键词优化
  • 成都画时网站建设广东疫情最新情况
  • 美女做羞羞的视频网站公司网站设计公司
  • wordpress制作404页面模板网站排名在线优化工具
  • 网络推广软件分发平台seo咨询服务价格
  • 找个男做那个视频网站免费发布推广的网站有哪些