-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakingObjects.js
More file actions
135 lines (102 loc) · 3.19 KB
/
Copy pathmakingObjects.js
File metadata and controls
135 lines (102 loc) · 3.19 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//Objects are made in this syntax:
/*
let objectName = {
key : value, these key and value pairs are the object's properties
key : value,
key:value
}
*///example:
let spaceship ={
'Fuel Type':'diesel',
color:'silver'
}
//to access the properties of an object, theres two ways
//1. use the .key (dot notation)
//2. bracket notation [] (use this for strings tho technically u can also use it for everything)
spaceship.color //returns silver
spaceship.bugs //returns undefined because never existed for the object
spaceship['Fuel Type'] //returns diesel
spaceship['color'] //also returns silver
let spaceship2 = {
'Fuel Type' : 'Turbo Fuel',
'Active Mission' : true,
homePlanet : 'Earth',
numCrew: 5
};
let propName = 'Active Mission';
// Write your code below
let isActive = spaceship2['Active Mission']
console.log(isActive) //returns true for this
//objects are mutable so we can change their properties whenever by accessing them
let raccoon = {
hunger:8,
strength:1000,
name:'Henry'
}
raccoon.name = 'Joseph'//reassigns the raccoons name to Joseph
console.log(raccoon.name)
//we can delete a property with the delete operator
delete raccoon.hunger //removes hunger property
//we can also add new properties by simply assigning a value to that type using
//bracket or dot notation
raccoon.hat = 'Widebrimmed'
console.log(raccoon.hat) //prints that new property we created
//we can also get objects methods in the same way we assigned key and value pairs
/*
const alienShip = {
invade: function () {
console.log('Hello! We have come to dominate your planet. Instead of Earth, it shall be called New Xaculon.')
}
};
here, the key is the method's name 'invade' while the value is an anonymous function
and ever since ES6, we don't even have to write the word function or include the :
so it can be written like:
const alienShip = {
invade () {
console.log('Hello! We have come to dominate your planet. Instead of Earth, it shall be called New Xaculon.')
}
};
*/
//example
let retreatMessage = 'We no longer wish to conquer your planet. It is full of dogs, which we do not care for.';
const alienShip ={
retreat () {
console.log(retreatMessage)
},
takeOff () {
console.log('Spim... Borp... Glix... Blastoff!')
}
}
alienShip.retreat()
alienShip.takeOff()
//nested Objects example:
let spaceVessel = {
passengers: [{name: 'Space Dog'}], //an array of passengers with the name space dog
telescope: {
yearBuilt: 2018,
model: "91031-XLT",
focalLength: 2032
},
crew: {
captain: {
name: 'Sandra',
degree: 'Computer Engineering',
encourageTeam() { console.log('We got this!') },
'favorite foods': ['cookies', 'cakes', 'candy', 'spinach'] }
},
engine: {
model: "Nimbus2000"
},
nanoelectronics: {
computer: {
terabytes: 100,
monitors: "HD"
},
'back-up': {
battery: "Lithium",
terabytes: 50
}
}
};
let capFave = spaceVessel.crew.captain['favorite foods'][0] //returns'cookies'
let firstPassenger = spaceVessel.passengers[0] //returns first passenger in the passenger array