-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
58 lines (50 loc) · 1.65 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
"use strict"
const ShippingCart = (function(){
const cost = 50;
const cart = [];
const stock = [];
let quantity = 5;
const shippingCost = 10;
let totalCost = (cost * quantity) + shippingCost;
const addToCart = function(product, qty){
//Code to Toggle Comment Starts Here...
// quantity = qty;
// totalCost = (cost * quantity) + shippingCost;
//Code to Toggle Comment Ends Here
cart.push({item: product, pieces: qty});
console.log(`You have added a product`);
console.log(totalCost);
}
const addToStock = function(product, quantity){
stock.push({item: product, pieces: quantity});
console.log(`${quantity} ${product}s added to Inventory`);
console.log(stock);
}
return {
cart,
quantity,
addToCart,
addToStock
}
})();
console.log(ShippingCart.quantity);
ShippingCart.addToCart('book', 5);
ShippingCart.quantity = 10;
console.log(ShippingCart.quantity);
ShippingCart.addToStock('pencil', 15);
ShippingCart.quantity = 10;
ShippingCart.addToCart('pencil', 10);
ShippingCart.quantity = 10;
ShippingCart.addToCart('shapner', 100);
ShippingCart.addToStock('rubber', 15);
console.log(ShippingCart.cart);
//Look for the totalCost log -- and see how closures are
//working.
//Toggle the assignment of quantity and totalCost variable to
//see how closures actually work
//See that changing the quantity value outside iife
//doesnt impact the value of total cost
//However if we need the total cost to change
//we can do it in the code
//See the effect yourself by Toggle comments
//This is a very strong use case for closures