35 lines
1.4 KiB
JavaScript
35 lines
1.4 KiB
JavaScript
const ShoppingCard2 = (function () {
|
|
// solche function wird automatisch ausgeführt und die Eigenschaften/Werte werden eingestellt
|
|
//die erwähnten Eigenschaften werden in die Variable ShoppingCard2 als Object gespeichert (sehe die Zeile 19 - 25)
|
|
const cart = [];
|
|
const shippingCost = 10;
|
|
const totalPrice = 235;
|
|
const totalQuantitiy = 5;
|
|
|
|
const addToCart = function (product, quantity) {
|
|
cart.push({ product, quantity });
|
|
console.log(`${product}, ${quantity} added to cart`);
|
|
};
|
|
|
|
const orderStock = function (product, quantity) {
|
|
cart.push({ product, quantity });
|
|
console.log(`${product}, ${quantity} ordered from supplier`);
|
|
};
|
|
|
|
return {
|
|
// die Funktion gibt die eingestellten Werte als Objekt der Eigenschaten zurück
|
|
cart,
|
|
totalPrice,
|
|
totalQuantitiy,
|
|
addToCart,
|
|
};
|
|
})();
|
|
// aus der Crome Console ist das Objekt ShoppingCart2 nicht sichtbar, weil alle Eigenschaten in Modulen privat sind,
|
|
// dort griffen zum Eigenschaften mit globalen Zugriff zu, aus diesem Grund sind solchen Daten unzugänglich
|
|
|
|
ShoppingCard2.addToCart("apfel", 5);
|
|
ShoppingCard2.addToCart("heidelbeeren", 1);
|
|
console.log(ShoppingCard2);
|
|
console.log(ShoppingCard2.shippingCost); // undefined - die Eigenschaft des ShoppingCard2 Objektes liefert
|
|
// nur die folgenden Eigenschaften des ShoppingCard2 Objektes: cart, totalPrice, totalQuantitiy, addToCart,
|