|
9 | 9 | </head> |
10 | 10 |
|
11 | 11 | <body> |
12 | | - <p>Hey</p> |
13 | | - <button>Click me</button> |
14 | | - <div class="wrapper"> |
15 | | - <p>Hey im in a wrapper</p> |
16 | | - </div> |
17 | | - <script> |
18 | | - const person = { |
19 | | - name: 'Wes Bos', |
20 | | - sayHi() { |
21 | | - console.log(this); |
22 | | - console.log(`hey ${this.name}`); |
23 | | - return `hey ${this.name}`; |
24 | | - } |
25 | | - }; |
26 | 12 |
|
27 | | - const jenna = { name: 'Jenna' }; |
28 | | - |
29 | | - const sayHi = person.sayHi.bind({ name: 'Harry' }); |
30 | | - |
31 | | - // QS Example |
32 | | - // by calling bind against querySelector, we say when the $ function is run, use `document` as the `this` value. |
33 | | - const $ = document.querySelector.bind(document); |
34 | | - const lookFor = document.querySelectorAll.bind(document); |
35 | | - |
36 | | - const wrapper = document.querySelector('.wrapper'); |
37 | | - const p = wrapper.querySelector('p'); |
38 | | - console.log(p); |
39 | | - console.log($('p')); |
40 | | - console.log(lookFor('p')); |
41 | | - |
42 | | - const bill = { |
43 | | - total: 1000, |
44 | | - calculate: function (taxRate) { |
45 | | - console.log(this); |
46 | | - return this.total + (this.total * taxRate); |
47 | | - }, |
48 | | - describe(mealType, drinkType, taxRate) { |
49 | | - console.log(`Your meal of ${mealType} with a drink of ${drinkType} was ${this.calculate(taxRate)}`); |
50 | | - } |
51 | | - }; |
52 | | - |
53 | | - const total = bill.calculate(0.13); |
54 | | - const calc = bill.calculate.bind({ total: 500 }, 0.06); |
55 | | - |
56 | | - console.log(calc()); |
57 | | - const total2 = bill.calculate.call({ total: 500 }, 0.06); |
58 | | - console.log(total2); |
59 | | - |
60 | | - const total3 = bill.calculate.apply({ total: 325 }, [0.60]); |
61 | | - console.log(total3); |
62 | | - |
63 | | - const myMeal = bill.describe.call(bill, 'pizza', 'beer', 0.13); |
64 | | - console.log(myMeal); |
65 | | - const myMeal2 = bill.describe.apply(bill, ['pizza', 'beer', 0.13]); |
66 | | - console.log(myMeal2); |
67 | | - |
68 | | - </script> |
69 | 13 | </body> |
70 | 14 |
|
71 | 15 | </html> |
0 commit comments