当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform
- In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
- C# 调用SendMessage刷新任务栏图标(强制结束时图标未消失)
- Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
- Description of phpshe SMS plug-in
- 【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
- Unity性能优化整理
- Interpretation of Cocos creator source code: engine start and main loop
- Behind the first lane level navigation in the industry
- Chainlink brings us election results into blockchain everipedia
猜你喜欢

ERD-ONLINE 免费在线数据库建模工具

To Lianyun analysis: why is IPFs / filecoin mining so difficult?

只有1个字节的文件实际占用多少磁盘空间

An article will take you to understand CSS3 fillet knowledge

每个大火的“线上狼人杀”平台,都离不开这个新功能

【转发】查看lua中userdata的方法

一部完整的游戏,需要制作哪些音乐?

EOS founder BM: what's the difference between UE, UBI and URI?

事件监听问题

Git rebase is in trouble. What to do? Waiting line
随机推荐
Asp.Net Core learning notes: Introduction
What are the criteria for selecting a cluster server?
Filecoin has completed a major upgrade and achieved four major project progress!
Summary of front-end performance optimization that every front-end engineer should understand:
What are manufacturing and new automation technologies?
美团内部讲座|周烜:华东师范大学的数据库系统研究
一路踩坑,被迫聊聊 C# 代码调试技巧和远程调试
【字节跳动 秋招岗位开放啦】Ohayoo!放学别走,我想约你做游戏!!!
大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
Ronglian completed US $125 million f round financing
Try to build my mall from scratch (2): use JWT to protect our information security and perfect swagger configuration
What are the common problems of DTU connection
Behind the first lane level navigation in the industry
【應用程式見解 Application Insights】Application Insights 使用 Application Maps 構建請求鏈路檢視
Analysis of serilog source code -- how to use it
CloudQuery V1.2.0 版本发布
How to turn data into assets? Attracting data scientists
意派Epub360丨你想要的H5模板都在这里,电子书、大转盘、红包雨、问卷调查……
How to play sortable JS vuedraggable to realize nested drag function of forms
Live broadcast preview | micro service architecture Learning Series live broadcast phase 3