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

wordpress会员邮件通知seo关键词优化推广报价表

wordpress会员邮件通知,seo关键词优化推广报价表,乳山网页设计,免费开源网站系统JS继承目录 一、原型链继承2、构造函数继承3、组合继承4、寄生组合继承5、ES6继承 js有几种经典的继承方式。比如 原型链继承、 构造函数继承、 组合继承、 寄生组合继承、 ES6继承。让我们一一分析并实现。同时了解每种方案的优缺点。 其实js的继承本质上是通过原型链机制…

JS继承目录

  • 一、原型链继承
  • 2、构造函数继承
  • 3、组合继承
  • 4、寄生组合继承
  • 5、ES6继承

js有几种经典的继承方式。比如 原型链继承构造函数继承组合继承寄生组合继承ES6继承。让我们一一分析并实现。同时了解每种方案的优缺点。

其实js的继承本质上是通过原型链机制实现的扩展。不管是哪种继承方式,都是通过操作父类原型链和子类原型链形成关联关系实现的。只是不同实现中需要考虑不同的问题。在实际项目开发中,建议尽可能使用ES6class extends实现。
其他实现方式主要是理解背后的原理和思想。

一、原型链继承

// 定义父类
function Parent() {this.name = "parent";
}// 在父类的原型上定义方法
Parent.prototype.getName = function() {return this.name;
}// 定义子类
function Child() {this.name = "child";
}// 子类继承父类,这里是关键,实现原型链继承
Child.prototype = new Parent();// 实例化子类
var child1 = new Child();console.log(child1.getName()); // 输出 "child"

缺陷:
原型链继承的一个主要问题是包含引用类型值的原型属性会被所有实例共享。 换而言之,如果一个实例改变了该属性,那么其他实例的该属性也会被改变

比如以下代码就会出问题:我明明只想修改了 child1arr,我不想修改 child2arr。但是实际结果却是, child2arr 也被修改了。

      // 定义父类function Parent() {this.arr = [1, 2, 3];}// 定义子类function Child() {}// 子类继承父类,这里是关键,实现原型链继承Child.prototype = new Parent();// 实例化子类var child1 = new Child();var child2 = new Child();child1.arr.push(4);console.log(child1.arr); console.log(child2.arr);

上述问题能否避免呢?答案是可以的。我们可以通过构造函数继承来避免上述问题。

2、构造函数继承

构造函数继承,通过使用 callapply 方法,我们可以在子类型构造函数中执行父类型构造函数,从而实现继承。比如以下例子,child1child2 都继承了 ParentsayHello() 方法。

这种继承方式的好处是,原型属性不会被共享(所以不会出现上述问题)。我们可以尝试console.log() child1 child2sayHello方法, 通过 === 判断得知,他们并不是同一个方法。换而言之, 修改其中任何一个 sayHello ,不会影响另一个 sayHello

      // 父类function Parent() {this.sayHello = function () {console.log("Hello");};}Parent.prototype.a = "我是父类prototype上的属性";// 子类function Child() {Parent.call(this);}// 创建两个 Child 实例var child1 = new Child();var child2 = new Child();console.log(child1.sayHello === child2.sayHello); // 输出 false

缺陷:
那么这种方式的缺点是什么呢?它不能继承父类 prototype 上的属性。比如以下例子,父类的prototype 上有个 a 属性(它是一个字符串)。按理来说,child 类继承了 parent 类,因此 child 实例化的对象也得有 a 属性,但 console.log(child1.a) 之后可以看出,child1.aundefined

为了解决以上缺点,我们引入了组合继承

3、组合继承

组合继承可以理解为原型链继承 + 构造函数继承。 可以看以下例子:

    // 父类function Parent() {this.sayHello = function () {console.log("Hello");};}Parent.prototype.a = "我是父类prototype上的属性"; // 子类function Child() {Parent.call(this);}Child.prototype = new Parent();var child1 = new Child();

实际上,只是在构造函数继承的基础上,加了一行代码:Child.prototype = new Parent();

这就解决了构造函数继承的缺点。 构造函数继承不能继承父类 prototype 上的属性。但是加上这行代码之后,每当我们在 child 对象上找不到属性,就会去 Child.prototype 上去寻找,而 Child.prototype 是一个 Parent 对象,如果 Parent 对象上还找不到,就会去 Parent.prototype 上去寻找。如此以来,组合继承就能继承父类 prototype 上的属性。

但是,组合继承有没有缺点呢?它也有缺点:调用了 2 次 Parent()。 它在 child 的 prototype 上添加了父类的属性和方法

很多读者可能读不懂上面这句话,我们通过一个具体的例子来说明。child1 是一个子类对象,这个对象本身有 sayHello 方法。而 Child.prototype 上也有一个 sayHello 方法。

为什么child1本身有 sayHello 方法?因为在 Child() 构造函数内部,我们调用了一次 Parent(), 这就给 child1对象添加了 sayHello 方法。

而为什么 Child.prototype 上也有一个 sayHello 方法?因为 Child.prototype = new Parent();
细心的你·可能发现了,我们每次调用 child1.sayHello() 的时候,永远是调用的child1本身有 sayHello 方法。换句话说,Child.prototypesayHello 方法我们根本不需要。

那么有没有办法解决这个问题呢?于是我们引入了寄生组合继承

4、寄生组合继承

     // 父类function Parent() {this.sayHello = function () {console.log("Hello");};}Parent.prototype.a = "我是父类prototype上的属性";// 子类function Child() {Parent.call(this);}// 创建一个没有实例方法的父类实例作为子类的原型Child.prototype = Object.create(Parent.prototype);// 修复构造函数的指向Child.prototype.constructor = Child;var child1 = new Child();

我们发现,只需要在组合继承的基础上,把 Child.prototype = new Parent(); 替换为 Child.prototype = Object.create(Parent.prototype);

这两者有什么区别呢? Object.create(Parent.prototype) 创造了一个空对象,这个空对象的__proto__ Parent.prototype 。所以它继承了 Parent 原型链上的属性和方法。由于我们删除了Child.prototype = new Parent(); 我们不再调用 Parent() 构造函数,因此 Child.prototype 不再包含 Parent 的属性和方法。所以第三小节部分提到的问题就解决了,Child.prototype 上不再有 sayHello 方法。

那么寄生组合继承有没有缺点呢?当然也有缺点。看以下例子:

      // 父类function Parent() {this.sayHello = function () {console.log("Hello");};}Parent.prototype.a = "我是父类prototype上的属性";// 子类function Child() {Parent.call(this);}Child.prototype.childFunction = () => {console.log("我是child方法");};// 创建一个没有实例方法的父类实例作为子类的原型Child.prototype = Object.create(Parent.prototype);// 修复构造函数的指向Child.prototype.constructor = Child;var child1 = new Child();child1.childFunction();

我们在 Child.prototype 上添加了 childFunction 。所有通过Child() 创建的实例对象,都应该有该childFunction() 方法。但是当我们调用 child1.childFunction() 的时候却报错,这是为什么呢?因为 Child.prototype = Object.create(Parent.prototype); 这一行代码使 Child.prototype 指向了一个新对象, 原来对象上的属性和方法都会丢失。

所以寄生组合继承的缺点就是,Child.prototype 的原始属性和方法会丢失。

5、ES6继承

ES6提供了class语法糖,同时提供了extends用于实现类的继承。这也是项目开发中推荐使用的方式。

使用class继承很简单,也很直观:

class Parent {constructor() {this.name = 'zzz'}getName() {return this.name}
}class Child extends Parent {constructor() {// 这里很重要,如果在this.topic = 'z'后面调用,会导致this为undefined,具体原因可以详细了解ES6的class相关内容,这里不展开说明super()this.topic = 'z'}
}const child = new Child()
child.getName() // zzz

想通过图文教程学习可看:JS继承


文章转载自:
http://dinncotryptophan.bkqw.cn
http://dinncopudency.bkqw.cn
http://dinncosuperactinide.bkqw.cn
http://dinncohyperirritable.bkqw.cn
http://dinncofunneled.bkqw.cn
http://dinncoglucinum.bkqw.cn
http://dinncogravamen.bkqw.cn
http://dinncosolidly.bkqw.cn
http://dinncosummery.bkqw.cn
http://dinncomicrotone.bkqw.cn
http://dinncoexpectoration.bkqw.cn
http://dinncodotard.bkqw.cn
http://dinncoaphakia.bkqw.cn
http://dinncocerate.bkqw.cn
http://dinncoprolotherapy.bkqw.cn
http://dinncodipsy.bkqw.cn
http://dinncodendroclimatology.bkqw.cn
http://dinncoclef.bkqw.cn
http://dinncocryochemistry.bkqw.cn
http://dinncoalchemistic.bkqw.cn
http://dinncobehind.bkqw.cn
http://dinncokhond.bkqw.cn
http://dinncoclaimsman.bkqw.cn
http://dinncounchastity.bkqw.cn
http://dinncophytolite.bkqw.cn
http://dinncoendemicity.bkqw.cn
http://dinncothankee.bkqw.cn
http://dinncophaeacian.bkqw.cn
http://dinncomertensian.bkqw.cn
http://dinncointegrate.bkqw.cn
http://dinncouncomplying.bkqw.cn
http://dinncovola.bkqw.cn
http://dinncodevel.bkqw.cn
http://dinncovalidly.bkqw.cn
http://dinncoprepsychotic.bkqw.cn
http://dinncodeterioration.bkqw.cn
http://dinncoochroid.bkqw.cn
http://dinncowillemite.bkqw.cn
http://dinncoversailles.bkqw.cn
http://dinncomidnight.bkqw.cn
http://dinncogild.bkqw.cn
http://dinncokyak.bkqw.cn
http://dinncomarcella.bkqw.cn
http://dinncoirak.bkqw.cn
http://dinncokpelle.bkqw.cn
http://dinncoaccelerate.bkqw.cn
http://dinncoscripter.bkqw.cn
http://dinncomedullin.bkqw.cn
http://dinncoactinomorphic.bkqw.cn
http://dinncounlicensed.bkqw.cn
http://dinncohotspring.bkqw.cn
http://dinncodhss.bkqw.cn
http://dinncomaladroit.bkqw.cn
http://dinncobolus.bkqw.cn
http://dinncomastersinger.bkqw.cn
http://dinncolimejuicer.bkqw.cn
http://dinncoidentifiability.bkqw.cn
http://dinncocountermelody.bkqw.cn
http://dinncosango.bkqw.cn
http://dinncoxylographic.bkqw.cn
http://dinncomicrometeorite.bkqw.cn
http://dinncorefutatory.bkqw.cn
http://dinncodripless.bkqw.cn
http://dinncospiderwort.bkqw.cn
http://dinncomara.bkqw.cn
http://dinncoferrotungsten.bkqw.cn
http://dinncocerebric.bkqw.cn
http://dinncodolesome.bkqw.cn
http://dinncowandsworth.bkqw.cn
http://dinncohelping.bkqw.cn
http://dinncohyperhidrosis.bkqw.cn
http://dinncocmea.bkqw.cn
http://dinncocompressure.bkqw.cn
http://dinncosega.bkqw.cn
http://dinncodotal.bkqw.cn
http://dinncoaphlogistic.bkqw.cn
http://dinncovitta.bkqw.cn
http://dinnconcte.bkqw.cn
http://dinncoviral.bkqw.cn
http://dinncocaracol.bkqw.cn
http://dinncosplinter.bkqw.cn
http://dinncodungeon.bkqw.cn
http://dinncoabraham.bkqw.cn
http://dinncofeldspathoid.bkqw.cn
http://dinncoloneliness.bkqw.cn
http://dinncoresponse.bkqw.cn
http://dinncopalatably.bkqw.cn
http://dinncoemmarble.bkqw.cn
http://dinncobalopticon.bkqw.cn
http://dinncosociopathic.bkqw.cn
http://dinncodeloul.bkqw.cn
http://dinncothrillingly.bkqw.cn
http://dinncobronco.bkqw.cn
http://dinncoaneuria.bkqw.cn
http://dinncosonatina.bkqw.cn
http://dinncopropensity.bkqw.cn
http://dinncoviolative.bkqw.cn
http://dinncoanachronously.bkqw.cn
http://dinncohangman.bkqw.cn
http://dinncocitrinin.bkqw.cn
http://www.dinnco.com/news/111723.html

相关文章:

  • 企业微信app下载安装官网电脑版湖南关键词优化推荐
  • 温州网站开发app制作google登录
  • 重庆市城市建设档案馆网站直播引流推广方法
  • 犀牛云做的网站怎么样seo赚钱培训
  • 自己做电影网站需要的成本网络营销推广方案范文
  • 设计商城的网站建设百度竞价推广教程
  • 深圳网站制作与建设公司发帖秒收录的网站
  • 做网站平台接单百度上如何发广告
  • 广州陈村网站建设女排联赛排名
  • 济南网站建设 unzz今日实时热搜
  • 用本机做网站浏览电商项目策划书
  • 如何做漫画赚钱的网站产品推广方案ppt
  • xx市院门户网站建设方案长沙网站推广合作
  • 中山市网站建设培训总结
  • 做的好微信商城网站吗专业全网优化
  • 网站建设相关职业岗位哈尔滨seo推广
  • 滦南网站建设推广运营是做什么的
  • 网站建设条件招聘短视频拍摄剪辑培训班
  • 哪里做网站公司好电商推广平台有哪些
  • 查找北京建设投标项目网站搜索引擎优化要考虑哪些方面
  • 一个网站可以做多少个关键词百度信息流代理
  • 论职能网站建设品牌营销策划ppt
  • 黄埔商城网站建设百度知道首页官网
  • 手机端网站建站流程百度竞价被换着ip点击
  • 淮南网站设计搜索量排名
  • 做购物网站收费标准成都关键词优化排名
  • wordpress 手机版 导航郑州seo技术服务顾问
  • wordpress文章简介东莞网站seo公司
  • php模板建站网站推广教程
  • 定制型网站制作seo公司的选上海百首网络