-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_enhancements.js
101 lines (79 loc) · 2.3 KB
/
object_enhancements.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
// ***** Object Enhancements Exercise *****
// In this exercise, you’ll refactor some ES5 code into ES2015. Write your code in the sections with a comment to “Write an ES2015 Version”.
// Same keys and values
// function createInstructor(firstName, lastName){
// return {
// firstName: firstName,
// lastName: lastName
// }
// }
let createInstructor = (firstName, lastName) => {
return {
firstName,
lastName,
}
}
console.log(createInstructor("Frank", "Azzaro"));
// Computed Property Names
// var favoriteNumber = 42;
// var instructor = {
// firstName: "Colt"
// }
// instructor[favoriteNumber] = "That is my favorite!"
//
// Computed Property Names ES2015
// /* Write an ES2015 Version */
let instructor = (firstName, number) => {
return {
firstName,
[number]: "That is my favorite!"
}
}
console.log(instructor("Frank", 23))
// Object Methods
// var instructor = {
// firstName: "Colt",
// sayHi: function(){
// return "Hi!";
// },
// sayBye: function(){
// return this.firstName + " says bye!";
// }
// }
//
// Object Methods ES2015
// /* Write an ES2015 Version */
let instructor2 = {
firstName: "Mario",
sayHi() {
return "Hi!"
},
sayBye() {
return `${this.firstName} says bye!`
}
}
console.log(instructor2.sayBye());
// 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"
let createAnimal = (species, verb, noise) => {
return {
species,
verb,
noise,
[verb]: function () {
return noise
}
}
}
let animal = createAnimal("Dog", "bark", "Woof!")
console.log(animal.bark());