JavaScript 中的类介绍和使用

发布于 2022-08-18 12:23:38 字数 4787 浏览 231 评论 0

在面向对象编程中, 是创建对象的模板。 JavaScript 的 class 关键字 是您在 JavaScript 中声明新类的方式。

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

const obj = new Rectangle(3, 5);
obj.height; // 3
obj.width; // 5

// The `instanceof` keyword is how you test whether an object was created
// from a certain class.
obj instanceof Rectangle; // true
({}) instanceof Rectangle; // false

Methods

在类中定义的函数,JavaScript 会添加到该类的每个实例中。 例如,假设你想计算一个 Rectangle,您可以定义一个 area() 方法如下图。

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  // To define a method named `methodName`, you put `methodName() {}` in
  // the class declaration.
  area() {
    return this.width * this.height;
  }
}

const obj = new Rectangle(3, 5);
obj.area(); // 15

在一种方法中, this 关键字 指的是方法附加到的类实例。 在上面的例子中, thisobj

Statics

Statics 是在类本身上定义的函数,在 JavaScript 中,类只是另一个变量,因此您可以在类上调用 静态函数

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  // To define a static named `functionName`, you put
  // `static functionName() {}` in the class declaration.
  static createSquare(length) {
    return new Rectangle(length, length);
  }
}

const obj = Rectangle.createSquare(5);
obj.height; // 5
obj.width; // 5

Getters/Setters

定义区域面积的另一种方法 Rectangle 正在使用 getters。 使用 getters,您可以 area a 的动态计算属性 Rectangle,而不是一种方法。

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  // To define a getter named `getterName`, put `get getterName() {}`
  // in the class definition. Getters are functions!
  get area() {
    return this.height * this.width;
  }
}

const obj = new Rectangle(3, 5);
obj.area; // 15

您还可以定义一个自定义设置器,当您设置属性时会调用该设置器。
例如,假设您想绝对确定 heightwidth 是数字。 您可以定义一个自定义设置器,每当有人尝试设置时抛出异常 height 为非数值。

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  get height() {
    return this._height;
  }

  set height(v) {
    assert.ok(typeof v === 'number', 'Height must be a number');
    return v;
  }

  get width() {
    return this._width;
  }

  set width(v) {
    assert.ok(typeof v === 'number', 'Width must be a number');
    return v;
  }
}

const obj = new Rectangle(3, 5);
// Throws 'AssertionError: Height must be a number'
obj.height = 'Not a number';

Inheritance 继承

当一个类 extends 另一个类 ,这意味着子类默认具有与父类相同的静态、方法、getter 和 setter。 但是子类可以定义额外的静态变量、方法、getter 和 setter。 子类还可以 覆盖 基类的静态、方法、getter 和 setter。

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }

  area() {
    return this.width * this.height;
  }
}

class Square extends Rectangle {
  constructor(length) {
    // `super` refers to the base class' constructor
    super(length, length);
  }

  // The radius of the inscribed circle
  // See: see http://mathworld.wolfram.com/Square.html
  inradius() {
    return this.height / 2;
  }
}

const obj = new Square(5);

obj.area(); // 25, from `Rectangle` base class
obj.inradius(); // 2.5, from `Square` class

obj instanceof Square; // true
obj instanceof Rectangle; // true

继承仍然是基于原型的

extends 关键字仍然在底层使用 基于原型的继承 。这意味着您可以将基于原型的模式与 ES6 类结合使用。

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
}

Rectangle.prototype.area = function area() {
  return this.width * this.height;
};

const obj = new Rectangle(3, 5);

obj.area(); // 15

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

伪装你

暂无简介

文章
评论
311 人气
更多

推荐作者

alipaysp_snBf0MSZIv

文章 0 评论 0

梦断已成空

文章 0 评论 0

瞎闹

文章 0 评论 0

寄意

文章 0 评论 0

似梦非梦

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文