#atom
ID: JS-011
Date: [Insert Date]
Content:
Classes in JavaScript are a syntactic sugar over the language's existing prototype-based inheritance. Introduced in ES6 (ECMAScript 2015), classes provide a cleaner and more familiar way to create objects and implement inheritance, especially for developers coming from class-based languages like Java or C++.
1. Definition of Classes
- A class is a blueprint for creating objects with shared properties and methods.
- Classes in JavaScript are built on top of prototypes and constructor functions.
2. Syntax and Usage
Declaring a Class:
Use the class
keyword to define a class.
Syntax:
class ClassName {
// Constructor method (optional)
constructor(parameters) {
// Initialize properties
}
// Instance methods
methodName() {
// Method logic
}
// Static methods
static staticMethodName() {
// Static method logic
}
}
Example:
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
static info() {
console.log('This is a Person class.');
}
}
Creating an Instance:
Use the new
keyword to create an instance of a class.
Example:
const alice = new Person('Alice', 25);
alice.greet(); // "Hello, my name is Alice and I am 25 years old."
Static Methods:
Static methods belong to the class itself, not instances. They are called on the class, not on objects.
Example:
Person.info(); // "This is a Person class."
3. Inheritance
Classes support inheritance using the extends
keyword. Child classes inherit properties and methods from parent classes.
Syntax:
class ChildClass extends ParentClass {
constructor(parameters) {
super(parameters); // Call the parent class constructor
// Initialize child class properties
}
}
Example:
class Student extends Person {
constructor(name, age, major) {
super(name, age); // Call the parent class constructor
this.major = major;
}
study() {
console.log(`${this.name} is studying ${this.major}.`);
}
}
const bob = new Student('Bob', 20, 'Computer Science');
bob.greet(); // "Hello, my name is Bob and I am 20 years old."
bob.study(); // "Bob is studying Computer Science."
4. Getters and Setters
Classes support getter and setter methods for accessing and modifying properties.
Example:
class Rectangle {
constructor(width, height) {
this._width = width;
this._height = height;
}
// Getter for area
get area() {
return this._width * this._height;
}
// Setter for width
set width(value) {
if (value > 0) {
this._width = value;
} else {
console.log('Width must be positive.');
}
}
}
const rect = new Rectangle(10, 20);
console.log(rect.area); // 200
rect.width = 15; // Set width to 15
console.log(rect.area); // 300
5. Relationship Between Classes and Prototypes
- Classes in JavaScript are syntactic sugar over prototypes.
- Under the hood, classes still use JavaScript's prototype-based inheritance.
- The
class
keyword makes the syntax cleaner and more intuitive.
Example:
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a sound.`);
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name);
this.breed = breed;
}
speak() {
console.log(`${this.name} barks.`);
}
}
const dog = new Dog('Rex', 'German Shepherd');
dog.speak(); // "Rex barks."
6. Advantages of Classes
- Readability: Classes provide a cleaner and more intuitive syntax for creating objects and implementing inheritance.
- Familiarity: Developers from class-based languages find it easier to work with classes.
- Encapsulation: Classes allow for better organization of properties and methods.
7. Limitations of Classes
- Not "Real" Classes: JavaScript classes are still based on prototypes, which can be confusing for developers expecting traditional class behavior.
- No Private Fields (Pre-ES2022): Before ES2022, JavaScript classes did not support truly private fields (though this has been added with
#
syntax).
Connections:
- JS-010: Prototypes in JavaScript
- JS-015: ES6+ Features in JavaScript
- JS-005: Functions in JavaScript
Connections:
Sources: