概述
在ES6中,class (类)作为对象的模板被引入,可以通过 class 关键字定义类。
class 的本质是 function。
它可以看作一个语法糖,让对象原型的写法更加清晰、更像面向对象编程的语法。
基础用法
类定义
类表达式可以为匿名或命名。
类声明
class Example { constructor(a) { this.a = a; } }
注意要点:不可重复声明。
class Example{} class Example{}
注意要点
类定义不会被提升,这意味着,必须在访问前对类进行定义,否则就会报错。
类中方法不需要 function 关键字。
方法间不能加分号。
new Example(); class Example {}
类的主体
属性
prototype
ES6 中,prototype 仍旧存在,虽然可以直接自类中定义方法,但是其实方法还是定义在 prototype 上的。 覆盖方法 / 初始化时添加方法
添加方法
Object.assign(Example.prototype,{
静态属性
静态属性:class 本身的属性,即直接定义在类内部的属性( Class.propname ),不需要实例化。 ES6 中规定,Class 内部只有静态方法,没有静态属性。
公共属性
class Example{} Example.prototype.a = 2;
实例属性
实例属性:定义在实例对象( this )上的属性。
class Example { a = 2; constructor () { console.log(this.a); } }
name 属性
返回跟在 class 后的类名(存在时)。
let Example=class Exam { constructor(a) { this.a = a; } } console.log(Example.name);
方法
constructor 方法
constructor 方法是类的默认方法,创建类的实例化对象时被调用。
class Example{ constructor(){ console.log('我是constructor'); } } new Example();
返回对象
class Test { constructor(){
静态方法
class Example{ static sum(a, b) { console.log(a+b); } } Example.sum(1, 2);
原型方法
class Example { sum(a, b) { console.log(a + b); } } let exam = new Example(); exam.sum(1, 2);
实例方法
class Example { constructor() { this.sum = (a, b) => { console.log(a + b); } } }
类的实例化
new
class 的实例化必须通过 new 关键字。
class Example {} let exam1 = Example();
实例化对象
共享原型对象
class Example { constructor(a, b) { this.a = a; this.b = b; console.log('Example'); } sum() { return this.a + this.b; } } let exam1 = new Example(2, 1); let exam2 = new Example(3, 1);
decorator
decorator 是一个函数,用来修改类的行为,在代码编译时产生作用。
类修饰
一个参数
第一个参数 target,指向类本身。
function testable(target) { target.isTestable = true; } @testable class Example {} Example.isTestable;
多个参数——嵌套实现
function testable(isTestable) { return function(target) { target.isTestable=isTestable; } } @testable(true) class Example {} Example.isTestable;
实例属性
上面两个例子添加的是静态属性,若要添加实例属性,在类的 prototype 上操作即可。
方法修饰
3个参数:target(类的原型对象)、name(修饰的属性名)、descriptor(该属性的描述对象)。
class Example { @writable sum(a, b) { return a + b; } } function writable(target, name, descriptor) { descriptor.writable = false; return descriptor;
修饰器执行顺序
由外向内进入,由内向外执行。
class Example { @logMethod(1) @logMethod(2) sum(a, b){ return a + b; } } function logMethod(id) { console.log('evaluated logMethod'+id); return (target, name, desctiptor) => console.log('excuted logMethod '+id); }
封装与继承
getter / setter
定义
class Example{ constructor(a, b) { this.a = a;
getter 不可单独出现
class Example { constructor(a) { this.a = a; } get a() { return this.a; } } let exam = new Example(1);
getter 与 setter 必须同级出现
class Father { constructor(){} get a() { return this._a; } } class Child extends Father { constructor(){ super(); } set a(a) { this._a = a; } } let test = new Child(); test.a = 2; console.log(test.a);
extends
通过 extends 实现类的继承。
class Child extends Father { ... }
super
子类 constructor 方法中必须有 super ,且必须出现在 this 之前。
class Father { constructor() {} } class Child extends Father { constructor() {}
调用父类构造函数,只能出现在子类的构造函数。
class Father { test(){ return 0; } static test1(){ return 1; } } class Child extends Father { constructor(){ super(); } } class Child1 extends Father { test2() { super();
调用父类方法, super 作为对象,在普通方法中,指向父类的原型对象,在静态方法中,指向父类
class Child2 extends Father { constructor(){ super();
注意要点
不可继承常规对象。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
2022-11-14 Web前端最新优化指标:FP、FCP、LCP、CLS、TTI、FID、TBT、FMP等
2022-11-14 前后端同构和模板渲染的区别是什么呢?
2022-11-14 关于进一步优化新冠肺炎疫情防控措施 科学精准做好防控工作的通知
2022-11-14 冬天能喝绿豆汤吗
2022-11-14 C# iText 7 切分PDF,处理PDF页面大小,添加水印
2022-11-14 C# 图片沿中心点进行角度旋转
2022-11-14 sql server 聚集索引,非聚集索引,Identity ,gudi,主键的概念和比较