当前位置:网站首页>ES6学习笔记(二):教你玩转类的继承和类的对象
ES6学习笔记(二):教你玩转类的继承和类的对象
2020-11-06 20:48:00 【叫我詹躲躲】
文章目录
继承
程序中的继承: 子类可以继承父类的一些属性和方法
class Father {
//父类
constructor () {
}
money () {
console.log(100)
}
}
class Son extends Father {
//子类继承父类
}
let son = new Son()
son.money() // 100
son.
super关键字
super关键字用于访问和调用对象父类上的函数,可以通过调用父类的构造函数,也可以调用父类的普通函数
class Father {
//父类
constructor (x, y) {
this.x = x
this.y = y
}
money () {
console.log(100)
}
sum () {
console.log(this.x + this.y)
}
}
class Son extends Father {
//子类继承父类
constructor (x, y) {
super(x, y) //调用了父类中的构造函数
}
}
let son = new Son(1,2)
son.sum() // 3
son.
继承的特点:
- 继承中,如果实例化子类输出一个方法,先看子类有没有这个方法,如果有就先执行子类,(就近原则)
- 继承中,如果子类里面没有,就去查找父类有没有这个方法,如果有,就执行父类的这个方法
- 在子类中,可以用super调用父类元素的方法
class Father {
say() {
return '我是父元素'
}
sing() {
return '父元素唱一首歌'
}
}
class Son extends Father {
say() {
console.log('我是子元素')
}
sing() {
console.log(super.sing())
}
}
var son = new Son()
son.say() //我是子元素
son.sing() //
子元素可以继承父元素的方法的同时,子元素也可以扩展自己的其他方法,子类在构造函数中用super调用父类的构造方法时候,必须放在子类的this之前调用
class Father {
constructor(x, y) {
this.x = x
this.y = y
}
sum() {
console.log(this.x + this.y)
}
}
class Son extends Father {
constructor(x,y) {
//利用super 调用父类的构造函数
super(x,y)
this.x = x
this.y = y
}
subtract() {
console.log(this.x - this.y)
}
}
let son = new Son(5,3)
son.subtract() // 2
son.sum() //8
ES6中的类和对象的4个注意点:
- 在ES6中类没有变量提升,所以必须先定义类,才能通过类实例化对象
- 类里面的共有属性和方法一定要加this使用
- 类里面的this指向问题
- constructor里面的this指向实例对象,方法里面的this向这个方法的调用者
总结
这篇文章主要分享了,关于类的继承、继承需要的用到的extends,super、ES6中的类和对象的注意点等。
版权声明
本文为[叫我詹躲躲]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/3995971/blog/4558932
边栏推荐
- Relationship between business policies, business rules, business processes and business master data - modern analysis
- How to get started with new HTML5 (2)
- 如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
- Vue 3 responsive Foundation
- ES6 essence:
- CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
- axios学习笔记(二):轻松弄懂XHR的使用及如何封装简易axios
- 一篇文章带你了解CSS3圆角知识
- Python3 e-learning case 4: writing web proxy
- Did you blog today?
猜你喜欢

Use of vuepress

Network security engineer Demo: the original * * is to get your computer administrator rights! 【***】

Character string and memory operation function in C language

Can't be asked again! Reentrantlock source code, drawing a look together!

I'm afraid that the spread sequence calculation of arbitrage strategy is not as simple as you think

Basic principle and application of iptables

Architecture article collection

小程序入门到精通(二):了解小程序开发4个重要文件

Calculation script for time series data

Do not understand UML class diagram? Take a look at this edition of rural love class diagram, a learn!
随机推荐
嘗試從零開始構建我的商城 (二) :使用JWT保護我們的資訊保安,完善Swagger配置
使用 Iceberg on Kubernetes 打造新一代云原生数据湖
In order to save money, I learned PHP in one day!
Can't be asked again! Reentrantlock source code, drawing a look together!
一篇文章带你了解CSS 渐变知识
Python crawler actual combat details: crawling home of pictures
Filecoin的经济模型与未来价值是如何支撑FIL币价格破千的
数字城市响应相关国家政策大力发展数字孪生平台的建设
容联完成1.25亿美元F轮融资
带你学习ES5中新增的方法
What is the difference between data scientists and machine learning engineers? - kdnuggets
在大规模 Kubernetes 集群上实现高 SLO 的方法
人工智能学什么课程?它将替代人类工作?
数据产品不就是报表吗?大错特错!这分类里有大学问
从海外进军中国,Rancher要执容器云市场牛耳 | 爱分析调研
你的财务报告该换个高级的套路了——财务分析驾驶舱
6.2 handleradapter adapter processor (in-depth analysis of SSM and project practice)
Let the front-end siege division develop independently from the back-end: Mock.js
Face to face Manual Chapter 16: explanation and implementation of fair lock of code peasant association lock and reentrantlock
Group count - word length