类元素

公有字段

静态公有字段和实例公有字段都是可编辑的,可遍历的,可配置的。它们本身不同于私有对应值(private counterparts)的是,它们参与原型的继承。

静态公有字段

静态公有字段在你想要创建一个只在每个类里面只存在一份,而不会存在于你创建的每个类的实例中的属性时可以用到。你可以用它存放缓存数据、固定结构数据或者其他你不想在所有实例都复制一份的数据。
静态公有字段是使用关键字 static 声明的。我们在声明一个类的时候,使用Object.defineProperty方法将静态公有字段添加到类的构造函数中。在类被声明之后,可以从类的构造函数访问静态公有字段。

  1. class ClassWithStaticField {
  2. static staticField = 'static field';
  3. }
  4. console.log(ClassWithStaticField.staticField);
  5. // 预期输出值: "static field"

没有设定初始化程序的字段将默认被初始化为undefined

  1. class ClassWithStaticField {
  2. static staticField;
  3. }
  4. console.assert(ClassWithStaticField.hasOwnProperty('staticField'));
  5. console.log(ClassWithStaticField.staticField);
  6. // 预期输出值: "undefined"

静态公有字段不会在子类里重复初始化,但我们可以通过原型链访问它们。

  1. class ClassWithStaticField {
  2. static baseStaticField = 'base field';
  3. }
  4. class SubClassWithStaticField extends ClassWithStaticField {
  5. static subStaticField = 'sub class field';
  6. }
  7. console.log(SubClassWithStaticField.subStaticField);
  8. // 预期输出值: "sub class field"
  9. console.log(SubClassWithStaticField.baseStaticField);
  10. // 预期输出值: "base field"

当初始化字段时,this指向的是类的构造函数。你可以通过名字引用构造函数,并使用super获取到存在的超类构造函数。

  1. class ClassWithStaticField {
  2. static baseStaticField = 'base static field';
  3. static anotherBaseStaticField = this.baseStaticField;
  4. static baseStaticMethod() { return 'base static method output'; }
  5. }
  6. class SubClassWithStaticField extends ClassWithStaticField {
  7. static subStaticField = super.baseStaticMethod();
  8. }
  9. console.log(ClassWithStaticField.anotherBaseStaticField);
  10. // 预期输出值: "base static field"
  11. console.log(SubClassWithStaticField.subStaticField);
  12. // 预期输出值: "base static method output"

公有实例字段

公有实例字段存在于类的每一个实例中。通过声明一个公有字段,我们可以确保该字段一直存在,而类的定义则会更加像是自我描述。
公有实例字段可以在基类的构造过程中(构造函数主体运行前)使用Object.defineProperty添加,也可以在子类构造函数中的super()函数结束后添加。

  1. class ClassWithInstanceField {
  2. instanceField = 'instance field';
  3. }
  4. const instance = new ClassWithInstanceField();
  5. console.log(instance.instanceField);
  6. // 预期输出值: "instance field"

没有设定初始化程序的字段将默认被初始化为undefined

  1. class ClassWithInstanceField {
  2. instanceField;
  3. }
  4. const instance = new ClassWithInstanceField();
  5. console.assert(instance.hasOwnProperty('instanceField'));
  6. console.log(instance.instanceField);
  7. // 预期输出值: "undefined"

和属性(properties)一样,字段名可以由计算得出。

  1. const PREFIX = 'prefix';
  2. class ClassWithComputedFieldName {
  3. [`${PREFIX}Field`] = 'prefixed field';
  4. }
  5. const instance = new ClassWithComputedFieldName();
  6. console.log(instance.prefixField);
  7. // 预期输出值: "prefixed field"

当初始化字段时,this指向的是类正在构造中的实例。和公共实例方法相同的是:你可以在子类中使用super来访问超类的原型。

  1. class ClassWithInstanceField {
  2. baseInstanceField = 'base field';
  3. anotherBaseInstanceField = this.baseInstanceField;
  4. baseInstanceMethod() { return 'base method output'; }
  5. }
  6. class SubClassWithInstanceField extends ClassWithInstanceField {
  7. subInstanceField = super.baseInstanceMethod();
  8. }
  9. const base = new ClassWithInstanceField();
  10. const sub = new SubClassWithInstanceField();
  11. console.log(base.anotherBaseInstanceField);
  12. // 预期输出值: "base field"
  13. console.log(sub.subInstanceField);
  14. // 预期输出值: "base method output"

公共方法

静态公共方法

关键字**static**将为一个类定义一个静态方法。静态方法不会在实例中被调用,而只会被类本身调用。它们经常是工具函数,比如用来创建或者复制对象。

  1. class ClassWithStaticMethod {
  2. static staticProperty = 'someValue';
  3. static staticMethod() {
  4. return 'static method has been called.';
  5. }
  6. }
  7. console.log(ClassWithStaticMethod.staticProperty);
  8. // output: "someValue"
  9. console.log(ClassWithStaticMethod.staticMethod());
  10. // output: "static method has been called."

静态方法是在类的赋值阶段用Object.defineProperty方法添加到类中的。静态方法是可编辑的、不可遍历的和可配置的。

公共实例方法

正如其名,公共实例方法是可以在类的实例中使用的。

  1. class ClassWithPublicInstanceMethod {
  2. publicMethod() {
  3. return 'hello world';
  4. }
  5. }
  6. const instance = new ClassWithPublicInstanceMethod();
  7. console.log(instance.publicMethod());
  8. // 预期输出值: "hello world"

公共实例方法是在类的赋值阶段用Object.defineProperty方法添加到类中的。静态方法是可编辑的、不可遍历的和可配置的。
你可以使用生成器(generator)、异步和异步生成器方法。

  1. class ClassWithFancyMethods {
  2. *generatorMethod() { }
  3. async asyncMethod() { }
  4. async *asyncGeneratorMethod() { }
  5. }

在实例的方法中,this指向的是实例本身,你可以使用super访问到超类的原型,由此你可以调用超类的方法。

  1. class BaseClass {
  2. msg = 'hello world';
  3. basePublicMethod() {
  4. return this.msg;
  5. }
  6. }
  7. class SubClass extends BaseClass {
  8. subPublicMethod() {
  9. return super.basePublicMethod();
  10. }
  11. }
  12. const instance = new SubClass();
  13. console.log(instance.subPublicMethod());
  14. // 预期输出值: "hello world"

gettersetter是和类的属性绑定的特殊方法,分别会在其绑定的属性被取值、赋值时调用。使用getset句法定义实例的公共gettersetter

  1. class ClassWithGetSet {
  2. #msg = 'hello world';
  3. get msg() {
  4. return this.#msg;
  5. }
  6. set msg(x) {
  7. this.#msg = `hello ${x}`;
  8. }
  9. }
  10. const instance = new ClassWithGetSet();
  11. console.log(instance.msg);
  12. // expected output: "hello world"
  13. instance.msg = 'cake';
  14. console.log(instance.msg);
  15. // 预期输出值: "hello cake"

私有字段

静态私有字段

静态私有字段可以在类声明本身内部的构造函数上被访问到。
静态变量只能被静态方法访问的限制依然存在。

  1. class ClassWithPrivateStaticField {
  2. static #PRIVATE_STATIC_FIELD;
  3. static publicStaticMethod() {
  4. ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD = 42;
  5. return ClassWithPrivateStaticField.#PRIVATE_STATIC_FIELD;
  6. }
  7. }
  8. assert(ClassWithPrivateStaticField.publicStaticMethod() === 42);

静态私有字段是在类赋值的时候被添加到类构造函数中的。
静态私有字段有一个来源限制。只有定义静态私有字段的类可以访问该字段。这在使用this时,可能会导致不符合预期的行为。

  1. class BaseClassWithPrivateStaticField {
  2. static #PRIVATE_STATIC_FIELD;
  3. static basePublicStaticMethod() {
  4. this.#PRIVATE_STATIC_FIELD = 42;
  5. return this.#PRIVATE_STATIC_FIELD;
  6. }
  7. }
  8. class SubClass extends BaseClassWithPrivateStaticField { }
  9. assertThrows(() => SubClass.basePublicStaticMethod(), TypeError);

私有实例字段

私有实例字段是通过# names句型(读作“哈希名称”)声明的,即为识别符加一个前缀“#”。“#”是名称的一部分,也用于访问和声明。
封装是语言强制实施的。引用不在作用域内的 # names 是语法错误。

  1. class ClassWithPrivateField {
  2. #privateField;
  3. constructor() {
  4. this.#privateField = 42;
  5. this.#randomField = 666; # Syntax error
  6. }
  7. }
  8. const instance = new ClassWithPrivateField();
  9. instance.#privateField === 42; // Syntax error

私有方法

静态私有方法

和静态公共方法一样,静态私有方法也是在类里面而非实例中调用的。和静态私有字段一样,它们也只能在类的声明中访问。
你可以使用生成器(generator)、异步和异步生成器方法。
静态私有方法可以是生成器、异步或者异步生成器函数。

  1. class ClassWithPrivateStaticMethod {
  2. static #privateStaticMethod() {
  3. return 42;
  4. }
  5. static publicStaticMethod() {
  6. return ClassWithPrivateStaticMethod.#privateStaticMethod();
  7. }
  8. }
  9. assert(ClassWithPrivateStaticMethod.publicStaticMethod() === 42);

私有实例方法

私有实例方法在类的实例中可用,它的访问方式的限制和私有实例字段相同。

  1. class ClassWithPrivateMethod {
  2. #privateMethod() {
  3. return 'hello world';
  4. }
  5. getPrivateMessage() {
  6. return #privateMethod();
  7. }
  8. }
  9. const instance = new ClassWithPrivateMethod();
  10. console.log(instance.getPrivateMessage());
  11. // 预期输出值: "hello world"

私有实例方法可以是生成器、异步或者异步生成器函数。私有gettersetter也是可能的:

  1. class ClassWithPrivateAccessor {
  2. #message;
  3. get #decoratedMessage() {
  4. return `✨${this.#message}✨`;
  5. }
  6. set #decoratedMessage(msg) {
  7. this.#message = msg;
  8. }
  9. constructor() {
  10. this.#decoratedMessage = 'hello world';
  11. console.log(this.#decoratedMessage);
  12. }
  13. }
  14. new ClassWithPrivateAccessor();
  15. // 预期输出值: "✨hello world✨"

构造方法

constructor是一种用于创建和初始化[class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class)创建的对象的特殊方法。

  1. class Polygon {
  2. constructor() {
  3. this.name = 'Polygon';
  4. }
  5. }
  6. const poly1 = new Polygon();
  7. console.log(poly1.name);
  8. // expected output: "Polygon"

在一个类中只能有一个名为 “constructor” 的特殊方法。 一个类中出现多次构造函数 (constructor)方法将会抛出一个 SyntaxError 错误。
在一个构造方法中可以使用super关键字来调用一个父类的构造方法。
如果没有显式指定构造方法,则会添加默认的 constructor 方法。
如果不指定一个构造函数(constructor)方法, 则使用一个默认的构造函数(constructor)。

使用constructor方法

以下代码片段来自 类的实例在线 demo)。

  1. class Square extends Polygon {
  2. constructor(length) {
  3. // 在这里, 它调用了父类的构造函数, 并将 lengths 提供给 Polygon 的"width"和"height"
  4. super(length, length);
  5. // 注意: 在派生类中, 必须先调用 super() 才能使用 "this"。
  6. // 忽略这个,将会导致一个引用错误。
  7. this.name = 'Square';
  8. }
  9. get area() {
  10. return this.height * this.width;
  11. }
  12. set area(value) {
  13. // 注意:不可使用 this.area = value
  14. // 否则会导致循环call setter方法导致爆栈
  15. this._area = value;
  16. }
  17. }
  1. class Polygon {
  2. constructor() {
  3. this.name = "Polygon";
  4. }
  5. }
  6. class Square extends Polygon {
  7. constructor() {
  8. super();
  9. }
  10. }
  11. class Rectangle {}
  12. Object.setPrototypeOf(Square.prototype, Rectangle.prototype);
  13. console.log(Object.getPrototypeOf(Square.prototype) === Polygon.prototype); //false
  14. console.log(Object.getPrototypeOf(Square.prototype) === Rectangle.prototype); //true
  15. let newInstance = new Square();
  16. console.log(newInstance.name); //Polygon

这里,Square类的原型被改变,但是在正在创建一个新的正方形实例时,仍然调用前一个基类Polygon的构造函数。

默认构造方法

如前所述,如果不指定构造方法,则使用默认构造函数。对于基类,默认构造函数是:

  1. constructor() {}

对于派生类,默认构造函数是:

  1. constructor(...args) {
  2. super(...args);
  3. }

extends

extends关键字用于类声明或者类表达式中,以创建一个类,该类是另一个类的子类。

  1. class ChildClass extends ParentClass { ... }

extends关键字用来创建一个普通类或者内建对象的子类。
继承的.prototype必须是一个Object 或者 null

使用 extends

第一个例子是根据名为 Polygon 类创建一个名为Square的类。这个例子是从这个在线演示中提取出来的。

  1. class Square extends Polygon {
  2. constructor(length) {
  3. // Here, it calls the parent class' constructor with lengths
  4. // provided for the Polygon's width and height
  5. super(length, length);
  6. // Note: In derived classes, super() must be called before you
  7. // can use 'this'. Leaving this out will cause a reference error.
  8. this.name = 'Square';
  9. }
  10. get area() {
  11. return this.height * this.width;
  12. }
  13. }

使用 extends与内置对象

这个示例继承了内置的Date对象。这个例子是从这个在线演示中提取出来的。

  1. class myDate extends Date {
  2. constructor() {
  3. super();
  4. }
  5. getFormattedDate() {
  6. var months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
  7. return this.getDate() + "-" + months[this.getMonth()] + "-" + this.getFullYear();
  8. }
  9. }

扩展 null

可以像扩展普通类一样扩展null,但是新对象的原型将不会继承 Object.prototype

  1. class nullExtends extends null {
  2. constructor() {}
  3. }
  4. Object.getPrototypeOf(nullExtends); // Function.prototype
  5. Object.getPrototypeOf(nullExtends.prototype) // null
  6. new nullExtends(); //ReferenceError: this is not defined

static

类(class)通过 static 关键字定义静态方法。不能在类的实例上调用静态方法,而应该通过类本身调用。这些通常是实用程序方法,例如创建或克隆对象的功能。

  1. class ClassWithStaticMethod {
  2. static staticProperty = 'someValue';
  3. static staticMethod() {
  4. return 'static method has been called.';
  5. }
  6. }
  7. console.log(ClassWithStaticMethod.staticProperty);
  8. // output: "someValue"
  9. console.log(ClassWithStaticMethod.staticMethod());
  10. // output: "static method has been called."

静态方法调用直接在类上进行,不能在类的实例上调用。静态方法通常用于创建实用程序函数。

调用静态方法

从另一个静态方法

静态方法调用同一个类中的其他静态方法,可使用 [this](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)关键字。

  1. class StaticMethodCall {
  2. static staticMethod() {
  3. return 'Static method has been called';
  4. }
  5. static anotherStaticMethod() {
  6. return this.staticMethod() + ' from another static method';
  7. }
  8. }
  9. StaticMethodCall.staticMethod();
  10. // 'Static method has been called'
  11. StaticMethodCall.anotherStaticMethod();
  12. // 'Static method has been called from another static method'

从类的构造函数和其他方法

非静态方法中,不能直接使用 [this](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/this) 关键字来访问静态方法。而是要用类名来调用:CLASSNAME.STATIC_METHOD_NAME() ,或者用构造函数的属性来调用该方法: this.constructor.STATIC_METHOD_NAME().

  1. class StaticMethodCall {
  2. constructor() {
  3. console.log(StaticMethodCall.staticMethod());
  4. // 'static method has been called.'
  5. console.log(this.constructor.staticMethod());
  6. // 'static method has been called.'
  7. }
  8. static staticMethod() {
  9. return 'static method has been called.';
  10. }
  11. }

示例

下面的例子说明了这几点:

  1. 静态方法如何在类上实现。
  2. 具有静态成员的类,可以被子类化 。
  3. 什么情况下静态方法可以调用,什么情况下不能调用。
  1. class Tripple {
  2. static tripple(n = 1) {
  3. return n * 3;
  4. }
  5. }
  6. class BiggerTripple extends Tripple {
  7. static tripple(n) {
  8. return super.tripple(n) * super.tripple(n);
  9. }
  10. }
  11. console.log(Tripple.tripple());// 3
  12. console.log(Tripple.tripple(6));// 18
  13. let tp = new Tripple();
  14. console.log(BiggerTripple.tripple(3));// 81(不会受父类实例化的影响)
  15. console.log(tp.tripple());// 'tp.tripple 不是一个函数'.