当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- Introduction to X Window System
- Unity性能优化整理
- 游戏开发中的新手引导与事件管理系统
- 開源一套極簡的前後端分離專案腳手架
- Description of phpshe SMS plug-in
- 事务的隔离级别与所带来的问题
- html+vue.js 實現分頁可相容IE
- Top 5 Chinese cloud manufacturers in 2018: Alibaba cloud, Tencent cloud, AWS, telecom, Unicom
- Behind the first lane level navigation in the industry
- Use modelarts quickly, zero base white can also play AI!
猜你喜欢
What course of artificial intelligence? Will it replace human work?
文件过多时ls命令为什么会卡住?
代码重构之法——方法重构分析
常用SQL语句总结
MongoDB与SQL常用语法对应表
How to hide part of barcode text in barcode generation software
2020年数据库技术大会助力技术提升
检测证书过期脚本
面试官: ShardingSphere 学一下吧
[Xinge education] poor learning host computer series -- building step 7 Simulation Environment
随机推荐
It is really necessary to build a distributed ID generation service
意外的元素..所需元素..
代码生成器插件与Creator预制体文件解析
ERD-ONLINE 免费在线数据库建模工具
The importance of big data application is reflected in all aspects
What are PLC Analog input and digital input
MongoDB与SQL常用语法对应表
What is the purchasing supplier system? Solution of purchasing supplier management platform
Swagger 3.0 brushes the screen every day. Does it really smell good?
The dynamic thread pool in Kitty supports Nacos and Apollo multi configuration centers
Unity性能优化整理
Multi robot market share solution
Isn't data product just a report? absolutely wrong! There are university questions in this category
常用SQL语句总结
How about small and medium-sized enterprises choose shared office?
What is alicloud's experience of sweeping goods for 100 yuan?
hdu3974 Assign the task線段樹 dfs序
It's time for your financial report to change to a more advanced style -- financial analysis cockpit
Installing ns-3 on ubuntu18.04
Behind the first lane level navigation in the industry