JavaScript From Beginner To Advanced

0% completed

Previous
Next
JavaScript - Classes

In traditional JavaScript, developers relied on constructor functions and prototypes to create blueprints for objects, mimicking the class-based object-oriented paradigm seen in languages like Java or C#. With the introduction of ECMAScript 2015 (ES6), JavaScript formally introduced classes as a part of the language syntax.

Classes in JavaScript provide a much clearer and more concise syntax for creating objects and dealing with inheritance. They are syntactic sugar over the existing prototype-based inheritance and are designed to simplify the way we create and manage objects, making the code more intuitive and easier to develop.

Syntax of Classes

Image
Javascript
Javascript
. . . .

This is the basic structure of a class in JavaScript. The class keyword initializes a new class. Inside the curly braces, you can define properties, constructors, and methods that belong to the class.

Let's delve into understanding classes through incremental examples, starting with the simplest form.

Example: Basic Class with Static Properties

Objective: Define a class with some static properties that do not change per instance.

Javascript
Javascript

. . . .

Explanation:

  • The Book class is defined with two properties: title and author, which are initialized to "1984" and "George Orwell", respectively.
  • An instance named myBook is created from the Book class.
  • When we print myBook.title and myBook.author, the static values assigned to these properties in the class are displayed.

Constructor() Method

The constructor() method is a special method for creating and initializing objects created with a class. There can only be one constructor method in a class.

Image

Example

Objective: Create a Person class that initializes with name and age properties.

Javascript
Javascript

. . . .

Explanation:

  • The Person class contains a constructor with parameters name and age.
  • Inside the constructor, this.name and this.age set the properties of the new object based on the values passed. Here, this keyword represents the instance of the class.
  • An instance alice is created, with "Alice" as the name and 30 as the age.
  • The properties are accessed and displayed, showing that the object has been initialized as expected.

Class Methods

Class methods are functions that belong to the class and can be called on any instance of the class.

Examples

Objective: Add a method to the Person class to display a greeting.

Javascript
Javascript

. . . .

Explanation:

  • The greet method is added to the Person class.
  • This method, when called, uses this.name and this.age to print a greeting. Here, this keyword refers to the instance of the class.
  • The instance bob uses greet to introduce himself.

Static Methods in Classes

Static methods are associated with the class itself rather than any instance. These methods are useful for implementing functions that belong to the class, but not necessarily to any individual object created from the class. You define static methods using the static keyword, which helps organize utility functions that can be called without instantiating the class.

Creating and Using Static Methods

Static methods are defined within the class body using the static keyword. This setup is ideal for utility functions or computational tasks that do not depend on instance properties.

Example: Simple Static Method

Objective: Add a static method to the Calculator class to perform a basic addition operation.

Javascript
Javascript

. . . .

Explanation:

  • The Calculator class defines a static method called add that takes two parameters, x and y.
  • This method simply returns the sum of x and y, showcasing a basic arithmetic operation.
  • The add method is called on the Calculator class itself, not on an instance of the class, illustrating the utility of static methods for tasks that don't require access to instance-specific data.

Static methods enhance the functionality of JavaScript classes by allowing the execution of class-specific operations without the need to create an object.

.....

.....

.....

Like the course? Get enrolled and start learning!
Previous
Next