Contenido del Curso
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 Static Properties and Methods in a Class
Task
Imagine you're building an inventory management system for an online store. The Product
class needs to keep track of the total number of products added to the inventory, as well as provide functionality to compare the prices of two products.
- Define a Static Property:
- In the
Product
class, create a static propertytotalProducts
initialized to0
; - Each time a new
Product
instance is created, incrementtotalProducts
by 1 to keep track of how many products have been added to the inventory.
- In the
- Define a Static Method: Define a static method
comparePrices(product1, product2)
that takes twoProduct
instances as parameters and returns:"Product 1 is more expensive"
ifproduct1
has a higher price;"Product 2 is more expensive"
ifproduct2
has a higher price;"Both products have the same price"
if they are equal.
class Product { _____ _____ = 0; // Define static property for total products constructor(name, price) { this.name = name; this.price = price; Product._____+=1; // Increment totalProducts } // Static method to compare prices _____ comparePrices(product1, product2) { if (product1.price > product2.price) { return _____; } else if (_____ < _____) { return _____; } else { return 'Both products have the same price'; } } } // Testing const product1 = new Product('Laptop', 1200); const product2 = new Product('Smartphone', 800); const product3 = new Product('Tablet', 1200); console.log(Product.comparePrices(product1, product2)); // Expected: Product 1 is more expensive console.log(Product.comparePrices(product1, product3)); // Expected: Both products have the same price console.log(Product.comparePrices(product2, product3)); // Expected: Product 2 is more expensive console.log(Product.totalProducts); // Expected: 3
- Define a static property named
totalProducts
and initialize it to0
; - In the constructor, increment
Product.totalProducts
by 1 each time a newProduct
instance is created; - Define a static method
comparePrices(product1, product2)
that takes two parameters:product1
andproduct2
; - In
comparePrices
, use anif
statement to check ifproduct1.price
is greater thanproduct2.price
. If true, return"Product 1 is more expensive"
; - Use an
else if
statement to check ifproduct1.price
is less thanproduct2.price
. If true, return"Product 2 is more expensive"
.
class Product { static totalProducts = 0; // Define static property for total products constructor(name, price) { this.name = name; this.price = price; Product.totalProducts+=1; // Increment totalProducts } // Static method to compare prices static comparePrices(product1, product2) { if (product1.price > product2.price) { return 'Product 1 is more expensive'; } else if (product1.price < product2.price) { return 'Product 2 is more expensive'; } else { return 'Both products have the same price'; } } } // Testing const product1 = new Product('Laptop', 1200); const product2 = new Product('Smartphone', 800); const product3 = new Product('Tablet', 1200); console.log(Product.comparePrices(product1, product2)); // Output: Product 1 is more expensive console.log(Product.comparePrices(product1, product3)); // Output: Both products have the same price console.log(Product.comparePrices(product2, product3)); // Output: Product 2 is more expensive console.log(Product.totalProducts); // Output: 3
¿Todo estuvo claro?
¡Gracias por tus comentarios!
Sección 1. Capítulo 13