Skip to main content

Classes

@serverSerrverlesskiy

JavaScript uses the prototypal inheritance model: each object inherits the fields (properties) and methods of the prototype object.

class#

The keyword class is used to define a class:

class MyClass {
// class methods
constructor() { ... }
method1() { ... }
method2() { ... }
method3() { ... }
...
}

This syntax is called a class declaration.

Class

Methods in the class are not comma separated

Class syntax differs from object literals. No commas are required inside classes.

The class may not have a name. A class expression can be used to assign a class to a variable πŸ””:

const UserClass = class {
// class body
}

Classes can be exported as modules. Here's an example of the default export:

export default class User {
// class body
}

And here's an example of a named export:

export class User {
// class body
}

The class becomes useful when you instantiate the class. An instance is an object that contains the data and behavior described by πŸ–ŠοΈ class.

The new operator creates an instance of a class in JavaScript like this: instance = new Class().

For example, you can createπŸ—οΈ an instance of the User classπŸ‘€ using the new operator:

const myUser = new User()

new User() creates an instance of the User class πŸ‘€.

Payment#

Now you are on a stripped-down version of the site, after subscribing to Patreon, you will get full access to the training course, as well as access to our server's private channels in Discord.

Download our mobile application or get tested in our JavaScript telegram bot, and also subscribe to our news.

Become a Patron!

Sumerian school

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


Philipp Dvinyaninov

πŸ“–

Dmitriy Vasilev

πŸ’΅

Resoner2005

πŸ› 🎨 πŸ–‹

Navernoss

πŸ–‹ πŸ› 🎨

Become a Patron!