JS constructors/prototypes 与 类 的语义名称

semantic names for JS constructors/prototypes vs classes

在 javascript es6 中我们有 classes 我们可以做这样的事情:

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

  getArea() {
    return this.height * this.width
  }

  static someStaticMethod() {
    // does some utility or something
  }
}

这大致只是以下旧 es5 代码的语法糖:

function Rectangle() {
  this.height = height;
  this.width = width;
}

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

Rectangle.someStaticMethod = function() {
  // does some utility or something
}

在 es6 class 中,标记以下内容似乎很简单:

我正在教授有关对象原型和 classes 的 class,因此我希望针对上述内容正确使用措辞。但另外...

在 es5 JS 的上下文中,上面的 class 是什么?以下是我的尝试:

我不完全确定它们在 es5 中的名称是否应该与在 es6 中的名称相同,或者我给它们的名称是否完全准确。

您提供的名称非常准确,除了最后一个可能有点有争议:

Rectangle.someStaticMethod = function() {
  // does some utility or something
}

你说:

someStaticMethod is a constructor method

你可以这样称呼它,或者只是一种方法,但本质上,是的,你问题中的所有这些都是正确的。

Rectangle is a constructor function

是的,听起来不错。它是一个构造新实例的函数,因此描述符合。

getArea is a prototype method

方法是函数属性,在这种情况下它确实是原型的一部分。

someStaticMethod is a constructor method

这有点误导,因为它听起来好像是一种构造某些东西的方法。我宁愿将其称为构造函数 上的 方法。不过,我通常称其为 "static method"。