Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Herausforderung: Statische Eigenschaften und Methoden in Einer Klasse Implementieren | JavaScript-Klassen und Vererbung Meistern
Fortgeschrittene JavaScript-Beherrschung

bookHerausforderung: Statische Eigenschaften und Methoden in Einer Klasse Implementieren

Aufgabe

Stellen Sie sich vor, Sie entwickeln ein Inventarverwaltungssystem für einen Online-Shop. Die Klasse Product muss die Gesamtanzahl der zum Inventar hinzugefügten Produkte verfolgen und eine Funktionalität bereitstellen, um die Preise von zwei Produkten zu vergleichen.

  1. Definieren Sie eine statische Eigenschaft:
    • Erstellen Sie in der Klasse Product eine statische Eigenschaft totalProducts, die mit 0 initialisiert wird;
    • Jedes Mal, wenn eine neue Instanz von Product erstellt wird, erhöhen Sie totalProducts um 1, um zu verfolgen, wie viele Produkte dem Inventar hinzugefügt wurden.
  2. Definieren Sie eine statische Methode: Definieren Sie eine statische Methode comparePrices(product1, product2), die zwei Instanzen von Product als Parameter erhält und Folgendes zurückgibt:
    • "Product 1 is more expensive", wenn product1 einen höheren Preis hat;
    • "Product 2 is more expensive", wenn product2 einen höheren Preis hat;
    • "Both products have the same price", wenn beide den gleichen Preis haben.
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
  • Definieren Sie eine statische Eigenschaft mit dem Namen totalProducts und initialisieren Sie sie mit 0;
  • Erhöhen Sie im Konstruktor Product.totalProducts um 1, jedes Mal, wenn eine neue Instanz von Product erstellt wird;
  • Definieren Sie eine statische Methode comparePrices(product1, product2), die zwei Parameter erhält: product1 und product2;
  • Verwenden Sie in comparePrices eine if-Anweisung, um zu prüfen, ob product1.price größer als product2.price ist. Falls ja, geben Sie "Product 1 is more expensive" zurück;
  • Verwenden Sie eine else if-Anweisung, um zu prüfen, ob product1.price kleiner als product2.price ist. Falls ja, geben Sie "Product 2 is more expensive" zurück.
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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 13

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

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

bookHerausforderung: Statische Eigenschaften und Methoden in Einer Klasse Implementieren

Swipe um das Menü anzuzeigen

Aufgabe

Stellen Sie sich vor, Sie entwickeln ein Inventarverwaltungssystem für einen Online-Shop. Die Klasse Product muss die Gesamtanzahl der zum Inventar hinzugefügten Produkte verfolgen und eine Funktionalität bereitstellen, um die Preise von zwei Produkten zu vergleichen.

  1. Definieren Sie eine statische Eigenschaft:
    • Erstellen Sie in der Klasse Product eine statische Eigenschaft totalProducts, die mit 0 initialisiert wird;
    • Jedes Mal, wenn eine neue Instanz von Product erstellt wird, erhöhen Sie totalProducts um 1, um zu verfolgen, wie viele Produkte dem Inventar hinzugefügt wurden.
  2. Definieren Sie eine statische Methode: Definieren Sie eine statische Methode comparePrices(product1, product2), die zwei Instanzen von Product als Parameter erhält und Folgendes zurückgibt:
    • "Product 1 is more expensive", wenn product1 einen höheren Preis hat;
    • "Product 2 is more expensive", wenn product2 einen höheren Preis hat;
    • "Both products have the same price", wenn beide den gleichen Preis haben.
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
  • Definieren Sie eine statische Eigenschaft mit dem Namen totalProducts und initialisieren Sie sie mit 0;
  • Erhöhen Sie im Konstruktor Product.totalProducts um 1, jedes Mal, wenn eine neue Instanz von Product erstellt wird;
  • Definieren Sie eine statische Methode comparePrices(product1, product2), die zwei Parameter erhält: product1 und product2;
  • Verwenden Sie in comparePrices eine if-Anweisung, um zu prüfen, ob product1.price größer als product2.price ist. Falls ja, geben Sie "Product 1 is more expensive" zurück;
  • Verwenden Sie eine else if-Anweisung, um zu prüfen, ob product1.price kleiner als product2.price ist. Falls ja, geben Sie "Product 2 is more expensive" zurück.
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

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 13
some-alt