-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvancedObjects.txt
More file actions
172 lines (144 loc) · 3.55 KB
/
advancedObjects.txt
File metadata and controls
172 lines (144 loc) · 3.55 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
const robot = {
model: 'B-4MI',
mobile: true,
greetMaster() {
console.log(`I'm model ${this.model}, how may I be of service?`);
}
}
const massProdRobot = (model, mobile) => {
return {
model,
mobile,
greetMaster() {
console.log(`I'm model ${this.model}, how may I be of service?`);
}
}
}
const shinyNewRobot = massProdRobot('TrayHax', true)
const chargingStation = {
_name: 'Electrons-R-Us',
_robotCapacity: 120,
_active: true,
_chargingRooms: ['Low N Slow', 'Middle of the Road', 'In and Output'],
set robotCapacity(newCapacity) {
if (typeof newCapacity === 'number') {
this._robotCapacity = newCapacity;
} else {
console.log(`Change ${newCapacity} to a number.`)
}
},
get robotCapacity() {
return this._robotCapacity;
}
}
const robot = {
model: '1E78V2',
energyLevel: 100,
provideInfo(){
return `I am ${this.model} and my current energy level is ${this.energyLevel}.`}
};
console.log(robot.provideInfo());
const robot = {
energyLevel: 100,
checkEnergy() {
console.log(`Energy is currently at ${this.energyLevel}%.`)
}
}
robot.checkEnergy();
const robot = {
_energyLevel: 100,
recharge(){
this._energyLevel += 30;
console.log(`Recharged! Energy is currently at ${this._energyLevel}%.`)
}
};
robot._energyLevel = 'high';
robot.recharge();
const robot = {
_model: '1E78V2',
_energyLevel: 100,
get energyLevel(){
if(typeof this._energyLevel === 'number'){
return 'My current energy level is ' this._energyLevel
}//end if
else {
return "System malfunction: cannot retrieve energy level."
}
}//end energyLevel
};//end robot
console.log(robot.energyLevel);
const robot = {
_model: '1E78V2',
_energyLevel: 100,
_numOfSensors: 15,
get numOfSensors(){
if(typeof this._numOfSensors === 'number'){
return this._numOfSensors;
} else {
return 'Sensors are currently down.'
}
},
set numOfSensors(num){
if(typeof num === 'number' && num >= 0){
this._numOfSensors = num;
} else {
console.log('Pass in a number that is greater than or equal to O')
}
}
};
robot.numOfSensors = 100;
console.log(robot.numOfSensors);
const robotFactory = (model, mobile) => {
return {
model: model,
mobile: mobile,
beep(){
console.log('Beep Boop');
}
}
};
const tinCan = robotFactory('P-500', true);
tinCan.beep();
function robotFactory(model, mobile){
return {
model,
mobile,
beep() {
console.log('Beep Boop');
}
}
}
// To check that the property value shorthand technique worked:
const newRobot = robotFactory('P-501', false)
console.log(newRobot.model)
console.log(newRobot.mobile)
const robot = {
model: '1E78V2',
energyLevel: 100,
functionality: {
beep() {
console.log('Beep Boop');
},
fireLaser() {
console.log('Pew Pew');
},
}
};
const { functionality } = robot;
functionality.beep();
const robot = {
model: 'SAL-1000',
mobile: true,
sentient: false,
armor: 'Steel-plated',
energyLevel: 75
};
// What is missing in the following method call?
const robotKeys = Object.keys(robot);
console.log(robotKeys);
// Declare robotEntries below this line:
const robotEntries = Object.entries(robot);
console.log(robotEntries);
// Declare newRobot below this line:
const newRobot = Object.assign({laserBlaster: true, voiceRecognition: true});
console.log(newRobot);