当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- GUI engine evaluation index
- The method of realizing high SLO on large scale kubernetes cluster
- DC-1靶機
- Humor: hacker programming is actually similar to machine learning!
- Take you to learn the new methods in Es5
- Cglib 如何实现多重代理?
- CloudQuery V1.2.0 版本发布
- How to get started with new HTML5 (2)
- 文件过多时ls命令为什么会卡住?
- IPFs rudder filecoin landing at the same time, fil currency price broke a thousand
猜你喜欢
Filecoin has completed a major upgrade and achieved four major project progress!
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
事件监听问题
给字节的学姐讲如何准备“系统设计面试”
What are the common problems of DTU connection
Diamond standard
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
ado.net和asp.net的关系
小游戏云开发入门
Git rebase is in trouble. What to do? Waiting line
随机推荐
CloudQuery V1.2.0 版本发布
快速排序为什么这么快?
Ronglian completed US $125 million f round financing
StickEngine-架构11-消息队列(MessageQueue)
GUI engine evaluation index
Use modelarts quickly, zero base white can also play AI!
What are the common problems of DTU connection
Flink's datasource Trilogy 2: built in connector
What knowledge do Python automated testing learn?
Analysis of ThreadLocal principle
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
2020年第四届中国 BIM (数字建造)经理高峰论坛即将在杭举办
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
Network programming NiO: Bio and NiO
Filecoin has completed a major upgrade and achieved four major project progress!
Jmeter——ForEach Controller&Loop Controller
如何对数据库账号权限进行精细化管理?
What is the purchasing supplier system? Solution of purchasing supplier management platform
DC-1靶機
Introduction to the structure of PDF417 bar code system