当前位置: 首页 > news >正文

ES6 class类

基本介绍

1. constructor

constructor()方法是类的默认方法,通过new命令生成对象实例时,自动调用该方法。

一个类必须有constructor()方法,如果没有显式定义,一个空的constructor()方法会被默认添加。如:

class Point {}// 等同于
class Point {constructor() {}
}

类必须使用new调用,否则会报错。如:new Point()

类的属性和方法,除非显式定义在其本身(即定义在this对象上),否则都是定义在原型上(即定义在class上)。

class Point {constructor(x, y) {this.x = x;this.y = y;}toString() {return '(' + this.x + ', ' + this.y + ')';}
}var point = new Point(2, 3);point.toString() // (2, 3)point.hasOwnProperty('x') // true
point.hasOwnProperty('y') // true
point.hasOwnProperty('toString') // false
point.__proto__.hasOwnProperty('toString') // true

实例属性也可以按照如下方法去写:

class IncreasingCounter {_count = 0;get value() {console.log('Getting the current value!');return this._count;}increment() {this._count++;}
}

2. 存取函数

在“类”的内部可以使用get和set关键字。如:

class MyClass {constructor() {}get prop() {return 'getter';}set prop(value) {console.log('setter: '+value);}
}let inst = new MyClass();inst.prop = 123;
// setter: 123inst.prop
// 'getter'

3. 属性表达式

类的属性名,可以采用表达式方式书写。如:

let methodName = 'getArea';class Square {constructor(length) {}[methodName]() {}
}

4. 静态方法和静态属性

类中也可以定义静态方法,静态方法通过关键字static定义。类相当于实例的原型,所有在类中定义的方法,都会被实例继承。但静态方法不会,如果定义了static类型,就表示该方法不会被实例继承,而是直接通过类来调用,

class Foo {static classMethod() {return 'hello';}
}Foo.classMethod() // 'hello'var foo = new Foo();
foo.classMethod()
// TypeError: foo.classMethod is not a function

5. 私有属性

ES6的私有属性是在变量名前加#来定义。私有属性只能在类的内部使用,如果在类的外部使用,就会报错。

6. in运算符

前面说过,直接访问某个类不存在的私有属性会报错,但是访问不存在的公开属性不会报错。这个特性可以用来判断,某个对象是否为类的实例。

继承

ES6中通过extends关键字实现继承。如:

class Point { /* ... */ }class ColorPoint extends Point {constructor(x, y, color) {super(x, y); // 调用父类的constructor(x, y)this.color = color;}toString() {return this.color + ' ' + super.toString(); // 调用父类的toString()}
}

父类所有的属性和方法,都会被子类继承,除了私有的属性和方法。子类无法继承父类的私有属性,或者说,私有属性只能在定义它的 class 里面使用。如:

class Foo {#p = 1;#m() {console.log('hello');}
}class Bar extends Foo {constructor() {super();console.log(this.#p); // 报错this.#m(); // 报错}
}

父类的静态属性和静态方法,也会被子类继承。如:

class A {static hello() {console.log('hello world');}
}class B extends A {
}B.hello()  // hello world

上面代码中,hello()是A类的静态方法,B继承A,也继承了A的静态方法。

注意,静态属性是通过软拷贝实现继承的。

http://www.lryc.cn/news/236763.html

相关文章:

  • Java的IO流-打印流
  • 如何使用$APPEALS法,分析用户期待?
  • 南京工程学院数据结构考试纲要
  • C++中将 sizeof() 用于类
  • Android studio访问选程https接口(.crt handshake)
  • 设计模式(一)-设计原则(1)
  • Linux|僵死进程
  • JDY蓝牙注意事项
  • 服务器IPMI管理操作
  • 自动驾驶学习笔记(九)——车辆控制
  • HTML5学习系列之项目实战1
  • git 提交成了LFS格式,如何恢复
  • 【ISP图像处理】Demosaic去马赛克概念介绍以及相关方法整理
  • 单图像3D重建AI算法综述【2023】
  • three.js相机调用reset方法复原但无法完全复原
  • 前端为什么要工程化
  • 软件测试/测试开发/人工智能丨视觉与图像识别自动化测试
  • nvm的下载与使用
  • 拼多多官方开放平台接口app商品详情接口获取实时商品详情数据演示
  • STM32F4系列单片机GPIO概述和寄存器分析
  • 【LeetCode刷题】--9.回文数
  • 大数据-之LibrA数据库系统告警处理(ALM-12057 元数据未配置周期备份到第三方服务器的任务)
  • 毅速丨嫁接打印在模具制造中应用广泛
  • 『亚马逊云科技产品测评』活动征文|基于next.js搭建一个企业官网
  • C# Winform围棋棋盘
  • vue使用本地图片设置为默认图
  • day17-高速缓冲区的管理机制
  • 文心一言 VS 讯飞星火 VS chatgpt (139)-- 算法导论11.4 3题
  • Java 解压文件
  • SASS/SCSS精华干货教程