-
Notifications
You must be signed in to change notification settings - Fork 0
/
object-enhance.js
123 lines (101 loc) · 2.55 KB
/
object-enhance.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
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
////////////////////////////////////////////////---Object-Enhancement-Exercises---///////////////////////////////////////////////////////
// Same keys and values
function createInstructor(firstName, lastName){
return {
firstName,
lastName,
}
}
// Computed Property Names
let favoriteNumber = 42;
const instructor1 = {
firstName: "Colt",
[favoriteNumber]: "That is my favorite!",
}
// Object Methods
const instructor = {
firstName: "Colt",
sayHi() {
return "Hi!";
},
sayBye() {
return `${firstName} says bye!`;
}
}
// createAnimal function
// Write a function which generates an animal object. The function should accepts 3 arguments:
// species: the species of animal (‘cat’, ‘dog’)
// verb: a string used to name a function (‘bark’, ‘bleet’)
// noise: a string to be printed when above function is called (‘woof’, ‘baaa’)
// Use one or more of the object enhancements we’ve covered.
const d = createAnimal("dog", "bark", "Woooof!")
// {species: "dog", bark: ƒ}
d.bark() //"Woooof!"
const s = createAnimal("sheep", "bleet", "BAAAAaaaa")
// {species: "sheep", bleet: ƒ}
s.bleet() //"BAAAAaaaa"
function animal(species, verb, noise) {
const animalObj = {
species,
[verb]() {
return noise
}
};
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
// function makePerson (first, last, age) {
// return {
// first: first,
// last: last,
// age: age,
// isAlive: true,
// }
// }
function makePerson (first, last, age) {
return {
first,
last,
age,
isAlive: true,
}
};
/////////////////////////////////////////////////////////////////////////
// const mathStuff = {
// x: 200,
// add: function(a,b) {
// return a + b;
// },
// square: function(a) {
// return a * a;
// },
// };
const mathStuff = {
x: 200,
add(a,b) {
return a + b;
},
square(a) {
return a * a;
},
multiply: (a,b) => {
return a * b;
},
};
/////////////////////////////////////////////////////////////////////////
//An object which makes colors searchable either from the name or the hex code.
// const colors = {
// periwinkle: '9c88ff',
// '9c88ff': periwinkle
// };
// function makeColor(name, hex) {
// const color = {};
// color[name] = hex;
// color[hex] = name;
// return color;
// }
function makeFast(name, hex) {
return {
[name]: hex,
[hex]: name,
};
}