You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* in your own words. explain the four principle for the "this" keyword below.
1
+
/* The four principles of "this";
2
+
* in your own words. explain the four principles for the "this" keyword below.
3
3
*
4
-
* 1.
5
-
* 2.
6
-
* 3.
7
-
* 4.
4
+
* 1. When a function is inside the global scope, 'this' refers to the window/console object.
5
+
* 2. When used with dot notation, the object before the dot is what 'this' is referring to.
6
+
* 3. Whenever we're using a constructor function, 'this' refers to the specific instance of that object that's created and returned by the constructor function.
7
+
* 4. Whenever we're using the call or apply methods, the object that 'this' is referring to is explicitly defined. This is as specific as we go.
8
8
*
9
9
* write out a code example of each explanation above
10
10
*/
11
11
12
12
// Principle 1
13
13
14
14
// code example for Window Binding
15
+
// We're using a function inside the global scope, so the 'this' refers to the entire window/console here.
15
16
16
-
// Principle 2
17
+
// function sayName(name) {
18
+
// console.log(this);
19
+
// return name;
20
+
// }
21
+
// sayName('Stephen');
17
22
18
-
// code example for Implicit Binding
23
+
// // Principle 2
24
+
25
+
// // code example for Implicit Binding
26
+
// // It is implied that since we're using dot notation, whatever is to the left of the dot is the object we're referring to.
27
+
28
+
// const myObj = {
29
+
// greeting: 'Hello',
30
+
// sayHello: function(name) {
31
+
// console.log(`${this.greeting} my name is ${name}`);
32
+
// console.log(this);
33
+
// }
34
+
// };
35
+
// myObj.sayHello('Stephen');
19
36
20
37
// Principle 3
21
38
22
39
// code example for New Binding
23
40
41
+
// function CordialPerson(greeter) {
42
+
// this.greeting = 'Hello ';
43
+
// this.greeter = greeter;
44
+
// this.speak = function() {
45
+
// console.log(this.greeting + this.greeter);
46
+
// // console.log(this);
47
+
// };
48
+
// }
49
+
50
+
// const jerry = new CordialPerson('Newman');
51
+
// const newman = new CordialPerson('Jerry');
52
+
53
+
// jerry.speak();
54
+
// //when Jerry speaks, he references the argument that was provided in CordialPerson('Newman').
55
+
// //This is passed along via the 'this' bindings - this.greeting = 'Hello ' plus this.greeter = greeter (which we already provided a value for via the argument.)
56
+
57
+
// newman.speak();
58
+
59
+
// //Same thing here when Newman speaks, except in this case Jerry has been provided as the argument instead of Newman's name.
60
+
24
61
// Principle 4
25
62
26
-
// code example for Explicit Binding
63
+
// code example for Explicit Binding
64
+
65
+
functionCordialPerson(greeter){
66
+
this.greeting='Hello ';
67
+
this.greeter=greeter;
68
+
this.speak=function(){
69
+
console.log(this.greeting+this.greeter);
70
+
console.log(this);
71
+
};
72
+
}
73
+
74
+
constjerry=newCordialPerson('Newman');
75
+
constnewman=newCordialPerson('Jerry');
76
+
77
+
jerry.speak.call(newman);
78
+
//with jerry.speak.call, we've re-bound jerry to reference newman instead. So now that we've re-bound jerry to newman, the jerry CordialPerson is going to behave like newman.
79
+
80
+
newman.speak.apply(jerry);
81
+
//likewise with newman.speak.apply, we've re-bound newman to reference jerry instead. It will behave like jerry instead of newman and will take the arguments that the original jerry object used. The difference here is only that .apply will expect a second argument to be an array that it uses as arguments for the function.
0 commit comments