Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Herausforderung: Verwenden von Statischen Eigenschaften und Methoden in Einer Klasse | Klassen
Fortgeschrittene JavaScript-Meisterschaft
course content

Kursinhalt

Fortgeschrittene JavaScript-Meisterschaft

Fortgeschrittene JavaScript-Meisterschaft

2. DOM-Manipulation
3. Ereignisse und Ereignisbehandlung
4. Asynchrones JavaScript und APIs

book
Herausforderung: Verwenden von Statischen Eigenschaften und Methoden in Einer Klasse

Aufgabe

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

  1. Definieren Sie eine statische Eigenschaft :

    • Erstellen Sie in der Product -Klasse eine statische Eigenschaft totalProducts , die auf 0 initialisiert wird;

    • Jedes Mal, wenn eine neue Product -Instanz 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 Product -Instanzen als Parameter nimmt und Folgendes zurückgibt:

    • "Produkt 1 ist teurer" , wenn product1 einen höheren Preis hat;

    • "Produkt 2 ist teurer" , wenn product2 einen höheren Preis hat;

    • "Beide Produkte haben den gleichen Preis" , wenn sie gleich sind.

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 namens totalProducts und initialisieren Sie sie mit 0 ;

  • Im Konstruktor erhöhen Sie Product.totalProducts um 1, jedes Mal, wenn eine neue Product -Instanz erstellt wird;

  • Definieren Sie eine statische Methode comparePrices(product1, product2) , die zwei Parameter entgegennimmt: product1 und product2 ;

  • In comparePrices verwenden Sie eine if -Anweisung, um zu prüfen, ob product1.price größer ist als product2.price . Wenn wahr, geben Sie "Product 1 is more expensive" zurück;

  • Verwenden Sie eine else if -Anweisung, um zu prüfen, ob product1.price kleiner ist als product2.price . Wenn wahr, 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
ChatGPT

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

course content

Kursinhalt

Fortgeschrittene JavaScript-Meisterschaft

Fortgeschrittene JavaScript-Meisterschaft

2. DOM-Manipulation
3. Ereignisse und Ereignisbehandlung
4. Asynchrones JavaScript und APIs

book
Herausforderung: Verwenden von Statischen Eigenschaften und Methoden in Einer Klasse

Aufgabe

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

  1. Definieren Sie eine statische Eigenschaft :

    • Erstellen Sie in der Product -Klasse eine statische Eigenschaft totalProducts , die auf 0 initialisiert wird;

    • Jedes Mal, wenn eine neue Product -Instanz 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 Product -Instanzen als Parameter nimmt und Folgendes zurückgibt:

    • "Produkt 1 ist teurer" , wenn product1 einen höheren Preis hat;

    • "Produkt 2 ist teurer" , wenn product2 einen höheren Preis hat;

    • "Beide Produkte haben den gleichen Preis" , wenn sie gleich sind.

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 namens totalProducts und initialisieren Sie sie mit 0 ;

  • Im Konstruktor erhöhen Sie Product.totalProducts um 1, jedes Mal, wenn eine neue Product -Instanz erstellt wird;

  • Definieren Sie eine statische Methode comparePrices(product1, product2) , die zwei Parameter entgegennimmt: product1 und product2 ;

  • In comparePrices verwenden Sie eine if -Anweisung, um zu prüfen, ob product1.price größer ist als product2.price . Wenn wahr, geben Sie "Product 1 is more expensive" zurück;

  • Verwenden Sie eine else if -Anweisung, um zu prüfen, ob product1.price kleiner ist als product2.price . Wenn wahr, 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
Wir sind enttäuscht, dass etwas schief gelaufen ist. Was ist passiert?
some-alt