forked from bloominstituteoftechnology/JavaScript-III
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththis.js
More file actions
96 lines (73 loc) · 2.18 KB
/
Copy paththis.js
File metadata and controls
96 lines (73 loc) · 2.18 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window binding - the god principle
Window binding occurs when this is left all alone. Orphaned "this" only has the window to depend on
* 2. Implicit binding -
Uses dot notation inside the object to reference an object property
* 3. New Binding -
hey object I'm going to uses the "new" keyword to introduce you two. You're now bound
* 4. Explicit Binding
.call - comma seperated
.apply - arrays
.bind - returns function
wala
*
* write out a code example of each explanation above
*/
// Principle 1
// code example for Window Binding
function WindowBind(bindtype){
console.log(this);
return bindtype;
}
WindowBind("this isn't bound to anything");
// Principle 2
// code example for Implicit Binding
const ImplicitObj = {
greeting: "Hello",
sayHello: function(name) {
console.log(`${this.greeting} ${name}, would you like to play a game?`);
}
};
ImplicitObj.sayHello("John");
// Principle 3
// code example for New Binding
function NewBinding(name) {
this.name = name;
this.playsgame = true;
this.question = function(){
console.log(`Will ${this.name} play the game? ${this.playsgame}, he will.`);
};
}
const John = new NewBinding("John");
John.question();
// Principle 4
// code example for Explicit Binding
let kitty = ["cat", "meow"];
function Animal (species, sound) {
this.species = species;
this.sound = sound;
}
function Dog (breed){
Animal.call(this, "Dog", "Woof");
this.breed = breed;
}
function Cat (breed){
Animal.apply(this, kitty);
this.breed = breed;
}
const Teddy = new Dog("Yorkie");
const Snuggles = new Cat("Tabby");
console.log(Teddy);
console.log(Snuggles);
const Turtle = {
name: "Franklin",
}
const actions = ["walk", "sleep", "eat", "poop"];
function printActions(walk, sleep, eat, poop){
console.log(`Hi, my name is ${this.name}. I like to ${walk}, ${sleep}, ${eat}, and ${poop}`);
}
printActions.call(Turtle, ...actions);
printActions.apply(Turtle, actions);
const turtleMessage = printActions.bind(Turtle) ("walk", "sleep", "eat", "poop");