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

济南做网站需要多少钱wordpress 知言

济南做网站需要多少钱,wordpress 知言,外贸推广网站,iis 新建网站 要登录JavaScript继承 1、JS 的继承到底有多少种实现方式呢? 2、ES6 的 extends 关键字是用哪种继承方式实现的呢? 继承种类 原型链继承 function Parent1() {this.name parentlthis.play [1, 2, 3] }function Child1() {this.type child2 }Child1.prototype new Parent1(…

JavaScript继承

1、JS 的继承到底有多少种实现方式呢?
2、ES6 的 extends 关键字是用哪种继承方式实现的呢?
在这里插入图片描述

继承种类

原型链继承

function Parent1() {this.name = 'parentl'this.play = [1, 2, 3]
}function Child1() {this.type = 'child2'
}Child1.prototype = new Parent1();
console.log(new Child1());let child1 = new Child1();
child1.play.push(4)
let child2 = new Child1();
console.log(child1.play,child2.play)

输出:

Parent1 { type: 'child2' }   
[ 1, 2, 3, 4 ] [ 1, 2, 3, 4 ]

特点
父类内存空间是共享的,当一个发生变化的时候,另外一个也随之进行了变化。

构造函数继承(借助 call)

function Parent1() {this.name = 'parent'
}Parent1.prototype.getName = function () {return this.name;
}function Child1() {Parent1.call(this);this.type = 'child1'
}let child = new Child1();
console.log(child);
console.log(child.getName())

输出

Child1 { name: 'parent', type: 'child1' }
C:\Users\liyd\WebstormProjects\test\dataConvert.js:16                                  
console.log(child.getName())                                                           ^                                                                    TypeError: child.getName is not a function    

缺点:
只能继承父类的实例属性和方法。不能继承原型属性和方法。

组合继承方式(推荐)

function Parent3() {this.name = 'parent3'this.play =[1,2,3]
}Parent3.prototype.getName = function () {return this.name;
}function Child3() {Parent3.call(this);this.type = 'child6'
}
// 第一次调用Parent3
Child3.prototype = new Parent3()
// 手动挂上构造器,指向自己的构造函数
Child3.prototype.constructor = Child3var child3 = new Child3();
var child4 = new Child3();
child3.play.push(4)
console.log(child3.play,child4.play)
console.log(child3.getName())
console.log(child4.getName())

输出:

[ 1, 2, 3, 4 ] [ 1, 2, 3 ]
parent3                   
parent3   

原型式继承

let parent4 = {name:"parent4",friends:["p1","p2","p3"],getName:function () {return this.name}
}let person4 = Object.create(parent4)
person4.name = "tom"
person4.friends.push("无始")let person5 = Object.create(parent4);
person5.friends.push("狂蟒")console.log(person4.name)
console.log(person4.name === person4.getName())
console.log(person5.name)
console.log(person4.friends)
console.log(person5.friends)

输出:

tom                                 
true                                
parent4                             
[ 'p1', 'p2', 'p3', '无始', '狂蟒' ]
[ 'p1', 'p2', 'p3', '无始', '狂蟒' ]

寄生式继承

使用原型式继承可以获得一份目标对象的浅拷贝然后利用这个浅拷贝的能力再进行增强添加一些方法

  • 寄生式继承相比于原型式继承还是在父类基础上添加了更多的方法
let parent5 = {name:"parent5",friends:["p1","p2","p3"],getName:function () {return this.name}
}function clone(original) {let clone = Object.create(original)clone.getFriends = function (){return this.friends}return clone
}let person5 = clone(parent5);
let person6 = clone(parent5);
person5.friends.push("666")
console.log(person5.getName())
console.log(person5.getFriends())
console.log(person6.getName())
console.log(person6.getFriends())let person5 = clone(parent5);
console.log(person5.getName())
console.log(person5.getFriends())

输出:

parent5                    
[ 'p1', 'p2', 'p3', '666' ]
parent5                    
[ 'p1', 'p2', 'p3', '666' ]

寄生组合式继承(强烈推荐)

在前面这几种继承方式的优缺点基础上进行改造得出了寄生组合式的继承方式
这也是所有继承方式里面相对最优的继承方式

function clone(parent, child) {// 这里改用 Object.create 就可以减少组合继承中多进行一次构造的过程child.prototype = Object.create(parent.prototype)child.prototype.constructor = child
}function Parent6(){this.name = "parent6"this.play = [1,2,3]
}Parent6.prototype.getName = function () {return this.name
}function Child(){Parent6.call(this)this.friends = 'child6'
}clone(Parent6,Child)Child.prototype.getFriends = function () {return this.friends
}let person6 = new Child()
console.log(person6)
console.log(person6.getName())
console.log(person6.getFriends())

输出:

Child { name: 'parent6', play: [ 1, 2, 3 ], friends: 'child6' }
parent6                                                        
child6  

总结

在这里插入图片描述

extends 实现继承(超推荐 ES6)

语法糖

class Person{constructor(name) {this.name = name}getName = function () {console.log('Person:',this.name)return this.name}
}class Gamer extends Person{constructor(name,age) {super(name);this.age = age}
}let gamer = new Gamer("无始无终",26);
console.log(gamer.getName())

输出:

Person: 无始无终
无始无终 
http://www.yayakq.cn/news/845807/

相关文章:

  • 深圳哪家网站建设服务好Django 个人博客网站开发
  • 中山企业做网站手机做网页的软件叫什么
  • 广州响应式网站百度关键词排名推广
  • 淘宝刷单的网站建设建立网站的模板
  • 双体系建设网站怎样做网站策划
  • 企业网站搭建方案wordpress免费下载
  • 河北pc端网站建设江阴网站开发公司电话
  • 哪些行业做网站推广的多网站设计 尺寸
  • 网站建设客户在哪里找东莞常平美食
  • wordpress网站克隆微信平台网站开发
  • 建设网站能盈利吗辽宁人工智能建站系统软件
  • 页面设计素材网站asp.net 网站开发
  • 做ppt找图片在哪个网站好辽宁世纪兴电子商务服务中心
  • 网站开发的摘要服装网站开发方案
  • 网站建设设计公司哪家好flash制作动画教程
  • 建网站可以用企业qq吗软件定制图片
  • 手机号交易网站源码用家用光纤宽带做网站
  • 站长工具seo综合查询广告惠州网站建设兼职
  • 网上虚拟银行注册网站微信h5页面制作免费软件
  • 网站 栏目管理wordpress建站软件
  • 知名的环保行业网站开发竞价单页网站制作
  • 用asp.net开发网站的优势域名推荐
  • 石家庄便宜网站制作上海网站建设治汇网络
  • 重庆市建设安全监督站的网站电子商务网站建设需求分析报告
  • 建设网站最重要的是什么静海网站建设制作
  • 免费手机端网站模板下载工具建外贸商城网站
  • 网站制作农业高端网站建设的方案
  • 宁乡的网站建设网络营销市场调研的内容
  • 网站建设任务分解青岛制作企业网站的公司
  • 广西柳州住房和城乡建设局网站宝塔面板安装wordpress