Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Uitdaging: Implementeer Statische Eigenschappen en Methoden in een Klasse | Beheersing van JavaScript-Klassen en Overerving
Geavanceerde JavaScript-Beheersing

bookUitdaging: 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.

  1. Definieer een statische eigenschap:
    • Maak in de Product-klasse een statische eigenschap totalProducts die wordt geïnitialiseerd op 0;
    • Tel elke keer dat een nieuw Product-object wordt aangemaakt totalProducts met 1 op om bij te houden hoeveel producten aan de voorraad zijn toegevoegd.
  2. Definieer een statische methode: Definieer een statische methode comparePrices(product1, product2) die twee Product-objecten als parameters neemt en het volgende retourneert:
    • "Product 1 is duurder" als product1 een hogere prijs heeft;
    • "Product 2 is duurder" als product2 een hogere prijs heeft;
    • "Beide producten hebben dezelfde prijs" als ze gelijk zijn.
12345678910111213141516171819202122232425262728293031
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
copy
  • Definieer een statische eigenschap genaamd totalProducts en initialiseer deze op 0;
  • Verhoog in de constructor Product.totalProducts met 1 telkens wanneer een nieuw Product-object wordt aangemaakt;
  • Definieer een statische methode comparePrices(product1, product2) die twee parameters ontvangt: product1 en product2;
  • Gebruik in comparePrices een if-statement om te controleren of product1.price groter is dan product2.price. Als dit waar is, retourneer "Product 1 is more expensive";
  • Gebruik een else if-statement om te controleren of product1.price kleiner is dan product2.price. Als dit waar is, retourneer "Product 2 is more expensive".
12345678910111213141516171819202122232425262728293031
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
copy

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 13

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

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

bookUitdaging: 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.

  1. Definieer een statische eigenschap:
    • Maak in de Product-klasse een statische eigenschap totalProducts die wordt geïnitialiseerd op 0;
    • Tel elke keer dat een nieuw Product-object wordt aangemaakt totalProducts met 1 op om bij te houden hoeveel producten aan de voorraad zijn toegevoegd.
  2. Definieer een statische methode: Definieer een statische methode comparePrices(product1, product2) die twee Product-objecten als parameters neemt en het volgende retourneert:
    • "Product 1 is duurder" als product1 een hogere prijs heeft;
    • "Product 2 is duurder" als product2 een hogere prijs heeft;
    • "Beide producten hebben dezelfde prijs" als ze gelijk zijn.
12345678910111213141516171819202122232425262728293031
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
copy
  • Definieer een statische eigenschap genaamd totalProducts en initialiseer deze op 0;
  • Verhoog in de constructor Product.totalProducts met 1 telkens wanneer een nieuw Product-object wordt aangemaakt;
  • Definieer een statische methode comparePrices(product1, product2) die twee parameters ontvangt: product1 en product2;
  • Gebruik in comparePrices een if-statement om te controleren of product1.price groter is dan product2.price. Als dit waar is, retourneer "Product 1 is more expensive";
  • Gebruik een else if-statement om te controleren of product1.price kleiner is dan product2.price. Als dit waar is, retourneer "Product 2 is more expensive".
12345678910111213141516171819202122232425262728293031
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
copy

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 13
some-alt