Skip to content

Commit c1265cd

Browse files
author
Genesis Gabiola
committed
End object literals
1 parent 89ab4f7 commit c1265cd

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* ******************************
3+
* Object Literals
4+
* ******************************
5+
**/
6+
7+
/**
8+
* ********************
9+
* Creating Object Literal
10+
* ********************
11+
**/
12+
const person = {
13+
firstName: 'John',
14+
lastName: 'Doe',
15+
age: 36,
16+
email: 'johndoe@gmail.com',
17+
hobbies: ['music', 'sports'],
18+
address: {
19+
city: 'Manchester',
20+
state: 'NH'
21+
},
22+
getBirthYear: function() {
23+
return 2019 - this.age;
24+
}
25+
};
26+
27+
let val;
28+
val = person;
29+
30+
/**
31+
* ********************
32+
* Get specific value using the key
33+
* ********************
34+
**/
35+
val = person.firstName; // Method 1: Dot Notion
36+
val = person['firstName']; // Method 2: Brachet Notation
37+
val = person.age;
38+
val = person.hobbies[1];
39+
val = person.address.state;
40+
val = person.address['city'];
41+
val = person.getBirthYear();
42+
43+
console.log(val);
44+
45+
/**
46+
* ********************
47+
* Arrays of Objects
48+
* ********************
49+
**/
50+
const people = [
51+
{ name: 'John', age: 30 },
52+
{ name: 'Mike', age: 23 },
53+
{ name: 'Nancy', age: 40 }
54+
];
55+
56+
// loop through array and print out the names of each person within the people array.
57+
for (let i = 0; i < people.length; i++) {
58+
console.log(people[i].name);
59+
}

02-fundamentals/README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [String Methods & Concatenation](#string-methods--concatenation)
1010
- [Template Literals](#template-literals)
1111
- [Arrays & Array Methods](#arrays--array-methods)
12+
- [Object Literals](#object-literals)
1213

1314
## Intro & File Setup
1415

@@ -352,3 +353,61 @@ Arrays are not immutable, meaning that we can insert, delete and change array va
352353
- `splice()` changes the contents of an array by removing existing elements and/or adding new elements
353354
- `reverse()` reverses an array, the first array element becomes the last, and the last array element becomes the first
354355
- `concat()` is used to merge two or more arrays and does not change the existing arrays, but instead returns a new array
356+
357+
358+
## Object Literals
359+
360+
Create a variable and place the values within curly braces (`{...}`) which defines the variable as an Object
361+
362+
**Note**: you can add arrays within Objects, embed Objects within Objects and add functions within objects, E.g.
363+
364+
```js
365+
const obj = {
366+
arr: [value, value, value],
367+
obj: {
368+
obj1: value,
369+
obj2: value
370+
},
371+
Function: function() {...}
372+
};
373+
374+
// Arrays of Objects
375+
const arr = [
376+
{key: value, lastName: value},
377+
{key: value, lastName: value},
378+
{key: value, lastName: value}
379+
];
380+
```
381+
382+
### Key: Value Pairs
383+
384+
In Object Literals we use what is called Key:Value pairs, E.g.
385+
386+
```js
387+
const obj = {
388+
key: value,
389+
key: value
390+
}
391+
```
392+
393+
### Methods to Access Value using the Key
394+
395+
```js
396+
obj.key; // Method 1: Dot Notation
397+
obj['key']; // Method 2: Bracket Notation
398+
```
399+
400+
**Note**: Method 1 Dot Notation is the preferred syntax.
401+
402+
### This keyword
403+
404+
Use `this` keyword inside our functions inside our object,to access the property values within our object, that means `this` relates to the current object
405+
406+
```js
407+
const obj = {
408+
key: value,
409+
method() {
410+
...
411+
}
412+
}
413+
```

0 commit comments

Comments
 (0)