|
| 1 | +const greenGrocer = { |
| 2 | + buyApples: function(quantity) { |
| 3 | + return quantity + ' apples'; |
| 4 | + }, |
| 5 | + buyTomatoes: function(quantity) { |
| 6 | + return quantity + ' tomatoes'; |
| 7 | + }, |
| 8 | + buyCauliflower: function(quantity) { |
| 9 | + return quantity + ' cauliflower'; |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +const cheeseShop = { |
| 14 | + buyCheese: function(type, weight) { |
| 15 | + this.cutCheese(type, weight); |
| 16 | + |
| 17 | + return weight + 'gr ' + type + ' cheese'; |
| 18 | + }, |
| 19 | + |
| 20 | + cutCheese: function(type, weight) { |
| 21 | + const stopTime = Date.now() + 100; |
| 22 | + while (Date.now() < stopTime); |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +const bakery = { |
| 27 | + buyBread: function(breadReadyCallback, type, quantity) { |
| 28 | + setTimeout(function() { |
| 29 | + breadReadyCallback(quantity + ' ' + type); |
| 30 | + }, 1000); |
| 31 | + }, |
| 32 | +}; |
| 33 | + |
| 34 | +function visitGreengrocer() { |
| 35 | + console.log('=== Greengrocer ==='); |
| 36 | + |
| 37 | + const beforeBuy = Date.now(); |
| 38 | + |
| 39 | + const fruitsAndVegetables = [ |
| 40 | + greenGrocer.buyApples(2), |
| 41 | + greenGrocer.buyTomatoes(2), |
| 42 | + greenGrocer.buyCauliflower(1), |
| 43 | + ]; |
| 44 | + |
| 45 | + const timeTaken = (Date.now() - beforeBuy).toFixed(2); |
| 46 | + |
| 47 | + console.log('You bought: ', fruitsAndVegetables); |
| 48 | + console.log('This took: ' + timeTaken + 'ms\n'); |
| 49 | +} |
| 50 | + |
| 51 | +function visitCheeseShop() { |
| 52 | + console.log('=== Cheese Shop ==='); |
| 53 | + const beforeBuy = Date.now(); |
| 54 | + |
| 55 | + const cheese = cheeseShop.buyCheese('belegen', 500); |
| 56 | + |
| 57 | + const timeTaken = (Date.now() - beforeBuy).toFixed(2); |
| 58 | + |
| 59 | + console.log('You bought: ' + cheese + ''); |
| 60 | + console.log('This took: ' + timeTaken + 'ms\n'); |
| 61 | +} |
| 62 | + |
| 63 | +function visitBakery() { |
| 64 | + console.log('=== Bakery ==='); |
| 65 | + const beforeBuy = Date.now(); |
| 66 | + |
| 67 | + const breadReady = function(bread) { |
| 68 | + const timeTaken = (Date.now() - beforeBuy).toFixed(2); |
| 69 | + console.log('--- Callback from Bakery ---'); |
| 70 | + console.log('You bought: ' + bread + ''); |
| 71 | + console.log('This took: ' + timeTaken + 'ms\n'); |
| 72 | + }; |
| 73 | + |
| 74 | + bakery.buyBread(breadReady, 'fijn volkoren', 1); |
| 75 | + console.log(`We'll call you back!\n`); |
| 76 | +} |
| 77 | + |
| 78 | +visitGreengrocer(); // ?. |
| 79 | +visitBakery(); // ?. |
| 80 | +visitCheeseShop(); // ?. |
0 commit comments