当前位置:网站首页>ES6 learning notes (2): teach you to play with class inheritance and class objects
ES6 learning notes (2): teach you to play with class inheritance and class objects
2020-11-06 20:48:00 【Tell me Zhan to hide】
List of articles
Inherit
Inheritance in programs : Subclasses can inherit some properties and methods of the parent class
class Father {
// Parent class
constructor () {
}
money () {
console.log(100)
}
}
class Son extends Father {
// Subclass inherits parent
}
let son = new Son()
son.money() // 100
son.
super keyword
super Keyword is used to access and call functions on the object's parent class , You can call the constructor of the parent class , You can also call the normal function of the parent class
class Father {
// Parent class
constructor (x, y) {
this.x = x
this.y = y
}
money () {
console.log(100)
}
sum () {
console.log(this.x + this.y)
}
}
class Son extends Father {
// Subclass inherits parent
constructor (x, y) {
super(x, y) // Called the constructor in the parent class
}
}
let son = new Son(1,2)
son.sum() // 3
son.
The characteristics of inheritance :
- In the inheritance , If you instantiate a subclass and output a method , Let's see if the subclass has this method , If so, execute subclasses first ,( Nearby principle )
- In the inheritance , If the subclass doesn't have , To find whether the parent class has this method , If there is , Just execute this method of the parent class
- In a subclass , It can be used super Call the method of the parent element
class Father {
say() {
return ' I am the father element '
}
sing() {
return ' Father element sings a song '
}
}
class Son extends Father {
say() {
console.log(' I'm a child element ')
}
sing() {
console.log(super.sing())
}
}
var son = new Son()
son.say() // I'm a child element
son.sing() //
The child element can inherit the method of the parent element at the same time , Child elements can also extend their own other methods , Subclasses are used in constructors super When calling the constructor of the parent class , Must be placed in subclass of this Previous call
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) {
// utilize super Call the constructor of the parent class
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 Of classes and objects in 4 A note :
- stay ES6 The middle class has no variable promotion , So you have to define the class first , To instantiate an object through a class
- The common properties and methods in the class must be added with this Use
- The inside of the class this Point to the problem
- constructor Inside this Point to instance object , Method this To the callers of this method
summary
This article mainly shares , About class inheritance 、 Inheritance needs to be used extends,super、ES6 Attention points of class and object in .
版权声明
本文为[Tell me Zhan to hide]所创,转载请带上原文链接,感谢
边栏推荐
- Swagger 3.0 brushes the screen every day. Does it really smell good?
- 大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
- 【自学unity2d传奇游戏开发】如何让角色动起来
- 前端未來趨勢之原生API:Web Components
- What are PLC Analog input and digital input
- 解决 WPF 绑定集合后数据变动界面却不更新的问题
- (2) ASP.NET Core3.1 Ocelot routing
- Who says cat can't do link tracking? Stand up for me
- Jmeter——ForEach Controller&Loop Controller
- FastThreadLocal 是什么鬼?吊打 ThreadLocal 的存在!!
猜你喜欢

What are Devops

面试官: ShardingSphere 学一下吧

Gather in Beijing! The countdown to openi 2020

Basic usage of Vue codemirror: search function, code folding function, get editor value and verify in time

美团内部讲座|周烜:华东师范大学的数据库系统研究
![PHP application docking justswap special development kit【 JustSwap.PHP ]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
PHP application docking justswap special development kit【 JustSwap.PHP ]

如何对数据库账号权限进行精细化管理?

What knowledge do Python automated testing learn?
![Tron smart wallet PHP development kit [zero TRX collection]](/img/3b/00bc81122d330c9d59909994e61027.jpg)
Tron smart wallet PHP development kit [zero TRX collection]

Outsourcing is really difficult. As an outsourcer, I can't help sighing.
随机推荐
開源一套極簡的前後端分離專案腳手架
事务的本质和死锁的原理
Tron smart wallet PHP development kit [zero TRX collection]
Analysis of ThreadLocal principle
【学习】接口测试用例编写和测试关注点
Interpretation of Cocos creator source code: engine start and main loop
Introduction to X Window System
An article will take you to understand CSS alignment
How to turn data into assets? Attracting data scientists
Details of dapr implementing distributed stateful service
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3
Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
小游戏云开发入门
Share with Lianyun: is IPFs / filecoin worth investing in?
(1) ASP.NET Introduction to core3.1 Ocelot
The method of realizing high SLO on large scale kubernetes cluster
Summary of front-end performance optimization that every front-end engineer should understand:
Flink's datasource Trilogy 2: built in connector
理解格式化原理
Look! Internet, e-commerce offline big data analysis best practice! (Internet disk link attached)