The community is working on translating this tutorial into Romanian, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".
Fields
As we talked about in the introduction to classes, they can be considered as boxes or containers for data and functionality. The functionality is covered by class methods, which we'll talk about in one of the next articles, while the data part is taken care of by class fields.
We already used a field in the introduction to classes example in the previous article, but in this article I want to go back and start from the beginning, giving you an introduction to fields and discussing it in detail.
A class field is basically just a variable, contained by the class, but it does offer some extra functionality, as we'll see in some of the later examples.
Simple fields
In its most simple form, a class field is added to a class simply by writing its name inside of the class body, followed by a semicolon. Let's illustrate that with a Dog class with two fields:
class Dog
{
name;
age;
}
This creates a class called Dog, containing two fields: "name" and "age".
But perhaps you would like to give your fields an initial value - no problem, you can do that, just like with regular variables:
class Dog
{
name = "Dog Doe";
age = 7;
}Using the fields
By default, a class field in JavaScript is public, which means that it can be accessed from both inside and outside of the class. We'll talk more about class member visibility later on, but for now, let's try using the fields in a more complete example:
class Dog
{
name = "Dog Doe";
age = 7;
Describe()
{
return this.name + " is " + this.age + " years old";
}
}
let dog = new Dog();
alert(dog.Describe());
// Dog birthday...
dog.age = dog.age + 1;
alert(dog.Describe());As you can see, after creating the dog object and calling the Describe() method, I then access the age field and add one to the age. To establish that the age has changed, I call the Describe() method again. We now use the field from within the class (in the Describe() method) as well as from the outside.
Private fields
However, sometimes you want to keep access to a field strictly to within the class. This is possible with JavaScript by defining the field as private, and as a concept it's often referred to as encapsulation because you are hiding something internally from the consumer of the class, allowing only the class itself to manipulate the data.
This might sound a bit strange, because you as the programmer are often both the definer and the consumer of the class, as we have just seen in the previous examples, but you have to think a bit bigger here. One example is a large project with hundreds or even thousands of classes, created by multiple developers. In a situation like tha