Uitdaging: Implementeer Statische Eigenschappen en Methoden in een Klasse
Opdracht
Stel je voor dat je een voorraadbeheersysteem bouwt voor een online winkel. De Product-klasse moet het totale aantal toegevoegde producten aan de voorraad bijhouden en functionaliteit bieden om de prijzen van twee producten te vergelijken.
- Definieer een statische eigenschap:
- Maak in de
Product-klasse een statische eigenschaptotalProductsdie wordt geïnitialiseerd op0; - Tel elke keer dat een nieuw
Product-object wordt aangemaakttotalProductsmet 1 op om bij te houden hoeveel producten aan de voorraad zijn toegevoegd.
- Maak in de
- Definieer een statische methode: Definieer een statische methode
comparePrices(product1, product2)die tweeProduct-objecten als parameters neemt en het volgende retourneert:"Product 1 is duurder"alsproduct1een hogere prijs heeft;"Product 2 is duurder"alsproduct2een hogere prijs heeft;"Beide producten hebben dezelfde prijs"als ze gelijk zijn.
12345678910111213141516171819202122232425262728293031class 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
- Definieer een statische eigenschap genaamd
totalProductsen initialiseer deze op0; - Verhoog in de constructor
Product.totalProductsmet 1 telkens wanneer een nieuwProduct-object wordt aangemaakt; - Definieer een statische methode
comparePrices(product1, product2)die twee parameters ontvangt:product1enproduct2; - Gebruik in
comparePriceseenif-statement om te controleren ofproduct1.pricegroter is danproduct2.price. Als dit waar is, retourneer"Product 1 is more expensive"; - Gebruik een
else if-statement om te controleren ofproduct1.pricekleiner is danproduct2.price. Als dit waar is, retourneer"Product 2 is more expensive".
12345678910111213141516171819202122232425262728293031class 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
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Can you explain how static properties and methods work in JavaScript?
What would happen if I created more Product instances?
Can you show how to add more features to the Product class?
Awesome!
Completion rate improved to 2.22
Uitdaging: Implementeer Statische Eigenschappen en Methoden in een Klasse
Veeg om het menu te tonen
Opdracht
Stel je voor dat je een voorraadbeheersysteem bouwt voor een online winkel. De Product-klasse moet het totale aantal toegevoegde producten aan de voorraad bijhouden en functionaliteit bieden om de prijzen van twee producten te vergelijken.
- Definieer een statische eigenschap:
- Maak in de
Product-klasse een statische eigenschaptotalProductsdie wordt geïnitialiseerd op0; - Tel elke keer dat een nieuw
Product-object wordt aangemaakttotalProductsmet 1 op om bij te houden hoeveel producten aan de voorraad zijn toegevoegd.
- Maak in de
- Definieer een statische methode: Definieer een statische methode
comparePrices(product1, product2)die tweeProduct-objecten als parameters neemt en het volgende retourneert:"Product 1 is duurder"alsproduct1een hogere prijs heeft;"Product 2 is duurder"alsproduct2een hogere prijs heeft;"Beide producten hebben dezelfde prijs"als ze gelijk zijn.
12345678910111213141516171819202122232425262728293031class 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
- Definieer een statische eigenschap genaamd
totalProductsen initialiseer deze op0; - Verhoog in de constructor
Product.totalProductsmet 1 telkens wanneer een nieuwProduct-object wordt aangemaakt; - Definieer een statische methode
comparePrices(product1, product2)die twee parameters ontvangt:product1enproduct2; - Gebruik in
comparePriceseenif-statement om te controleren ofproduct1.pricegroter is danproduct2.price. Als dit waar is, retourneer"Product 1 is more expensive"; - Gebruik een
else if-statement om te controleren ofproduct1.pricekleiner is danproduct2.price. Als dit waar is, retourneer"Product 2 is more expensive".
12345678910111213141516171819202122232425262728293031class 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
Bedankt voor je feedback!