The community is working on translating this tutorial into Uzbek, 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".
Methods
As we have seen previously in this tutorial, regular functions can exist in the global scope and be accessed from anywhere, while a method is a function declared on a class and accessed either directly on the class or on an instance of the class. In other words, a method is just a function which belongs to a class.
In the previous article, we covered class fields, which are the containers used by a class for storing data. Methods are there to provide the actual functionality of the class, and while they will often interact with the fields, they can also work without using any fields.
We already saw an example of how a method could look in the introduction article for this chapter on classes, but now it's time to dig a bit deeper. First of all, let's just make it clear how a class method is defined:
class Dog
{
Bark()
{
}
}
Here we have a simple class, called Dog, with a simple function called Bark(). If you have read about regular functions elsewhere in this tutorial, you will notice that classes doesn't use the function keyword to denote a function. Instead, the JavaScript interpreter will know that it's a function because of the parentheses and the following curly brackets.
Inside the parentheses, you can have zero or more parameters, like this:
class Dog
{
Bark(param1, param2)
{
}
}
And inside the curly brackets is where you place the actual code (body) of the function, like this:
class Dog
{
Bark(sound)
{
alert("The dog says: " + sound);
}
}
With that in place, let's try creating a new Dog and calling the Bark() class method:
class Dog
{
Bark(sound)
{
alert("The dog says: " + sound);
}
}
let dog = new Dog();
dog.Bark("Woof!");
There's a LOT more to know about functions in general, but they are not specific to classes, so if you haven't already read the functions chapter of our tutorial, I suggest you do it now. Especially the articles on function parameters are very relevant.
The constructor
Something that is indeed specific to classes is the special type of function called a constructor. If a class defines a constructor, this special method is automatically called when the class is instantiated, allowing the class to initialize fields etc. A constructor is defined by using the constructor keyword in the place where the name of the function would normally appear, like this:
class Dog
{
constructor()
{
}
}
And just like a regular function, you are allowed to specify one or more parameters for it, if needed. These parameters can be passed to the class when creating an object based on the class. Let me show you a complete example of this:
class Dog
{
name;
age;
constructor(name, age)
{
this.name = name;
this.age = age;
}
Describe()
{
return this.name + " is " + this.age + " years old";
}
}
let dog = new Dog("Dog Doe", 7);
alert(dog.Describe());
Notice how we use the constructor to set values for our two fields, name and age. We pass in these values when we create the dog object, and they are later used by the Describe() method.
Private methods
In a previous article, we discussed the concept of private fields, and private members in general. So if you want to know more about the concept of private members, please take a step back and read the article on private fields.
For now, I just want to mention that a private method can be added to a class, preventing this method from being called outside of the class. To make a method private, prefix its name with the hash sign. Here's an example of it in action:
class Dog
{
name;
age;
constructor(name, age)
{
this.name = name;
this.age = age;
}
#getDogDescription()
{
return this.name + " is " + this.age + " years old";
}
Describe()
{
return this.#getDogDescription();
}
}
let dog = new Dog("Dog Doe", 7);
alert(dog.Describe());
In this example, I have created a private method called #getDogDescription(). Notice the hash-sign at the start of the name - it will tell the JavaScript interpreter that this method is private and should only be accessed from within the class. We then expose a public method, called Describe(), which internally just calls the #getDogDescription() method.
If you want to test this, try changing the last line to use the private method instead, like this:
alert(dog.#getDogDescription());
You will immediately find an error like this one in the console:
Uncaught SyntaxError: Private field '#getDogDescription' must be declared in an enclosing class
Summary
A method is just a function defined on a class. We can use class methods to add functionality to the class, and just like regular JavaScript functions, a class method can define one or more parameters, used to feed information to the method. If you haven't already read the chapter on functions, I suggest you do it now, because they share a lot of important functionality with class methods.