Search code examples
javascriptecmascript-6terminologyecmascript-5

semantic names for JS constructors/prototypes vs classes


In javascript es6 we have classes where we can do something like this:

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

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

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

Which is roughly just syntactic sugar for the following older es5 code:

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
}

In an es6 class it seems simple enough to label the following:

  • Rectangle is a class
  • getArea is an instance method
  • someStaticMethod is a class or static method

I'm teaching a class on objects prototypes and classes, so I want get the verbiage correct for the above. But additionally...

In the context of es5 JS what are the above classified as? Below is my attempt:

  • Rectangle is a constructor function
  • getArea is a prototype method
  • someStaticMethodis a constructor method

I'm not entire sure if they should be called the same thing in es5 as in es6 or if the names i've given them are entirely accurate.


Solution

  • The names you have given are pretty much accurate, except the last one might be a little bit debatable:

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

    You said:

    someStaticMethod is a constructor method

    You could call it that, or simply a method, but essentially, yes, all of those in your question are correct.