当前位置:网站首页>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
边栏推荐
猜你喜欢

關於數據在內存中的存儲下

New Gospel of drug design: Tencent, together with China University of science and technology and Zhejiang University, developed an adaptive graph learning method to predict molecular interactions and

Uncover gaussdb (for redis): comprehensive comparison of CODIS

leetcode - 384. Scramble array
![[flask tutorial] flask overview](/img/e8/d85ac54f3a9eb3b1ab31852761154c.jpg)
[flask tutorial] flask overview

Knowledge of initial C language 2.0

Online service emergency research methodology

AGCO AI frontier promotion (6.25)

指针,它那些不得不说的题目
![Leetcode: Sword finger offer II 091 Painting house [2D DP]](/img/d7/dc8a3522dbd58b4573cfd75497460c.png)
Leetcode: Sword finger offer II 091 Painting house [2D DP]
随机推荐
An article clearly explains MySQL's clustering / Federation / coverage index, back to table, and index push down
Knowledge of initial C language 2.0
Used in time filter (EL table)
Include what you use to save chaotic header files
药物设计新福音:腾讯联合中科大、浙大开发自适应图学习方法,预测分子相互作用及分子性质
KDD 2022 | GraphMAE:自监督掩码图自编码器
Sword finger offer II 032 Effective anagrams
Nova中的api
[pit avoidance means "difficult"] actionref current. Reload() does not take effect
揭秘GaussDB(for Redis):全面对比Codis
Implementation of a small book system
New Gospel of drug design: Tencent, together with China University of science and technology and Zhejiang University, developed an adaptive graph learning method to predict molecular interactions and
Golang keyboard input statement scanln scanf code example
剑指offer 第 3 天字符串(简单)
Openstack learning notes (II)
提高排名的 15 个基本 SEO 技巧
[data visualization] antv L7 realizes map visualization, drilldownlayer drill asynchronously obtains data, and suspends the warning box
解析數倉lazyagg查詢重寫優化
leetcode:918. Maximum sum of circular subarray [reverse thinking + maximum subarray sum]
Pointer, it has to say that the subject