When working with JavaScript ES6, classes allow us to write code like this:
class RectangularShape {
constructor(height, width) {
this.height = height;
this.width = width;
}
getArea() {
return this.height * this.width
}
static someStaticMethod() {
// performs some utility function
}
}
This syntax is essentially just a cleaner version of the older ES5 code shown below:
function RectangularShape() {
this.height = height;
this.width = width;
}
RectangularShape.prototype.getArea = function() {
return this.height * this.width
}
RectangularShape.someStaticMethod = function() {
// performs some utility function
}
In an ES6 class, we can identify the following roles:
RectangularShape
is a classgetArea
is an instance methodsomeStaticMethod
is either a class method or a static method
While teaching about objects, prototypes and classes, it's important to use the correct terminology. However...
What are these components classified as in the context of ES5 JavaScript? Here's my breakdown:
RectangularShape
is a constructor functiongetArea
is a prototype methodsomeStaticMethod
is a constructor method
I'm unsure if the terminology should be consistent between ES5 and ES6, or if the terms I've used are completely accurate.