Зміст курсу
Advanced JavaScript Mastery
Advanced JavaScript Mastery
1. Mastering JavaScript Classes and Inheritance
Getting StartedUnderstanding Class Declarations in JavaScriptChallenge: Create a JavaScript ClassDefining Methods in JavaScript ClassesChallenge: Add Methods to a ClassUsing Parameter ObjectsWorking with Private Properties in ClassesChallenge: Implement Private Properties in a ClassManaging Properties with Getters and SettersChallenge: Implement Getters and Setters in a ClassExploring Static Properties in JavaScriptUsing Static Methods in JavaScriptChallenge: Implement Static Properties and Methods in a ClassUnderstanding Inheritance with extends and super()Challenge: Implement Class Inheritance with extends and super()
2. DOM Manipulation for Interactive Web Development
What Is the Document Object Model (DOM)?Querying and Selecting Elements in the DOMChallenge: Query and Select DOM ElementsUnderstanding the DOM Hierarchy and RelationshipsChallenge: Navigate the DOM HierarchyExploring DOM Properties in JavaScriptWorking with Element Attributes in the DOMChallenge: Manage Element Properties and AttributesAdding Elements to the DOM DynamicallyRemoving Elements From the DOMChallenge: Add and Remove DOM ElementsModifying Element Styles with JavaScriptChallenge: Apply Dynamic Styles to DOM Elements
3. Event Handling and User Interactions in JavaScript
4. Asynchronous JavaScript and API Integration
Introduction to Asynchronous JavaScriptUnderstanding Callbacks in JavaScriptHandling Asynchronous Operations with PromisesUsing Async/Await for Cleaner Asynchronous CodeFetching and Working with APIs in JavaScriptIntegrating APIs in JavaScript ApplicationsChallenge: Fetch and Use API DataIntegrating Third-Party Libraries in JavaScriptChallenge: Work with Third-Party LibrariesHandling Multiple Asynchronous Requests
Challenge: Implement Getters and Setters in a Class
Task
You are working within a Car
class that has a private #mileage
property. Your task is to define getters and setters for this property to control how it’s accessed and modified.
- Define Getter for Mileage: Define a getter for the
mileage
property to retrieve the current mileage; - Define Setter for Mileage with Validation: Define a setter for the
mileage
property that accepts a new mileage value. This setter should:- Only allow updating if the new mileage is greater than the current mileage (mileage can only increase);
- If the new mileage is invalid, log
"Invalid mileage update"
and do not change the current mileage.
class Car { #mileage = 1000; // Initial private mileage // Getter for mileage _____ mileage() { return this.#_____; } // Setter for mileage with validation _____ mileage(newMileage) { _____ (_____ > this.#_____) { this.#_____ = newMileage; } _____ { console.log(_____); } } } // Testing const car1 = new Car(); console.log(car1.mileage); // Expected: 1000 car1.mileage = 1500; // Update mileage to 1500 console.log(car1.mileage); // Expected: 1500 car1.mileage = 900; // Attempt invalid mileage update, expected: Invalid mileage update
- Define a
get
accessor formileage
to retrieve the current mileage; - Define a
set
accessor formileage
that takesnewMileage
as a parameter; - In the setter, check if
newMileage
is greater than the current mileage (this.#mileage
); - If
newMileage
is valid, update#mileage
tonewMileage
; - If
newMileage
is not greater than the current mileage, log"Invalid mileage update"
and do not change the mileage.
class Car { #mileage = 1000; // Initial private mileage // Getter for mileage get mileage() { return this.#mileage; } // Setter for mileage with validation set mileage(newMileage) { if (newMileage > this.#mileage) { this.#mileage = newMileage; } else { console.log('Invalid mileage update'); } } } // Testing const car1 = new Car(); console.log(car1.mileage); // Output: 1000 car1.mileage = 1500; // Update mileage to 1500 console.log(car1.mileage); // Output: 1500 car1.mileage = 900; // Attempt invalid mileage update, expected: Invalid mileage update
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 1. Розділ 10