Skip to content

Commit 64355ac

Browse files
committed
"this" - Keyword Advanced
1 parent c460892 commit 64355ac

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

app.js

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
1-
// this
2-
// points to the object which is left of the dot
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);
12+
}
313

414
const john = {
5-
firstName: "john",
6-
lastName: "anderson",
7-
fullName: function () {
8-
console.log(this);
9-
console.log(`hello, my name is ${this.firstName} ${this.lastName}`);
10-
},
15+
name: "john",
16+
showThis: showThis,
1117
};
1218

1319
const bob = {
14-
firstName: "peter",
15-
lastName: "anderson",
16-
fullName: function () {
17-
console.log(this);
18-
console.log(`hello, my name is ${this.firstName} ${this.lastName}`);
19-
},
20+
name: "bob",
21+
showThis: showThis,
2022
};
2123

22-
john.fullName();
23-
bob.fullName();
24+
john.showThis();
25+
bob.showThis();
26+
27+
showThis();
28+
29+
const btn1 = document.querySelector(".btn-1");
30+
const btn2 = document.querySelector(".btn-2");
31+
32+
btn1.addEventListener("click", showThis);
33+
34+
btn2.addEventListener("click", function () {
35+
showThis();
36+
});

index.html

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

0 commit comments

Comments
 (0)