-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy paththis.js
More file actions
104 lines (86 loc) · 3.14 KB
/
Copy paththis.js
File metadata and controls
104 lines (86 loc) · 3.14 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
// We have a simple object we'll call "calculator"
const calculator = {
// There is a total on it that starts out as 0
total: 0,
// We have an add method,
add(x, y) {
return x + y;
},
// A subtract method,
subtract(x, y) {
return x - y;
},
// A multiply method,
multiply(x, y) {
return x * y;
},
// And a divide method
divide(x, y) {
return x / y;
},
// In order to modify the total, we use the "calculate" method
// We first pass in one of the methods defined above that will modify it accordingly (modify)
// We also pass in a number that will be the magnitude of the modification (y)
// It then returns the new total so we can see it change
calculate(modify, y) {
return this.total = modify(this.total, y);
},
// This method can be used to start the "total" over from scratch
clear() {
this.total = 0;
},
};
// Let's start our calculations by adding 13 to the current total
console.log(calculator.calculate(calculator.add, 13));
// And then let's subtract 3 from the result
console.log(calculator.calculate(calculator.subtract, 3));
// This "calculator." is driving me crazy! There HAS to be a way that makes it simpler
// Let's assign that method to another variable we'll name after the method
let calculate = calculator.calculate;
// And let's try to multiply the result by 6
console.log(calculate(calculator.multiply, 6));
// NaN - ???
// Where'd that come from?
/*
This is a common problem in JavaScript, especially with callbacks.
We could be passing the "calculator.calculate" function as a part of a Promise.
We could be assigning it to a variable and using that instead (like we did here).
So what is happening?
"this" refers to the current object context of a function. When a function is called in reference to an object
like:
```
calculator.calculate(...)
```
or
```
calculator['calculate'](...)
```
or is run inside of another method like:
```
const calculator = {
...
displayTotal() {
console.log(this.total);
},
calculate(modify, y) {
this.total = modify(this.total, y);
this.displayTotal();
return this.total;
},
...
};
```
in reference to "this.displayTotal", since it's being run inside of the "calculate" method (this also works
if "displayTotal" isn't a method but instead a separate function that still uses the "this" keyword and runs
inside of the "calculate" method).
The reason why it breaks is when the "calculate" method is assigned to a separate variable, and that
function is run not in reference to the "calculator" object like:
```
calculate(...)
```
it no longer is referencing the "calculator" object, and in this case, since the function isn't being run inside
of any other object, it's referencing the global object instead ("global" if in Node, "window" if in browser).
As such, we're going to need to ensure that "this" references the right object.
There are three different strategies that we can use in order to ensure that we don't get this problem.
In this same folder, they can be found in "bind.js", "call.js", and "apply.js".
*/