-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.js
63 lines (52 loc) · 1.14 KB
/
objects.js
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
const robot = {
model: 'B-4MI',
mobile: true,
greeting() {
console.log(`I'm model ${this.model}, how may I be of service?`);
}
}
const massProdRobot = (model, mobile) => {
return {
model,
mobile,
greeting() {
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;
}
}
class User {
constructor(name) {
// invokes the setter
this.name = name;
}
get name() {
return this._name;
}
set name(value) {
if (value.length < 4) {
alert("Name is too short.");
return;
}
this._name = value;
}
}
let user = new User("John");
alert(user.name); // John
user = new User(""); // Name is too short.