当前位置:网站首页>ES6-class类
ES6-class类
2022-07-31 05:18:00 【春意迟迟、】
class (类)作为对象的模板被引入,可以通过 class 关键字定义类;class 的本质是 function;类不可重复声明;类定义不会被提升,这意味着必须在访问前对类进行定义,否则就会报错。
js没有类 是原型的思想设计的类。
类名要大写:
<script> //声明类 class Student{} //匿名类 Teacher=class{} </script>
类的属性:ES6的类中不能直接定义变量,变量被定义在constructor中。使用new创建对象时调用
<script> class Student{ constructor(){ this.name="rose" } } let stu1=new Student() console.log(stu1.name) </script>
类的方法:
constructor 方法是类的默认方法,创建类的对象时被调用。也被称为类的构造方法(构造函数、构造器)。一个类中有且仅有一个构造方法。
原型方法:不需要使用function关键字,通过“对象.原型方法”调用。
<script> class Student{ name(name){ console.log(`my name is ${name}`) } } let stu1=new Student() stu1.name("rose") </script>
静态方法:使用static修饰,调用时不需要创建对象,直接通过“类名.静态方法”调用
<script> class Student{ static name(name){ console.log(`my name is ${name}`) } } let stu1=new Student() Student.name("rose") </script>
静态方法调用:类名.静态方法
原型方法调用:对象.原型方法
类的继承:
- 主要解决代码的复用,
- 使用extends关键字实现继承;
- 子类可以继承父类中所有的方法和属性
子类只能继承一个父类(单继承),一个父类可以有多个子类
子类的构造方法中必须有super()来指定调用父类的构造方法,并且位于子类构造方法中的第一行
子类中如果有与父类相同的方法和属性,将会优先使用子类的(覆盖)
<script> class Father{ constructor(){ this.name="father" this.sex="n男" console.log("父类构造方法") } play(){ console.log("父类的原型方法:play") } static sleep(){ console.log("父类的静态方法:sleep") } } class Son extends Father{ constructor(){ super(); this.name1="son" this.sex="男" console.log("子类构造方法") } study(){ console.log("子类的原型方法:study") } static eat(){ console.log("子类的静态方法:eat") } } var son1=new Son(); console.log(son1.name,son1.name1,son1.sex) son1.play() Son.sleep() son1.study() Son.eat() </script>
结果:
子类也可以调用父类的静态方法,但是注意要用类名调用
内部类: 属于外部类的成员,必须通过“外部类.内部类”访问
<script> // 外部类 class Outer { constructor() { console.log("outer"); } } // 内部类 Outer.Inner = class { constructor() { console.log("Inner"); } } new Outer.Inner(); </script>
边栏推荐
- Tensorflow——演示
- Unity软件中UGUI和NGUI的多语言开发
- VRchat_udon脚本介绍:傻瓜式教程教你如何实现VRchat地图的功能
- 解决nx安装 jtop问题
- DSPE-PEG-Azide DSPE-PED-N3 Phospholipid-Polyethylene Glycol-Azide Lipid PFG
- Picture-in-Picture API in the browser
- 5G的用途和工作原理
- mPEG-DSPE 178744-28-0 Methoxy-polyethylene glycol-phosphatidylethanolamine linear PEG phospholipids
- Fluorescein-PEG-DSPE Phospholipid-Polyethylene Glycol-Fluorescein Fluorescent Phospholipid PEG Derivatives
- DingTalk H5 micro-app login authentication
猜你喜欢
浅谈音视频开发入门基础及进阶资源分享
四种常见的POST提交数据方式
Wangeditor rich text editor to upload pictures and solve cross-domain problems
UR3机器人雅克比矩阵
浏览器中的画中画(Picture-in-Picture)API
The content of the wangeditor editor is transferred to the background server for storage
Four common ways of POST to submit data
Websocket协议解析与QT代码示例
2021-09-30
Learn how to get a database connection with JDBC
随机推荐
链表理论基础
顶级程序员都是怎么做的?
钉钉H5微应用免登鉴权
Cholesterol-PEG-Thiol CLS-PEG-SH Cholesterol-Polyethylene Glycol-Sulfhydryl
UE5 最新动态虚幻引擎全新版本引爆互联网
C语言静态变量static
测试CSDN积分需求
Cholesterol-PEG-Amine CLS-PEG-NH2 Cholesterol-Polyethylene Glycol-Amino Research Use
力扣151. 颠倒字符串中的单词
DSPE-PEG-COOH CAS: 1403744-37-5 Phospholipid-polyethylene glycol-carboxy lipid PEG conjugate
2021年开发人员的绊脚石:构建时间长
DSPE-PEG-Azide DSPE-PED-N3 磷脂-聚乙二醇-叠氮脂质PFG
螺旋矩阵Ⅱ
【Latex】TexLive+VScode+SumatraPDF 配置LaTex编辑环境
Unity转微信小游戏与JS交互
Natural language processing related list
Log jar package conflict, and its solution
mPEG-DMPE 甲氧基-聚乙二醇-双肉豆蔻磷脂酰乙醇胺用于形成隐形脂质体
C语言对文件的操作(完整版)
2021-09-30