-
Notifications
You must be signed in to change notification settings - Fork 17.2k
/
Copy pathscript.js
145 lines (111 loc) · 3.31 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
///////////////////////////////////////
// Exporting and Importing in ES6 Modules
// Importing module
// import { addToCart, totalPrice as price, tq } from './shoppingCart.js';
// addToCart('bread', 5);
// console.log(price, tq);
console.log('Importing module');
// console.log(shippingCost);
// import * as ShoppingCart from './shoppingCart.js';
// ShoppingCart.addToCart('bread', 5);
// console.log(ShoppingCart.totalPrice);
// import add, { addToCart, totalPrice as price, tq } from './shoppingCart.js';
// console.log(price);
import add, { cart } from './shoppingCart.js';
add('pizza', 2);
add('bread', 5);
add('apples', 4);
console.log(cart);
/*
///////////////////////////////////////
// Top-Level Await (ES2022)
// console.log('Start fetching');
// const res = await fetch('https://jsonplaceholder.typicode.com/posts');
// const data = await res.json();
// console.log(data);
// console.log('Something');
const getLastPost = async function () {
const res = await fetch('https://jsonplaceholder.typicode.com/posts');
const data = await res.json();
return { title: data.at(-1).title, text: data.at(-1).body };
};
const lastPost = getLastPost();
console.log(lastPost);
// Not very clean
// lastPost.then(last => console.log(last));
const lastPost2 = await getLastPost();
console.log(lastPost2);
///////////////////////////////////////
// The Module Pattern
const ShoppingCart2 = (function () {
const cart = [];
const shippingCost = 10;
const totalPrice = 237;
const totalQuantity = 23;
const addToCart = function (product, quantity) {
cart.push({ product, quantity });
console.log(
`${quantity} ${product} added to cart (sipping cost is ${shippingCost})`
);
};
const orderStock = function (product, quantity) {
console.log(`${quantity} ${product} ordered from supplier`);
};
return {
addToCart,
cart,
totalPrice,
totalQuantity,
};
})();
ShoppingCart2.addToCart('apple', 4);
ShoppingCart2.addToCart('pizza', 2);
console.log(ShoppingCart2);
console.log(ShoppingCart2.shippingCost);
///////////////////////////////////////
// CommonJS Modules
// Export
export.addTocart = function (product, quantity) {
cart.push({ product, quantity });
console.log(
`${quantity} ${product} added to cart (sipping cost is ${shippingCost})`
);
};
// Import
const { addTocart } = require('./shoppingCart.js');
*/
///////////////////////////////////////
// Introduction to NPM
// import cloneDeep from './node_modules/lodash-es/cloneDeep.js';
import cloneDeep from 'lodash-es';
const state = {
cart: [
{ product: 'bread', quantity: 5 },
{ product: 'pizza', quantity: 5 },
],
user: { loggedIn: true },
};
const stateClone = Object.assign({}, state);
const stateDeepClone = cloneDeep(state);
state.user.loggedIn = false;
console.log(stateClone);
console.log(stateDeepClone);
if (module.hot) {
module.hot.accept();
}
class Person {
#greeting = 'Hey';
constructor(name) {
this.name = name;
console.log(`${this.#greeting}, ${this.name}`);
}
}
const jonas = new Person('Jonas');
console.log('Jonas' ?? null);
console.log(cart.find(el => el.quantity >= 2));
Promise.resolve('TEST').then(x => console.log(x));
import 'core-js/stable';
// import 'core-js/stable/array/find';
// import 'core-js/stable/promise';
// Polifilling async functions
import 'regenerator-runtime/runtime';