@Rohith K P 's answer is the solution in short.
If you want to understand it, you need to understand what JavaScript classes are “under the hood".
This is how you would create a class prior to ES6:
// This is a constructor function that initializes new Range objects.
// Note that it does not create or return the object. It just initializes this.
function Range(from, to) {
// Store the start and end points (state) of this new range object.
// These are noninherited properties that are unique to this object.
this.from = from;
this.to = to;
}
// All Range objects inherit from this object.
// Note that the property name must be "prototype" for this to work.
// Note that the prototype property is the property of the Range function
Range.prototype = {
// create some methods
// Return true if x is in the range, false otherwise
// This method works for textual and Date ranges as well as numeric.
includes: function(x) { return this.from <= x && x <= this.to; },
// A generator function that makes instances of the class iterable.
// Note that it only works for numeric ranges.
[Symbol.iterator]: function*() {
for( let x = Math.ceil( this.from); x <= this.to; x ++) yield x;
},
// Return a string representation of the range
toString: function() { return "(" + this.from + "..." + this.to + ")"; }
};
// Here are example uses of this new Range class
let r = new Range(1,3);
// r inherits from Range.prototype
r.includes(2) // = > true: 2 is in the range
r.toString() // = > "(1...3)"
[...r] // => [1, 2, 3]; convert to an array via iterator
Image of inheritance strructure in this case
So, a class is essentially a function, which is a public interface (constructor) of its prototype object.
let F = function() {}; // This is a function object.
let p = F.prototype; // This is the prototype object associated with F.
let c = p.constructor; // This is the function associated with the prototype.
c === F // true
It is important to understand that the class defined in this Example works in exactly the same way as ES6 classes. The introduction of the "class" keyword to the language does not alter the fundamental nature of JavaScript’s prototype-based classes.
Static methods
Static methods are defined as properties of the constructor function rather than properties of the prototype object.
static parse(s) {
let matches = s.match(/^\((\d+)\.\.\.(\d+)\)$/);
if (!matches) {
throw new TypeError(`Cannot parse Range from "${s}".`)
}
return new Range(parseInt(matches[1]), parseInt(matches[2]));
}
The method defined by this code is Range.parse(), not Range.prototype.parse(), and you must invoke it through the constructor, not through an instance:
let r = Range.parse('(1...10)'); // Returns a new Range object
r.parse('(1...10)'); // TypeError: r.parse is not a function
You’ll sometimes see static methods called class methods because they are invoked using the name of the class/ constructor. When this term is used, it is to contrast class methods with the regular instance methods that are invoked on instances of the class. Because static methods are invoked on the constructor rather than on any particular instance, it almost never makes sense to use the this keyword in a static method.
Source: Flanagan, David. JavaScript: The Definitive Guide.