forked from bloominstituteoftechnology/JavaScript-II-Mini
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
58 lines (43 loc) · 1.35 KB
/
this.js
File metadata and controls
58 lines (43 loc) · 1.35 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window binding - occurs in browser when not using other bindings, default binding, global
* 2. Implicit - whatever is to the left of the dot is the target
* 3. New Binding - a constructor function that returns an object
* 4. Explicit Binding - when you explicitly pass a this through call(), apply() or bind()
*
* write out a code example of each explanation above
*/
console.log('hello world!');
// Principle 1
// code example for Window Binding
function coding(language) {
console.log(this.code);
return language;
}
window.code = "javascript";
// Principle 2
// code example for Implicit Binding
const myDay = {
listen: "Sufjan Stevens",
code: "JavaScript",
dinner: "meatloaf",
cook: function () {
console.log(this.dinner);
}
};
myDay.cook();
// Principle 3
// code example for New Binding
function veganSandwiches(name, mainIngredient, secondaryIngredient) {
this.name = name;
this.mainIngredient = mainIngredient;
this.secondaryIngredient = secondaryIngredient;
}
var sandwich1 = new veganSandwiches("Refried Bean Wrap", "Refried Beans", "Avocado")
// Principle 4
// code example for Explicit Binding
function eating() {
return console.log(this.name + " is a functon, not a recipe!");
}
eating.call(veganSandwiches);