当前位置:网站首页>Class usage and inheritance in ES6
Class usage and inheritance in ES6
2022-06-25 13:22:00 【wendyTan10】
( One )class The goal is to make defining classes easier
Let's review the implementation of functions Peopel
Methods :
function Peopel(name) {
this.name = name;
}
Peopel.prototype.hello = function () {
console.log('Hello, ' + this.name + '!');
}
If you use a new class Keywords to write Peopel
, It can be written like this :
// Capitalize the beginning of the class
class Peopel {
// Constructor construction properties
constructor(name) {
this.name = name;
}
// Method
hello() {
console.log('Hello, ' + this.name + '!');
}
}
A comparison shows that ,class The definition of contains constructors constructor
And functions defined on prototype objects hello()( Note that there is no function keyword ), This avoids Student.prototype.hello = function () {...}
This decentralized code .
// Create an instance of a person
var xiaoming = new Peopel(' Xiao Ming ');
console.log(xiaoming.name); // Xiao Ming
xiaoming.hello(); // hello, Xiao Ming !
( Two ) Class inheritance
Inherited parent class , adopt extends To achieve , Method can call directly , Property needs to be called super To carry out
// Parent class
class Peopel {
constructor(name) {
this.name = name;
}
eat() {
console.log(`${
this.name} eat something`);
}
}
// Subclass inheritance Peopel
// Be careful Student The definition of is also class Keyword implementation of , and extends It means that the prototype chain object comes from Peopel
class Student extends Peopel {
constructor(name, number) {
super(name); // call super(), Instead of the parent constructor , Initializes properties common to the parent class
this.number = number;
}
information() {
console.log(` full name :${
this.name}, Student number :${
this.number}`)
}
}
// Subclass inheritance Peopel
class Teach extends Peopel {
constructor(name, major) {
super(name);
this.major = major;
}
teach() {
console.log(` full name :${
this.name}, Major :${
this.major}`)
}
}
example
// Create an instance of a student
const newStudent = new Student('wendy', 101);
console.log(newStudent.name); // wendy
console.log(newStudent.number); // 101
newStudent.information(); // full name :wendy, Student number :101
// Create an instance of a teacher
const newTeacher = new Teach(' Mr. Wen ', ' Chinese language and literature ');
console.log(newTeacher.name); // Mr. Wen
console.log(newTeacher.major); // Chinese language and literature
newTeacher.teach(); // full name : Mr. Wen , Major : Chinese language and literature
边栏推荐
猜你喜欢
Pointer, it has to say that the subject
Django框架——缓存、信号、跨站请求伪造、 跨域问题、cookie-session-token
Fedora 35 部署DNS主从和分离解析 —— 筑梦之路
坡道带来的困惑
Sword finger offer II 028 Flatten multi-level bidirectional linked list
Sword finger offer day 1 stack and queue (simple)
解析数仓lazyagg查询重写优化
Sword finger offer day 3 string (simple)
It's an artifact to launch a website in a few minutes
剑指 Offer 第 1 天栈与队列(简单)
随机推荐
爱可可AI前沿推介(6.25)
Download File blob transcoding
剑指offer 第 3 天字符串(简单)
Discuz仿今日头条模板/Discuz新闻资讯商业版GBK模板
Judge whether it is a mobile terminal
Pointer, it has to say that the subject
三行代码简单修改jar包的项目代码
[machine learning] what is machine learning?
解析數倉lazyagg查詢重寫優化
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
Golang keyboard input statement scanln scanf code example
字符串入门十八讲合集四
Sword finger offer day 2 linked list (simple)
Leetcode: Sword finger offer II 091 Painting house [2D DP]
指针,它那些不得不说的题目
德国举行全球粮食安全团结会议
始终保持疫情防控不放松 营造安全稳定的社会环境
Knowledge of initial C language 2.0
[pit avoidance means "difficult"] to realize editable drag and drop sorting of protable
Sword finger offer day 3 string (simple)