|
5 | 5 | <meta charset="UTF-8">
|
6 | 6 | <meta name="viewport" content="width=device-width,initial-scale=1.0">
|
7 | 7 | <title>New, This, Prototypes and Classes</title>
|
8 |
| - <link rel="stylesheet" href="../base.css"> |
| 8 | + <link rel="stylesheet" href="../base.css" type="html/css"> |
9 | 9 | </head>
|
10 | 10 |
|
11 | 11 | <body>
|
12 | 12 |
|
| 13 | + <button id="button1">Click Me!</button> |
| 14 | + <button id="button2">Click Me!</button> |
| 15 | +<script> |
| 16 | + // THIS KEYWORD |
| 17 | + |
| 18 | + // add event listeners to button by using ID |
| 19 | + button1.addEventListener('click', tellMeAboutTheButton) |
| 20 | + button2.addEventListener('click', tellMeAboutTheButton) |
| 21 | + |
| 22 | + // access button by using this keyword |
| 23 | + // note the differences in scoping between declaration and arrow functions |
| 24 | + // when a new function is made within another, it's scope is set to the window object |
| 25 | + function tellMeAboutTheButton(){ |
| 26 | + console.log('outer: ',this) |
| 27 | + this.textContent = 'DoneClicked!' |
| 28 | + setTimeout(() => { |
| 29 | + console.log('innerArrow: ',this) |
| 30 | + }, 1000); |
| 31 | + setTimeout(function(){ |
| 32 | + console.log('innerDeclaration: ',this) |
| 33 | + }, 1000); |
| 34 | + } |
| 35 | + |
| 36 | + // OR |
| 37 | + |
| 38 | + // access button by using event object |
| 39 | + function tellMeAboutTheButtonEvent(e){ |
| 40 | + console.log(e) |
| 41 | + e.currentTarget.textContent = 'EVENTS RULE' |
| 42 | + } |
| 43 | + |
| 44 | +</script> |
| 45 | +<script> |
| 46 | + // NEW KEYWORD |
| 47 | + |
| 48 | + // create a new object model which takes in two arguments |
| 49 | + function Pizza(toppings = [], customer){ |
| 50 | + // sets instances of new Pizza |
| 51 | + this.toppings = toppings; |
| 52 | + this.customer = customer; |
| 53 | + this.slices = 8; |
| 54 | + // creates methods attached to object model |
| 55 | + this.itsReady = function(){ |
| 56 | + console.log(`Da pizza is a ready, come and get it ${customer}!`) |
| 57 | + } |
| 58 | + this.eatASlice = function(){ |
| 59 | + if (this.slices > 0){ |
| 60 | + this.slices-- |
| 61 | + console.log('Mmmmm, delicious...😋') |
| 62 | + } else { |
| 63 | + console.log(`I'm out of 🍕`) |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + const pepperoniPizza = new Pizza(['pepperoni'], 'David') |
| 69 | + pepperoniPizza.itsReady() |
| 70 | + console.log(pepperoniPizza) |
| 71 | + const ericsPizza = new Pizza(['cheese', 'onion', 'sausage'], 'Eric') |
| 72 | + console.log(ericsPizza) |
| 73 | + ericsPizza.eatASlice() |
| 74 | + ericsPizza.eatASlice() |
| 75 | + ericsPizza.eatASlice() |
| 76 | + ericsPizza.eatASlice() |
| 77 | + ericsPizza.eatASlice() |
| 78 | + ericsPizza.eatASlice() |
| 79 | + ericsPizza.eatASlice() |
| 80 | + ericsPizza.eatASlice() |
| 81 | + ericsPizza.eatASlice() |
| 82 | + |
| 83 | + |
| 84 | +</script> |
13 | 85 | </body>
|
14 | 86 |
|
15 | 87 | </html>
|
0 commit comments