|
1 |
| -// Objects Basics |
| 1 | +// nested objects |
| 2 | +// set variable as property value |
| 3 | +// dot notation vs bracket notation |
2 | 4 |
|
3 |
| -/* |
4 |
| -Objects are collections of key (property) value pairs |
5 |
| -Property values can be strings, numbers, booleans, arrays and functions however if the property value is a function it's called a method |
6 |
| -*/ |
| 5 | +const age = 30; |
7 | 6 |
|
8 |
| -// object literal syntax,{} |
9 |
| -// dot notation |
| 7 | +let random = "random value"; |
| 8 | +random = "age"; |
10 | 9 |
|
11 | 10 | const person = {
|
12 | 11 | name: "john",
|
13 |
| - age: 25, |
| 12 | + age: age, |
14 | 13 | siblings: ["anna", "susan", "peter"],
|
15 | 14 | greet: function (name) {
|
16 | 15 | console.log(`Hi, I am ${name}`);
|
17 | 16 | },
|
18 | 17 | sayHello(name) {
|
19 | 18 | console.log(`Hello, My name is ${name}`);
|
20 | 19 | },
|
| 20 | + job: { |
| 21 | + title: "developer", |
| 22 | + company: { |
| 23 | + name: "coding addict", |
| 24 | + address: "123 main street", |
| 25 | + }, |
| 26 | + }, |
| 27 | + "random-value": "random", |
21 | 28 | };
|
22 | 29 |
|
23 |
| -console.log(person.name); |
| 30 | +console.log(person.job.company.name); |
| 31 | + |
| 32 | +console.log(person.work); // undefined |
24 | 33 |
|
25 |
| -// aassign and access the variable |
26 |
| -const firstName = person.sayHello("john"); |
| 34 | +/* Cannot read properties of undefined (reading 'title') */ |
| 35 | +// console.log(person.work.title); |
27 | 36 |
|
28 |
| -// Adding property |
29 |
| -person.city = "illinois"; |
| 37 | +console.log(person["random-value"]); |
30 | 38 |
|
31 |
| -//Deleting property |
32 |
| -// delete person.siblings; |
33 |
| -const deleteSiblings = delete person.siblings; |
34 |
| -console.log(deleteSiblings); |
| 39 | +// check in person object for the property with age which is value of "random" |
| 40 | +console.log(person[random]); |
35 | 41 |
|
36 | 42 | console.log(person);
|
0 commit comments