|
9 | 9 | - [String Methods & Concatenation](#string-methods--concatenation) |
10 | 10 | - [Template Literals](#template-literals) |
11 | 11 | - [Arrays & Array Methods](#arrays--array-methods) |
| 12 | +- [Object Literals](#object-literals) |
12 | 13 |
|
13 | 14 | ## Intro & File Setup |
14 | 15 |
|
@@ -352,3 +353,61 @@ Arrays are not immutable, meaning that we can insert, delete and change array va |
352 | 353 | - `splice()` changes the contents of an array by removing existing elements and/or adding new elements |
353 | 354 | - `reverse()` reverses an array, the first array element becomes the last, and the last array element becomes the first |
354 | 355 | - `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