Skip to content

Commit 328fbf6

Browse files
committed
Factory Functions
1 parent 64355ac commit 328fbf6

File tree

2 files changed

+43
-34
lines changed

2 files changed

+43
-34
lines changed

app.js

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
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+
};
1238
}
1339

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();
3142

32-
btn1.addEventListener("click", showThis);
43+
const bob = createPerson("bob", "jordan");
44+
bob.fullName();
3345

34-
btn2.addEventListener("click", function () {
35-
showThis();
36-
});
46+
const susy = createPerson("susy", "apple");
47+
susy.fullName();

index.html

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
</head>
1313
<body>
1414
<h1>objects</h1>
15-
<button class="btn-1">click me</button>
16-
<button class="btn-2">click me</button>
1715
<!-- Link to JavaScript -->
1816
<script src="./app.js"></script>
1917
</body>

0 commit comments

Comments
 (0)