forked from DevMountain/javascript-4-afternoon-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.js
More file actions
138 lines (93 loc) · 3.04 KB
/
context.js
File metadata and controls
138 lines (93 loc) · 3.04 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
/*
Once you complete a problem, refresh ./context.html in your browser and check to see if the problem's test(s) are passing.
Passed tests will be indicated by a green circle.
Failed tests will be indicated by a red X.
You can refresh the page at any time to re-run all the tests.
Context is the value of the "this" keyword which is a reference to the object that "owns" the executing code
*/
////////// PROBLEM 1 //////////
/*
Create an object called user which has the following properties.
username --> which is a string
email --> which is a string
getUsername --> which is a function that returns the current object's username property. *Don't use 'user' instead use the 'this' keyword*
*/
//Code Here
let user = {
username: "Blamb31",
email: "blake.lamb31@gmail.com",
getUsername: function() {
return this.username
}
}
////////// PROBLEM 2 //////////
/*
Below we have the class Animal. The eat method is using the "this" keyword. Use the "new" keyword to assign context to "this", and save the instance to a variable named animal1. You can pass anything you want in for name, species and food.
*/
class Animal {
constructor(name, species, food) {
this.name = name
this.species = species
this.food = food
}
eat() {
return this.name + ' is a ' + this.species + ' and likes to eat ' + this.food;
}
}
//Code Here
let animal1 = new Animal("Max", "Dog", "Dog Food")
////////// PROBLEM 3 //////////
/*
Use the bind method to assign context of the "this" keyword in the sayHi function to the user object; and save the bound function to a variable named whoSaysHi.
*/
function sayHi(greeting) {
return this.name + ' says ' + greeting
}
let who = {
name: 'Scuba Steve',
age: 35,
location: 'Belize'
}
//Code Here
let whoSaysHi = sayHi.bind(who)
////////// PROBLEM 4 //////////
/*
here we have a function that just returns the "this" keyword. We will give context to "this", and your job is to tell us what the context is.
*/
function whatIsThis() {
console.log(this)
return this
}
// uncomment the line below and tell us what the context of "this" is for whatIsThis()
let context1 = window
let product = {
name: 'snake plant',
price: 45.32,
description: 'Beautiful plant that can help filter the air inside your house.'
}
let func = whatIsThis.bind(product)
// uncomment the line below and tell us what the context of "this" is when we invoke func
let context2 = product
let vacation = {
location: 'Hawaii',
price: 3000,
days: 7,
nights: 6,
whatIsThis: whatIsThis
}
// uncomment the line below and tell us what the context of "this" is when we invoke vacation.whatIsThis
let context3 = vacation
vacation.whatIsThis()
class Family {
constructor(numParents, numKids, numPets) {
this.numParents = numPets;
this.numKids = numKids;
this.numPets = numPets;
}
whatIsThis() {
return this
}
}
let family1 = new Family(2, 4, 1)
// uncomment the line below and tell us what the context of "this" is for the instance of Family created above.
let context4 = family1