Skip to content

Commit c460892

Browse files
committed
'this' - Keyword Basics
1 parent 09ad812 commit c460892

File tree

1 file changed

+19
-38
lines changed

1 file changed

+19
-38
lines changed

app.js

Lines changed: 19 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,23 @@
1-
// nested objects
2-
// set variable as property value
3-
// dot notation vs bracket notation
4-
5-
const age = 30;
6-
7-
let random = "random value";
8-
random = "age";
9-
10-
const person = {
11-
name: "john",
12-
age: age,
13-
siblings: ["anna", "susan", "peter"],
14-
greet: function (name) {
15-
console.log(`Hi, I am ${name}`);
1+
// this
2+
// points to the object which is left of the dot
3+
4+
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}`);
1610
},
17-
sayHello(name) {
18-
console.log(`Hello, My name is ${name}`);
19-
},
20-
job: {
21-
title: "developer",
22-
company: {
23-
name: "coding addict",
24-
address: "123 main street",
25-
},
26-
},
27-
"random-value": "random",
2811
};
2912

30-
console.log(person.job.company.name);
31-
32-
console.log(person.work); // undefined
33-
34-
/* Cannot read properties of undefined (reading 'title') */
35-
// console.log(person.work.title);
36-
37-
console.log(person["random-value"]);
38-
39-
// check in person object for the property with age which is value of "random"
40-
console.log(person[random]);
13+
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+
};
4121

42-
console.log(person);
22+
john.fullName();
23+
bob.fullName();

0 commit comments

Comments
 (0)