|
1 |
| -/* In Reg Functions (not arrow) "this" |
2 |
| -determined by "HOW"!!! a function is invoked (left of .) |
3 |
| -
|
4 |
| -defaults to global - window |
5 |
| -arrow functions - pump the breaks |
6 |
| -*/ |
7 |
| - |
8 |
| -// console.log(this); |
9 |
| - |
10 |
| -function showThis() { |
11 |
| - console.log(this); |
| 1 | +// Blue Print |
| 2 | +// Factory Functions and Constructor Functions |
| 3 | +// Factory Functions |
| 4 | + |
| 5 | +// const john = { |
| 6 | +// firstName: "john", |
| 7 | +// lastName: "anderson", |
| 8 | +// fullName: function () { |
| 9 | +// // console.log(this); |
| 10 | +// console.log( |
| 11 | +// `Hello, my name is ${this.firstName} ${this.lastName} and I love JS` |
| 12 | +// ); |
| 13 | +// }, |
| 14 | +// }; |
| 15 | + |
| 16 | +// const bob = { |
| 17 | +// firstName: "bob", |
| 18 | +// lastName: "anderson", |
| 19 | +// fullName: function () { |
| 20 | +// // console.log(this); |
| 21 | +// console.log(`Hello, my name is ${this.firstName} ${this.lastName}`); |
| 22 | +// }, |
| 23 | +// }; |
| 24 | + |
| 25 | +// john.fullName(); |
| 26 | +// bob.fullName(); |
| 27 | + |
| 28 | +function createPerson(firstName, lastName) { |
| 29 | + return { |
| 30 | + firstName: firstName, |
| 31 | + lastName: lastName, |
| 32 | + fullName: function () { |
| 33 | + console.log( |
| 34 | + `Hello, my name is ${this.firstName} ${this.lastName} and I love JS` |
| 35 | + ); |
| 36 | + }, |
| 37 | + }; |
12 | 38 | }
|
13 | 39 |
|
14 |
| -const john = { |
15 |
| - name: "john", |
16 |
| - showThis: showThis, |
17 |
| -}; |
18 |
| - |
19 |
| -const bob = { |
20 |
| - name: "bob", |
21 |
| - showThis: showThis, |
22 |
| -}; |
23 |
| - |
24 |
| -john.showThis(); |
25 |
| -bob.showThis(); |
26 |
| - |
27 |
| -showThis(); |
28 |
| - |
29 |
| -const btn1 = document.querySelector(".btn-1"); |
30 |
| -const btn2 = document.querySelector(".btn-2"); |
| 40 | +const john = createPerson("john", "anderson"); |
| 41 | +john.fullName(); |
31 | 42 |
|
32 |
| -btn1.addEventListener("click", showThis); |
| 43 | +const bob = createPerson("bob", "jordan"); |
| 44 | +bob.fullName(); |
33 | 45 |
|
34 |
| -btn2.addEventListener("click", function () { |
35 |
| - showThis(); |
36 |
| -}); |
| 46 | +const susy = createPerson("susy", "apple"); |
| 47 | +susy.fullName(); |
0 commit comments