Frequently it is necessary to create many objects of the same kind. As it was already noted in chapter Constructor, operator “new”, you can do it with the help of the new function.
In modern JavaScript, there exists a more advanced “class” construct, introducing new features that are useful for object-oriented programming.
The “class” Syntax
The syntax is as follows:
class MyClass {
// class methods
constructor() { ...
}
method1() { ...
}
method2() { ...
}
method3() { ...
}
...
}
Then it would be best if you used new MyClass() for creating a new object including all the listed methods.
The constructor() method is automatically invoked by new. Hence, the object can be initialized there.
For instance:

At the time a new Site("W3Docs") is called, a new object is generated. Besides, the constructor runs with a particular argument assigning this.siteName to it.
Then you can call object methods. For example, site.welcome().
It is essential to know that you should not put a comma between class methods. It results in a syntax error.
Describing a Class
For a better understanding of many complex aspects, it’s necessary to learn what a class is.
So, a JavaScript class is a kind of function.
For instance:

The class Site {...} construct operates as follows:
- It generates a function, known as Site, which becomes the result of the class declaration. The function code is derived from the constructor method.
- It stocks class methods (welcome, in Site.prototype).
Following the creation of the new Site object, it is taken from the prototype. It means that the object has access to class methods.
The code looks like this:

More Than a “syntactic sugar”
Often the class is compared to “syntactic sugar” (a syntax that is aimed at making things easy-readable not introducing anything new) as it is possible to declare the same without using the class keyword.
Take a look at this example:

But there exist notable differences:
- First of all, a function that class creates is stamped by a unique internal property, such as [[FunctionKind]]:"classConstructor".
In contrast with a regular function, a class constructor should be called with new:
Javascript class
class Site { constructor() {} } console.log(typeof Site); // function Site(); // Error: Class constructor Site cannot be invoked without 'new' - The class methods are considered non-enumerable. The definition of the class sets enumerable flag to false overall methods in the "prototype".
Note that in case you for..in over an object, commonly you don’t want the class methods.
- The use strict is always used by the classes. So, the code inside the class construct is by default in strict mode.
Class Expression
Classes are like functions: they can be defined inside another expression, assigned, passed around, returned, and more.
is a class expression example:
let Site = class {
welcome() {
console.log("W3Docs");
}
};
Class expression can have a name. But its name is visible only inside the class.
For instance:

You even have the option of making “on-demand” classes, as follows:

Shorthands
Like literal objects, classes can include shorthands, such as getter/setters, computer properties, and more.
In this example, the getter/setter is used:

The class declaration generates getters and setters inside Site.prototype, as follows:
Object.defineProperties(Site.prototype, {
siteName: {
get() {
return this._siteName
},
set(siteName) {
// ...
}
}
});
Another example includes computed property name in brackets [...]:

Class Properties
Let’s try to add a property to the example above:

The name of the property is not placed into Site.prototype. It is generated by new before running the constructor. It’s a property of the object.