当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- 如何对数据库账号权限进行精细化管理?
- [Xinge education] poor learning host computer series -- building step 7 Simulation Environment
- C語言I部落格作業03
- 小游戏云开发入门
- An article will take you to understand SVG gradient knowledge
- Digital city responds to relevant national policies and vigorously develops the construction of digital twin platform
- Tron smart wallet PHP development kit [zero TRX collection]
- 【学习】接口测试用例编写和测试关注点
- How to hide part of barcode text in barcode generation software
- 华为云微认证考试简介
猜你喜欢

The AI method put forward by China has more and more influence. Tianda et al. Mined the development law of AI from a large number of literatures

Small program introduction to proficient (2): understand the four important files of small program development

【ElasticSearch搜索引擎】

From overseas to China, rancher wants to do research on container cloud market

CloudQuery V1.2.0 版本发布

How to understand Python iterators and generators?

Pn8162 20W PD fast charging chip, PD fast charging charger scheme
![Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]](/img/14/ede1ffa7811dbc2a5b15b9a7b17a5e.jpg)
Network security engineer Demo: the original * * is to get your computer administrator rights! [maintain]

What is alicloud's experience of sweeping goods for 100 yuan?

新建一个空文件占用多少磁盘空间?
随机推荐
Summary of front-end performance optimization that every front-end engineer should understand:
The method of realizing high SLO on large scale kubernetes cluster
大会倒计时|2020 PostgreSQL亚洲大会-中文分论坛议程安排
华为云微认证考试简介
CCR coin frying robot: the boss of bitcoin digital currency, what you have to know
游戏开发中的新手引导与事件管理系统
An article will take you to understand CSS alignment
[C] (original) step by step teach you to customize the control element - 04, ProgressBar (progress bar)
What is the purchasing supplier system? Solution of purchasing supplier management platform
To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
Unity性能优化整理
Analysis of serilog source code -- how to use it
How to play sortable JS vuedraggable to realize nested drag function of forms
理解格式化原理
PHP application docking justswap special development kit【 JustSwap.PHP ]
How to get started with new HTML5 (2)
CloudQuery V1.2.0 版本发布
Application of restful API based on MVC
How to understand Python iterators and generators?