当前位置:网站首页>ES6 类
ES6 类
2022-07-31 14:55:00 【Mr_Bobcp】
前言
JavaScript在 ES6 引入的类的概念无疑让这门语言拥有了更贴近于传统面向对象编程语言的编写模式
类的声明
ES6引入了 class 关键字来进行类的声明,类的声明可以直接使用class关键字和类名组合声明,也可以是使用表达式的形式声明。
1、类声明和即将要讲解的类表达式都不会被提升;
2、类中的代码在执行时,会强制开启严格模式;
3、类的所有方法都不可枚举,并且不能与new组合使用。
使用class关键字和类名直接声明
// 在ES5及以前中,没有类的概念,通过构造函数和原型混合模拟定义‘类’,转化一下的写法如下
// function Person(aAge, aName) {
// this.age = aAge;
// this.name = aName;
// }
// Person.prototype.showName = function () {
// console.log(this.name);
// }
// 类的声明
class Person {
// 构造函数
constructor(aAge, aName) {
this.age = aAge;
this.name = aName;
}
// 等同于Person.prototype.showName
showName(){
console.log(this.name);
}
}
// 执行输出:Bob
let person = new Person(18, 'Bob');
person.showName();
表达式声明
let Person = class {
// 构造函数
constructor(aAge, aName) {
this.age = aAge;
this.name = aName;
}
// 等同于Person.prototype.showName
showName(){
console.log(this.name);
}
}
- 每个类有且只有一个构造函数:constructor(),如果未显式声明,则默认一个空的构造函数以供调用(与C++类似)。
- 类的使用consturctor方法名字定义构造函数
- 自有(私有)属性和方法一般在consturctor中定义
类的成员
类的成员包含普通的原型方法或自有属性,还有特殊功能的构造函数、生成器、静态方法和访问器属性等,并且成员名可以是表达式。
自有属性
类中的自有属性一般在构造函数中执行初始化。
class People {
constructor() {
this.name = "Bob";
}
}
访问器属性
访问器属性通过关键字 get 和 set 创建,可以用来控制属性的读写。
class Person {
constructor(aAge, aName) {
this._age = aAge;
this._name = aName;
}
showName() {
console.log(this.name);
}
// age 可以读写
get age() {
return this._age;
}
set age(value) {
this._age = value;
}
// name只读
get name() {
return this._name;
}
}
let person = new Person(18, 'Bob');
console.log(person.age); // 18
person.age = 20;
console.log(person.age);// 20
console.log(person.name); // Bob
person.name = 'CP';
console.log(person.name);// Bob
静态方法
ES6新增了static关键字,可把类中的方法(除了构造函数)定义成静态的,注意ES6无静态属性,只有静态方法。
class Person {
// 构造函数
constructor(aAge, aName) {
this.age = aAge;
this.name = aName;
}
static sTest(){
console.log('sTest');
}
}
Person.sTest(); // sTest
类的继承
ES6提供了 extends 关键字实现类的继承。
代码示例:Student类通过关键字 extends 继承Person类,Student类称为派生类,Person类称为基类。
class Person {
constructor(aAge, aName) {
this.age = aAge;
this.name = aName;
}
run() {
console.log(this.name + ' is running...');
}
}
class Student extends Person{
constructor(aAge, aName, aId){
super(aAge, aName);
this.id = aId;
}
}
let student = new Student(18, 'Bob','001');
student.run(); //Bob is running...
- 派生类定义了构造函数,则必需且需要在this之前调用super();
- 派生类没有定义构造函数,在派生类实例化的时候实例会自动调用super()并传入所有参数;
class Person {
constructor(aAge, aName) {
this.age = aAge;
this.name = aName;
}
run() {
console.log(this.name + ' is running...');
}
}
class Teacher extends Person {
// 无构造函数
}
// 等价于
// class Teacher extends Person{
// constructor(...args){
// super(...args)
// }
// }
let teacher = new Teacher(28, 'CP', '002');
teacher.run(); // CP is running...
- 派生类中可以重写基类的方法
class Person {
constructor(aAge, aName) {
this.age = aAge;
this.name = aName;
}
run() {
console.log(this.name + ' is running...');
}
}
class Student extends Person {
constructor(aAge, aName, aId) {
super(aAge, aName);
this.id = aId;
}
// 覆盖Person.prototype.run()方法
run() {
console.log(this.id + this.name + ' is running...');
}
}
let student = new Student(18, 'Bob', '001');
student.run();// 001Bob is running...
- 派生类中可以用super调用基类方法
class Person {
constructor(aAge, aName) {
this.age = aAge;
this.name = aName;
}
run() {
console.log(this.name + ' is running...');
}
}
class Student extends Person {
constructor(aAge, aName, aId) {
super(aAge, aName);
this.id = aId;
}
study() {
super.run();
console.log(this.name + ' is studying...');
}
}
let student = new Student(18, 'Bob', '001');
student.study();
//Bob is running...
//Bob is studying...
边栏推荐
猜你喜欢

"Listen to me, thank you" can be said in ancient poetry?Tsinghua University has developed an artifact of "Searching Sentences According to Meaning", which can search for the famous sayings you want wi

基于最小二乘法和SVM从天气预报中预测太阳能发电量(Matlab代码实现)

Sentinel流量控制

OAuth2:搭建授权服务器

OpenShift 4 - 定制 RHACS 安全策略,阻断生产集群使用高风险 Registry

如何进行需求分析评审

Five dimensions to start MySQL optimization

TRACE32——常用操作

Redis与分布式:哨兵模式

49. The copy constructor and overloaded 】
随机推荐
Nuget打包并上传教程
RecyclerView高效使用第二节
Nuget打包并上传教程
435. 无重叠区间
R语言ggstatsplot包ggbarstats函数可视化条形图、并添加假设检验结果(包含样本数、统计量、效应大小及其置信区间、显著性、组间两两比较、贝叶斯假设)、检验结果报告符合APA标准
易驱线主控芯片对比(电动三轮电机90O瓦世纪通达)
TRACE32——常用操作
charles进行弱网测试(app弱网测试怎么做)
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化箱图、使用font函数自定义图例标题文本(legend.title)字体的大小、颜色、样式(粗体、斜体)
STM32(十)------- SPI通信
thread_local 变量的析构顺序
In the future, the interviewer asks you why it is not recommended to use Select *, please answer him out loud!
R语言ggplot2可视化:使用ggpubr包的ggboxplot函数可视化分组箱图、使用ggpar函数改变图形化参数(caption、添加、修改可视化图像的题注、脚注内容)
三角恒等变换公式
How to clean up the lodash.memoize cache in the element-plus virtual table virtual-list component?
Small test knife: Go reflection helped me convert Excel to Struct
Ubantu专题5:设置静态ip地址
C language basic practice (nine-nine multiplication table) and printing different asterisk patterns
小试牛刀:Go 反射帮我把 Excel 转成 Struct
Uniapp WeChat small application reference standard components