-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjects-lecture.html
More file actions
180 lines (161 loc) · 4.18 KB
/
objects-lecture.html
File metadata and controls
180 lines (161 loc) · 4.18 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Objects Intro</title>
</head>
<body>
<h1>Intro to Objects</h1>
<script>
"use strict";
/*
JS Objects
- like a named index array
- used to store related state and behavior
- where arrays are good for storing lists of data, objects store related aspects of a greater structure
- we have already used objects: String, Array, Math, Number
*/
// Creating Objects (dog, user, forecast, dictionaryEntry, movie, post, etc.)
// New Object Instance
var dog = new Object();
console.log(typeof dog);
// Object Literal Notation
var cat = {};
console.log(typeof cat);
// Setting Properties on a Custom Object
// create a pet object that has the following properties...
// givenName
// age
// species
// hasOffspring
// valueInDollars
var pet = {};
pet.givenName = 'Freckles';
pet.age = 4;
pet.species = 'Dog';
pet.hasOffspring = false;
pet.valueInDollars = 2000;
pet.isCute = true;
pet.medicalHistory = {};
pet.treatsEaten = [];
pet.treatsEaten.push('Today he ate a bone');
pet.treatsEaten.push('Today he ate a shoe');
pet.treatsEaten.push('Today he ate a biscuit');
console.log(pet);
var pet = { givenName: 'Freckles', ageInYears: 4, species: 'Dog'};
pet.ageInYears = 5;
console.log(pet);
// var pet = {};
//
// pet.givenName = 'Sparkles';
// pet.age = 12;
// pet.species = 'turtle';
// pet.hasOffspring = true;
// pet['value-in-dollars'] = 400; // alternative syntax for assigning properties
// console.log(pet);
// var pet2 = {
// givenName: 'Rex',
// age: 5,
// species: 'dog',
// hasOffspring: false,
// valueInDollars: 'priceless'
// };
// Peeking into an Object
// console.log(pet);
// Accessing Properties on an Object
// console.log(pet.givenName);
// console.log(pet2.givenName);
// console.log(pet2['givenName']);
//
// pet.givenName = "Sabrina";
// console.log(pet.givenName);
// !! MINI-EXERCISE 1 !!
// Nested Values
// Add the following to a pet object...
// altNames
// vitals: averageTemp, restingHeartRate, isHungry
var pet3 = {
name: 'Bowser',
age: 6,
species: 'dog',
hasOffspring: false,
valueInDollars: 'priceless',
altNames: [
'Bowser Boy',
'Bowser Man',
'Bowser Buddy',
'Puggle Wuggle Boy'
],
vitals: {
averageTempDegF: 101,
averageRestingHeartRate: 80,
isHungry: true
}
};
// log if Bowser is hungry
console.log(pet3.vitals.isHungry);
// log Bowser's average heartRate
console.log(pet3.vitals.averageRestingHeartRate);
// log Bowser's most recent alternative name
console.log(pet3.altNames[3])
// change age to 7 and isHungry to false
pet3.age = 7;
pet3.vitals.isHungry = true;
// Arrays of Objects
var pets = [
{
givenName: 'Spot',
isDog: true
},
{
givenName: 'Max',
isDog: false
},
{
givenName: 'Goldy',
isDog: false
}
];
for (var i = 0; i < pets.length; i += 1) {
console.log(pets[i].givenName);
}
//change every pet's name to Bowser
pets.forEach(function(pet){
pet.givenName = 'Bowser';
})
// !! MINI EXERCISE 2 !!
// Getting a List of Object Keys as an array
// console.log(Object.keys(pet4));
// Assigning Functionality to an Object
// var pet4 = {
// name: "Pickles",
// age: 20,
// species: "horse",
// eat: function (food) {
// console.log("nom nom nom " + food);
// },
// gallop: function() {
// console.log("THUNK THUMP...THUNK THUMP!");
// }
// };
//
// pet4.eat("carrots");
// pet4.gallop();
// The this Keyword
// var pet6 = {
// name: "Sparky",
// age: 3,
// getOlder: function() {
// console.log(this.age);
// this.age += 1;
// }
// };
//
// console.log(pet6.age);
// pet6.getOlder();
// console.log(pet6.age);
// why use this vs. the object name: https://stackoverflow.com/questions/12799702/javascript-this-versus-object-name
// !! MINI EXERCISE 3 !!
</script>
</body>
</html>