hinzugefügt Module Pattern, await und async Funtionen, verwendet json placeholder API
This commit is contained in:
parent
1ee4cd646c
commit
8eb52fb8ef
26
javascript/Javascript Expert/chapter17/awaitMethod.js
Normal file
26
javascript/Javascript Expert/chapter17/awaitMethod.js
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
import "./shoppingCart.js";
|
||||||
|
|
||||||
|
// console.log("Start fetching");
|
||||||
|
// const response = await fetch("https://jsonplaceholder.typicode.com/posts");
|
||||||
|
|
||||||
|
// const data = await response.json();
|
||||||
|
// console.log(data);
|
||||||
|
// console.log("Something");
|
||||||
|
|
||||||
|
const getLastPost = async function () {
|
||||||
|
const response = await fetch("https://jsonplaceholder.typicode.com/posts");
|
||||||
|
const data = await response.json();
|
||||||
|
console.log(data);
|
||||||
|
|
||||||
|
return { title: data.at(-1).title, text: data.at(-1).body };
|
||||||
|
};
|
||||||
|
|
||||||
|
const lastPost = getLastPost();
|
||||||
|
console.log(lastPost);
|
||||||
|
|
||||||
|
lastPost.then(function (last) {
|
||||||
|
console.log(last);
|
||||||
|
});
|
||||||
|
|
||||||
|
const lastPost2 = await getLastPost();
|
||||||
|
console.log(lastPost2);
|
||||||
@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8" />
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
|
||||||
<script defer type="module" src="script.js"></script>
|
<script defer type="module" src="modulePattern.js"></script>
|
||||||
<title>Modern JavaScript Development: Modules and Tooling</title>
|
<title>Modern JavaScript Development: Modules and Tooling</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
|
|||||||
33
javascript/Javascript Expert/chapter17/modulePattern.js
Normal file
33
javascript/Javascript Expert/chapter17/modulePattern.js
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
const ShoppingCard2 = (function () {
|
||||||
|
// solche function wird automatisch ausgeführt und die Eigenschaften/Werte werden eingestellt
|
||||||
|
//und werden in die Variable ShoppingCard2 als Object gespeichert
|
||||||
|
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 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
|
||||||
@ -1,6 +1,9 @@
|
|||||||
console.log("Exporting Module");
|
console.log("Exporting Module");
|
||||||
const shippingCost = 10;
|
const shippingCost = 10;
|
||||||
const cart = [];
|
const cart = [];
|
||||||
|
console.log("Start fetching users");
|
||||||
|
await fetch("https://jsonplaceholder.typicode.com/users");
|
||||||
|
console.log("Finish fetching users");
|
||||||
|
|
||||||
const addToCart = function (product, quantity) {
|
const addToCart = function (product, quantity) {
|
||||||
cart.push({ product, quantity });
|
cart.push({ product, quantity });
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user