Course Content
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 Private Properties in a Class
Task
You're creating a BankAccount
class to manage users' bank accounts securely. Each account has an owner and a balance, but you want the balance to be protected from direct access. Your goal is to use private properties to prevent unintended modifications to the balance while allowing controlled access through methods.
- Private Balance Property:
- Inside the
BankAccount
class, declare a private property#balance
.
- Inside the
- Constructor:
- The constructor should accept
owner
andinitialBalance
as parameters; - Assign
owner
to a public property andinitialBalance
to the private#balance
property.
- The constructor should accept
- Add Methods:
- deposit: Define a method that takes
amount
as a parameter. If the amount is greater than 0, it should addamount
to#balance
; - withdraw: Define a method that takes
amount
as a parameter. If the amount is greater than 0 and less than or equal to#balance
, it should subtractamount
from#balance
; - getBalance: Define a method that returns a string with the owner's name and the account balance, e.g.,
"Account balance for John: $1500"
.
- deposit: Define a method that takes
class BankAccount { #_____; // Declare private property constructor(owner, initialBalance) { this._____ = owner; this.#_____ = initialBalance; } deposit(_____) { if (_____) { this.#_____ += _____; } } withdraw(_____) { if (_____ && _____) { this.#_____ -= _____; } } getBalance() { return `Account balance for ${this._____}: $${this.#_____}`; } } // Testing const account1 = new BankAccount('Alice', 1000); account1.deposit(500); console.log(account1.getBalance()); // Expected: Account balance for Alice: $1500 account1.withdraw(300); console.log(account1.getBalance()); // Expected: Account balance for Alice: $1200 // Attempt direct access (should cause an error) // console.log(account1.#balance);
- Declare a private property
#balance
in theBankAccount
class; - In the constructor, assign
owner
to a public property andinitialBalance
to the private#balance
property; - Define a
deposit
method that takesamount
as a parameter. Check ifamount
is greater than 0, then addamount
to#balance
; - Define a
withdraw
method that takesamount
as a parameter. Check ifamount
is greater than 0 and less than or equal to#balance
, then subtractamount
from#balance
; - Define a
getBalance
method that returns a string with the owner's name and the account balance.
class BankAccount { #balance; // Declare private property constructor(owner, initialBalance) { this.owner = owner; this.#balance = initialBalance; } deposit(amount) { if (amount > 0) { this.#balance += amount; } } withdraw(amount) { if (amount > 0 && amount <= this.#balance) { this.#balance -= amount; } } getBalance() { return `Account balance for ${this.owner}: $${this.#balance}`; } } // Testing const account1 = new BankAccount('Alice', 1000); account1.deposit(500); console.log(account1.getBalance()); // Output: Account balance for Alice: $1500 account1.withdraw(300); console.log(account1.getBalance()); // Output: Account balance for Alice: $1200 // Attempt direct access (should cause an error) // console.log(account1.#balance);
Everything was clear?
Thanks for your feedback!
Section 1. Chapter 8