JavaScript Tutorial

Javascript Object Methods

Constructor Method

The constructor method is a special method used to initialize and create an object in JavaScript. It is automatically called when memory is allocated for an object.

Here are some key points to remember when using the constructor method:

  1. Use the "constructor" keyword to declare a constructor method in a class.
  2. A class can have only one constructor method.
  3. The constructor method is responsible for initializing the object's properties and performing any setup required.
  4. To access the constructor of a parent class, use the "super" keyword.

By using the constructor method, you can ensure that an object is properly initialized when it is created. It allows you to set default values, assign initial values to properties, and perform any necessary setup before the object is used.

Input:-

   

Output:-

Static Method

A static method is a method that belongs to the class itself rather than an instance of that class. This means that you can call a static method directly on the class without the need for an instance of the class.

Here are some important points to remember when using static methods:

  1. Use the "static" keyword to declare a static method in a class.
  2. Static methods can be used to create utility functions that are not tied to a specific instance of the class.
  3. You can give any name to a static method.
  4. A class can contain more than one static method.
  5. If you declare multiple static methods with the same name, JavaScript will always invoke the last one defined.
  6. You cannot use the "this" keyword directly to call a static method within a non-static method. In such cases, you can call the static method either using the class name or as a property of the constructor.

Static methods are useful when you want to define utility functions or perform operations that are not related to specific instances of a class. They provide a way to organize and encapsulate functionality within a class without the need for object instantiation.

Input:-

   

Output:-

Go back to Previous Course