0% completed
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.
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.
Objective: Define a class with some static properties that do not change per instance.
Explanation:
Book class is defined with two properties: title and author, which are initialized to "1984" and "George Orwell", respectively.myBook is created from the Book class.myBook.title and myBook.author, the static values assigned to these properties in the class are displayed.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.
Objective: Create a Person class that initializes with name and age properties.
Explanation:
Person class contains a constructor with parameters name and age.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.alice is created, with "Alice" as the name and 30 as the age.Class methods are functions that belong to the class and can be called on any instance of the class.
Objective: Add a method to the Person class to display a greeting.
Explanation:
greet method is added to the Person class.this.name and this.age to print a greeting. Here, this keyword refers to the instance of the class.bob uses greet to introduce himself.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.
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.
Objective: Add a static method to the Calculator class to perform a basic addition operation.
Explanation:
Calculator class defines a static method called add that takes two parameters, x and y.x and y, showcasing a basic arithmetic operation.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.
.....
.....
.....