A class in JavaScript is a blueprint for creating objects, defined with the class keyword.
This page covers Class in JavaScript. For a language-agnostic introduction, see Class.
Defining a Class#
class Player {
}By convention, class names use PascalCase: Player, OddsAndEvens, RockPaperScissors.
Adding a Constructor#
The constructor method runs automatically when an object is created. Use it to set the initial state.
class Player {
constructor(name) {
this.name = name;
}
}this.name = name stores the value on the object. this refers to the current instance.