-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs8.js
More file actions
72 lines (53 loc) · 2.69 KB
/
Copy pathjs8.js
File metadata and controls
72 lines (53 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Object literals and the dot notation
/* One of the most common ways of building an object in JavaScript is using the object literal syntax: {}.
To be able to access this object literal, it is very common to assign it to a variable */
var user = {}
/* Now an object literal is assigned to the variable user, which means that the object it is bound to can be extended and manipulated in a myriad of ways.
Sometimes, an entire object can be immediately built, using the object literal syntax, by specifying the object's properties, delimited as key-value pairs, using syntax that was already covered in an earlier lesson item in this lesson.
Here's one such previously built object: */
//creating an object with properties and their values
var assistantManager = {
rangeTilesPerTurn: 3,
socialSkills: 30,
streetSmarts: 30,
health: 40,
specialAbility: "young and ambitious",
greeting: "Let's make some money"
}
/* The beauty of this syntax is that it's so easily readable.
It essentially consists of two steps:
1. Declaring a new variable and assigning an object literal to it - in other words, this: var assistantManager = {}
2. Assigning the values to each of the object's keys, using the assignment operator, =
Notice that it's very easy to build any kind of an object in JavaScript using this example syntax.
For example, here's a table object: */
var table = {
legs: 3,
color: "brown",
priceUSD: 100,
}
//To access the table object, I can simply console log the entire object
console.log(table);//display the object in the developer console
//The returned value is the entire table object:
{legs: 3, color; 'brown', priceUSD; 100}
//Additionally, I can console log any individual property, like this
console.log(table.color); // 'brown'
//Now that I have this "syntax recipe", I can build any other object in a similar way
var house = {
rooms: 3,
color: "brown",
priceUSD: 10000,
}
//An alternative approach of building objects is to first save an empty object literal to a variable,
//then use the dot notation to declare new properties on the fly, and use the assignment operator to add
//values to those properties; for example
var house2 = {};
house2.rooms = 4;
house2.color = "pink";
house2.priceUSD = 12345;
//Additionally, nothing is preventing me from combining the two approaches. For example
console.log(house); // {rooms: 3, color: "brown", priceUSD: 10000}
house.windows = 10;
console.log(house); // {rooms: 3, color: "brown", priceUSD: 10000, windows: 10}
//This flexbility additionally means that I can update already existing properties, not just add new ones
house.windows = 11;
console.log(house); // {rooms: 3, color: "brown", priceUSD: 10000, windows: 11}