-
Notifications
You must be signed in to change notification settings - Fork 0
/
objectFactoriesPractice.js
47 lines (42 loc) · 1.51 KB
/
objectFactoriesPractice.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
// let chile = {
// name: 'The Republic of Chile',
// continent: 'South America',
// getDescription() {
// return this.name + ' is located in ' + this.continent + '.';
// },
// };
// let canada = {
// name: 'Canada',
// continent: 'North America',
// getDescription() {
// return this.name + ' is located in ' + this.continent + '.';
// },
// };
// let southAfrica = {
// name: 'The Republic of South Africa',
// continent: 'Africa',
// getDescription() {
// return this.name + ' is located in ' + this.continent + '.';
// },
// };
function makeCountry(name, continent, visited = false) {
return { name,
continent,
visited,
getDescription() {
let visitString = this.visited ? 'I have visited ' : "I haven't visited ";
return this.name + ' is located in ' + this.continent + '. ' + visitString + this.name;
},
visitCountry() {
this.visited = true;
},
};
}
let chile = makeCountry('The Republic of Chile', 'South America');
let canada = makeCountry('Canada', 'North America');
let southAfrica = makeCountry('The Republic of South Africa', 'Africa');
console.log(chile.getDescription()); // "The Republic of Chile is located in South America."
console.log(canada.getDescription()); // "Canada is located in North America."
console.log(southAfrica.getDescription()); // "The Republic of South Africa is located in Africa."
canada.visitCountry();
console.log(canada.getDescription());