Repository navigation
Expand file tree
/
Copy paththis.js
More file actions
83 lines (64 loc) · 1.93 KB
/
Copy paththis.js
File metadata and controls
83 lines (64 loc) · 1.93 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
/* The for principles of "this";
* in your own words. explain the four principle for the "this" keyword below.
*
* 1. Window/Global Object Binding: When in global scope, this refers to the window/console
* 2. Implicit Binding: When a function is called by an object, that object to the left of the dot is 'this'.
* 3. New Binding: When a constructor is used, this refers to the new object that is returned by the constructor.
* 4. Explicit Binding: Whenever .call or .apply methods are used.
*
* write out a code example of each explanation above
*/
console.log('hello world!');
// Principle 1
// code example for Window Binding
const windowBinding = name => {
console.log(this);
return name;
}
windowBinding('Zack');
// Principle 2
// code example for Implicit Binding
const implicitBinding = obj => {
obj.sayName = function() {
console.log(`Hello my name is ${this.name}`);
console.log(this);
};
}
const me = {name: 'Zack'};
implicitBinding(me);
me.sayName();
// Principle 3
// code example for New Binding
function NewBinding(greeter) {
this.greeting = 'Hello';
this.greeter = greeter.name;
this.speak = function() {
console.log(`${this.greeting}, ${this.greeter}`);
console.log(this);
};
}
const zack = new NewBinding({name: 'Zack'});
zack.speak();
const elvis = new NewBinding({name: 'Elvis'});
elvis.speak();
// Principle 4
// code example for Explicit Binding
//using NewBinding above:
zack.speak.call(elvis);
elvis.speak.apply(zack);
class Animal {
constructor(object) {
this.type = object.type;
this.name = object.name;
this.sound = object.sound;
}
speak() {
return this.sound;
}
}
const pupper = new Animal({type: 'Pupper', name: 'Elvis', sound: 'Ruff'});
console.log(pupper);
const doggo = new Animal({type: 'Doggo', name: 'Oreo', sound: 'Woof'});
console.log(doggo);
console.log(pupper.speak());
console.log(doggo.speak.call(pupper));