JavaScript
JS07

JS07 Exercises

Practical

  1. Basic Class Creation:

Create a class named Book with private fields for title, author, and ISBN. Include a constructor to initialize these fields. Add public getters and setters for each field. Demonstrate creating an instance of Book and using the getters and setters.

class Book {
	#title;
	#author;
	#ISBN;
 
	constructor(title, author, ISBN) {
		this.#title = title;
		this.#author = author;
		this.#ISBN = ISBN;
	}
 
	getTitle() {
		return this.#title;
	}
 
	setTitle(title) {
		this.#title = title;
	}
 
	getAuthor() {
		return this.#author;
	}
 
	setAuthor(author) {
		this.#author = author;
	}
 
	getISBN() {
		return this.#ISBN;
	}
 
	setISBN(ISBN) {
		this.#ISBN = ISBN;
	}
}
 
// Usage
let myBook = new Book('1984', 'George Orwell', '1234567890');
console.log(myBook.getTitle()); // 1984
myBook.setTitle('Animal Farm');
console.log(myBook.getTitle()); // Animal Farm
  1. Using Static Elements: Modify the Book class to include a static field that keeps track of the total number of books created. Add a static method that returns this count. Create several instances of the Book class and demonstrate how the static count is updated.
class Book {
	static count = 0;
 
	constructor(title, author, ISBN) {
		this.title = title;
		this.author = author;
		this.ISBN = ISBN;
		Book.count++;
	}
 
	static getCount() {
		return Book.count;
	}
}
 
// Usage
let book1 = new Book('1984', 'George Orwell', '1234567890');
let book2 = new Book('Brave New World', 'Aldous Huxley', '0987654321');
console.log(Book.getCount()); // 2
  1. Private Class Fields with Hash Names: Implement a class named User where the password field is private and uses a hash name (e.g., #password). Include methods to set and validate the password. Show how the password field can be accessed and modified using these methods.
class User {
	// Private field
	#password;
 
	constructor() {
		this.#password = '';
	}
 
	// Method to set the password
	setPassword(newPassword) {
		this.#password = newPassword;
	}
 
	// Method to validate the password
	validatePassword(inputPassword) {
		return this.#password === inputPassword;
	}
}
 
// Usage
const user = new User();
user.setPassword('mySecurePassword123');
console.log(user.validatePassword('mySecurePassword123')); // true
console.log(user.validatePassword('incorrectPassword')); // false
  1. Class with Static Initialization Block: Create a class DatabaseConnector with a static block that simulates loading a database driver. Include a static method to return the status of the driver (e.g., loaded or not). Demonstrate how this static block is executed without creating an instance of the class.
class DatabaseConnector {
	static driverLoaded = false;
 
	static {
		console.log('Loading database driver...');
		DatabaseConnector.driverLoaded = true;
	}
 
	static isDriverLoaded() {
		return DatabaseConnector.driverLoaded;
	}
}
 
// Usage
console.log(DatabaseConnector.isDriverLoaded()); // true
  1. Advanced Constructor Usage: Define a class Rectangle with private fields for width and height. Include multiple constructors: a default constructor, and one that takes width and height. Demonstrate creating instances using both constructors.
class Rectangle {
	constructor(width = 0, height = 0) {
		this.width = width;
		this.height = height;
	}
}
 
// Usage
let defaultRectangle = new Rectangle();
let customRectangle = new Rectangle(10, 20);
console.log(defaultRectangle.width, defaultRectangle.height); // 0, 0
console.log(customRectangle.width, customRectangle.height); // 10, 20
  1. Using new.target in Constructor: Create a class Shape with a constructor that prints a message if the class is instantiated directly versus being extended. Create a subclass of Shape named Circle. Show the difference in output when instantiating Shape directly and when creating an instance of Circle.
class Shape {
	constructor() {
		if (new.target === Shape) {
			console.log('Shape instantiated directly');
		} else {
			console.log('Shape extended');
		}
	}
}
 
class Circle extends Shape {
	constructor(radius) {
		super();
		this.radius = radius;
	}
}
 
// Usage
let shape = new Shape(); // Shape instantiated directly
let circle = new Circle(5); // Shape extended
  1. Create a circle class using a class expression. This class should take a radius in the constructor and have a getArea method, which returns the area of the circle.
let Circle = class {
    constructor(radius) {
        super();
        this.radius = radius;
    }
 
    getArea() {
        return Math.PI * this.radius * this.radius;
    }
};
 
// Usage
let circle = Circle(5);
console.log(circle.getArea())